OC學習 第六章 NSArray

2021-07-06 07:46:38 字數 2868 閱讀 3473

oc學習   第六章                      nsarray

1.建立: 

(1)   nsarray *arr = [nsarray array];//初始化乙個空的陣列

(2) nsarray *arr1 = [nsarray arraywithobject:@"qwe"];//初始化乙個包含乙個元素的陣列

nslog(@"%@",arr1);

(3)   nsarray *arr2 = [nsarray arraywithobjects:@"1",@"2",@"234", nil];//初始化多個元素的數 組,元素之間用逗號隔開,最後以nil結束

(4)nsarray *arr3 = @[

@"12",

@"23",

@"34"

];//初始化多個元素的陣列,元素之間用逗號隔開,注意,最後乙個元素後面不要加逗號。

2.常用方法:

nsuinteger count = arr3.count;//陣列元素的個數

[arr3 firstobject];//陣列的第乙個元素

[arr3 lastobject];//陣列的最後乙個

[arr3 objectatindex:2];//獲取陣列某個位置的元素

int i =  [arr3 indexofobject:@"34"];//獲取某個元素在陣列中的位置

bool flag =[arr3 containsobject:@"12"];//這判斷陣列中是否包含某個元素

nslog(@"===%d",flag)

3.陣列遍歷:

for迴圈遍歷:

for(

inti =

0; i < arr.

count

; i++)

列舉器遍歷:

nsenumerator

*e = [arr

objectenumerator];

nsstring

*obj ;

while

(obj = [e

nextobject])

快速列舉:

for(id

obj1

inarr)

4.陣列存放自定義類的物件(遍歷):

student

*stu = [[

student

alloc

]init];

stu.

name

=

@"122";

student

*stu1 = [[

student

alloc]

init];

stu1.

name

=

@"123";

nsarray

*stuarr =

@[ stu,

stu1

];

for(

inti =

0; i < stuarr.

count; i++)

5.陣列排序:

1.       nsarray*arr =

@[@"reg",

@"ehb",

@"fbd"

];

nsarray *sortarr = [arr

sortedarrayusingselector:

@selector

(compare:)];(只能是公升序)

nslog

(@"%@"

,sortarr);用要排序的原陣列呼叫例項方法,第二個引數,是方法選擇,如果陣列的元素都是字串,那麼直接用compare:就行

2.      

nsarray *arr2 = [arr

sortedarrayusingcomparator:^

nscomparisonresult(

id obj1,

id obj2) ];            字串才有compare:方法

nslog

(@"%@"

,arr2);根據比較結果,如果結果是1,則交換

3.

-(nscomparisonresult

)comparewithname:(

student

*)stu

4.

-(nscomparisonresult

)comparewithage:(

student

*)stu

else

return

result;

}

// 描述器排序

nssortdescriptor

*sort = [

nssortdescriptor

sortdescriptorwithkey

:@"age"

ascending

:yes ];

nsarray

*scorearr =

@[ sort

] ;

nsarray

*arr10 = [arr

sortedarrayusingdescriptors

:scorearr];

nslog

(@"%@"

,arr10);

//block(

**塊)

排序nsarray *sortarr2 = [arr sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

nslog(@"%@",sortarr2);

OC 第六章筆記摘要

nsinteger 大致等於long nsuinteger 大致等於unsigned long型整數 cgfloat 在64位平台上大致相當於double,在32位平台上大致相當於float。他們並不是包裝類,它們只是基本型別 nsvalue和nsnumber都是包裝類 nsvalue是nsnumb...

mysql第六章 第六章 mysql日誌

第六章 mysql日誌 一 錯誤日誌 錯誤日誌的預設存放路徑是 mysql 存放資料的地方 hostname.err 1.修改錯誤日誌存放路徑 mysqld log error data mysql mysql.log 2.檢視配置命令 show variables like log error 3...

第六章 學習筆記

1.引數分為位置引數和關鍵字引數。def func positional para x,y,z passdef func keyword para kwd1 1,kwd2 4,kwd3 9 pass2.1 當有多個位置引數時,可以用乙個星號來收集引數,函式內使用這些位置引數時可以通過for 迴圈依次...