Objective-C中NSArray和NSMutableArray是如何使用的?

Objective-C的數(shù)組比C++,Java的數(shù)組強(qiáng)大在于,NSArray保存的對(duì)象可以是不同的對(duì)象。但只能保存對(duì)象,int ,char,double等基本數(shù)據(jù)類型不能直接保存,需要通過(guò)轉(zhuǎn)換成對(duì)象才能加入數(shù)組。

image

基本概念

1、NSArray 不可變數(shù)組

[array count] : 數(shù)組的長(zhǎng)度。 [array objectAtIndex 0]: 傳入數(shù)組腳標(biāo)的id 得到數(shù)據(jù)對(duì)象。 [arrayWithObjects; ...] :向數(shù)組對(duì)象初始化賦值。這里可以寫任意對(duì)象的指針,結(jié)尾必須使用nil。

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; 
padding: 8px; font-weight: normal; position: relative; white-space: pre-wrap; 
overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace;
font-size: 14px; line-height: 22px; color: rgb(0, 0, 0);">#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
 { @autoreleasepool { NSObject *obj = [[NSObject alloc]init];
 NSArray *array = [NSArray arrayWithObjects: @"a", obj, @"c",nil];
 NSLog(@"array Count:%lu",[array count]);
 //遍歷數(shù)組 for (NSObject *object in array)
 { NSLog(@"數(shù)組對(duì)象:%@", object);
 } 
[obj release]; 
} return 0; }
</pre>

打印結(jié)果:

2012-07-09 10:52:17.050 objectiveC[944:403] array Count:3

2012-07-09 10:52:17.052 objectiveC[944:403] 數(shù)組對(duì)象:a

2012-07-09 10:52:17.052 objectiveC[944:403] 數(shù)組對(duì)象:< NSObject : 0x7fe479c14110>

2012-07-09 10:52:17.053 objectiveC[944:403] 數(shù)組對(duì)象:c

|

查找數(shù)組索引對(duì)應(yīng)的對(duì)象

NSLog(@"Index 2:%@", [array objectAtIndex:2]);

打印結(jié)果:2012-07-09 10:55:16.382 objectiveC[993:403] Index 2:c

2、NSMutableArray可變對(duì)象數(shù)組

[NSMutableArray arrayWithCapacity:6] :初始化可變數(shù)組對(duì)象的長(zhǎng)度,如果后面代碼繼續(xù)添加數(shù)組超過(guò)長(zhǎng)度6以后NSMutableArray的長(zhǎng)度會(huì)自動(dòng)擴(kuò)充,6是自己可以設(shè)置的顆粒度。 [array addObject:...] : 向可變數(shù)組尾部添加數(shù)據(jù)對(duì)象。 [array addObjectsFromArray:..] :向可變數(shù)組尾部添加一個(gè)數(shù)組對(duì)象。

2.1 普通使用:


<pre style="box-sizing: border-box; outline: 0px; 
margin: 0px 0px 24px; padding: 8px; font-weight: normal; 
position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; 
font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; 
color: rgb(0, 0, 0);">#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
 { @autoreleasepool { NSObject *obj = [[NSObject alloc]init];
 NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
 [muArray addObject:@"對(duì)象1"]; [muArray addObject:@"對(duì)象2"];
 [muArray addObject:@"對(duì)象3"]; [muArray addObject:@"對(duì)象4"];
 [muArray insertObject:@"攪局的" atIndex:2]; [muArray addObject:obj];
 for (NSObject * object in muArray) { NSLog(@"數(shù)組對(duì)象:%@", object);
 } [obj release]; } return 0; }</pre>

打?。?/strong>

2012-07-09 14:01:08.994 objectiveC[2090:403] 數(shù)組對(duì)象:對(duì)象1 2012-07-09 14:01:08.996 objectiveC[2090:403] 數(shù)組對(duì)象:對(duì)象2 2012-07-09 14:01:08.997 objectiveC[2090:403] 數(shù)組對(duì)象:攪局的 2012-07-09 14:01:08.997 objectiveC[2090:403] 數(shù)組對(duì)象:對(duì)象3 2012-07-09 14:01:08.998 objectiveC[2090:403] 數(shù)組對(duì)象:對(duì)象4 2012-07-09 14:01:08.998 objectiveC[2090:403] 數(shù)組對(duì)象:<NSObject: 0x109714110>

NSRange range = NSMakeRange(0,3); 設(shè)置一個(gè)范圍為 0 到 3 之間。 [array removeObject:obj inRange:range] : 設(shè)置在一個(gè)范圍內(nèi)刪除數(shù)據(jù),如果這個(gè)范圍內(nèi)沒(méi)有刪除的這個(gè)對(duì)象則不會(huì)刪除任何東西。例子中因?yàn)閛bj對(duì)象在 數(shù)組 0 到 3的范圍內(nèi),所以obj就被刪除掉了。

2.2 removeObject和removeObjectIdenticalTo

[array removeObject:(id)] :刪除數(shù)組中指定元素,根據(jù)對(duì)象isEqual消息判斷。

[array removeObjectIdenticalTo:(id)] : 刪除數(shù)組中指定元素,根據(jù)對(duì)象的地址判斷

[array removeObjectIdenticalTo:(id) inRange:(NSRange)] : 在指定范圍內(nèi)刪除指定的元素。

[array removeObjectsInArray:(NSArray *)] :刪除一個(gè)數(shù)組的元素。

下面我們主要驗(yàn)證下removeObject removeObjectIdenticalTo這兩個(gè)方法的區(qū)別,

先實(shí)驗(yàn)removeObject
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *str1 = [[NSString alloc] init]; NSString *str2 = [[NSString alloc] init];
 NSString *str3 = [str1 stringByAppendingFormat:@"字符串"]; 
NSString *str4 = [str2 stringByAppendingFormat:@"字符串"]; 
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6]; 
[muArray addObject:@"對(duì)象"]; [muArray addObject:str3];
[muArray addObject:str4];
 for (NSObject * object in muArray) { NSLog(@"數(shù)組對(duì)象:%@", object); 
} if ([str3 isEqual:str4]) { NSLog(@"str1 isEqual str2"); } if (str3 == str4) { NSLog(@"str1 == str2");
 } [muArray removeObject:str3]; for (NSObject * object in muArray) { NSLog(@"數(shù)組對(duì)象:%@", object);
 } [str1 release]; [str2 release];
 } return 0; }

運(yùn)行打?。?/p>

2012-07-09 14:57:52.059 objectiveC[2399:403] 數(shù)組對(duì)象:對(duì)象 2012-07-09 14:57:52.061 objectiveC[2399:403] 數(shù)組對(duì)象:字符串 2012-07-09 14:57:52.062 objectiveC[2399:403] 數(shù)組對(duì)象:字符串 2012-07-09 14:57:52.062 objectiveC[2399:403] str1 isEqual str2 2012-07-09 14:57:52.063 objectiveC[2399:403] 數(shù)組對(duì)象:對(duì)象

這時(shí),“字符串”都被去除了。

現(xiàn)在試試 removeObjectIdenticalTo

代碼改成: [muArray removeObjectIdenticalTo:str3];

打?。?/p>

|

2012-07-09 14:59:53.520 objectiveC[2432:403] 數(shù)組對(duì)象:對(duì)象
2012-07-09 14:59:53.521 objectiveC[2432:403] 數(shù)組對(duì)象:字符串
2012-07-09 14:59:53.522 objectiveC[2432:403] 數(shù)組對(duì)象:字符串
2012-07-09 14:59:53.523 objectiveC[2432:403] str1 isEqual str2
2012-07-09 14:59:53.523 objectiveC[2432:403] 數(shù)組對(duì)象:對(duì)象
2012-07-09 14:59:53.524 objectiveC[2432:403] 數(shù)組對(duì)象:字符串

|

這是還有一個(gè)“字符串”留下來(lái)了。

我們用對(duì)比兩個(gè)對(duì)象isEqual,用==對(duì)比對(duì)象的地址,打印出來(lái)了str1 isEqual str2

<pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: normal; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0);"> NSRange rang = NSMakeRange(0, 3);
[muArray removeObject:obj inRange:rang];</pre>

指定范圍刪除對(duì)象,其他方法也可以指定范圍刪除,如果對(duì)象不在這個(gè)范圍內(nèi),則不會(huì)被刪除。

2.3替換某索引值對(duì)應(yīng)的對(duì)象

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSObject *obj = [[NSObject alloc]init]; NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6]; [muArray addObject:@"對(duì)象1"]; [muArray addObject:@"對(duì)象2"]; [muArray addObject:@"對(duì)象3"]; [muArray addObject:@"對(duì)象4"]; [muArray addObject:obj]; for (NSObject * object in muArray) { NSLog(@"數(shù)組對(duì)象:%@", object); } [muArray replaceObjectAtIndex:4 withObject:@"字符串替換回來(lái)"]; for (NSObject * object in muArray) { NSLog(@"數(shù)組對(duì)象:%@", object); } [obj release]; } return 0; }

替換后的打印結(jié)果:

2012-07-09 15:06:01.919 objectiveC[2497:403] 數(shù)組對(duì)象:對(duì)象1 2012-07-09 15:06:01.920 objectiveC[2497:403] 數(shù)組對(duì)象:對(duì)象2 2012-07-09 15:06:01.920 objectiveC[2497:403] 數(shù)組對(duì)象:對(duì)象3 2012-07-09 15:06:01.921 objectiveC[2497:403] 數(shù)組對(duì)象:對(duì)象4 2012-07-09 15:06:01.921 objectiveC[2497:403] 數(shù)組對(duì)象:字符串替換回來(lái)

** 3、數(shù)組迭代的遍歷方法**

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSObject *obj = [[NSObject alloc]init]; NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6]; 
[muArray addObject:@"對(duì)象1"]; [muArray addObject:@"對(duì)象2"]; [muArray addObject:@"對(duì)象3"]; 
[muArray addObject:@"對(duì)象4"]; [muArray addObject:obj]; NSEnumerator *enmuerator = [muArray objectEnumerator]; 
id object; while (object = [enmuerator nextObject]) { NSLog(@"數(shù)組中的對(duì)象:%@", object); 
} [obj release]; } return 0; }

NSArray 跟 NSMutableArray 使用 區(qū)別

NSArray 可變數(shù)組

一、NSArray是靜態(tài)數(shù)組,創(chuàng)建后數(shù)組內(nèi)容及長(zhǎng)度不能再修改。

實(shí)例:

 

//用arrayWithObjects初始化一個(gè)不可變的數(shù)組對(duì)象。
//初始化的值之間使用逗號(hào)分開,以nil結(jié)束。
NSArray6 *city = [NSArray arrayWithObjects:@"上海",@"廣州",@"重慶",nil];

for(int i=0; i < [city count];i++){
    NSLog(@"%@",[city objectAtIndex:i]);
}

NSArray常用方法:

+(id)arrayWithObjects:obj1,obj2,...nil             //創(chuàng)建一個(gè)新的數(shù)組,obj1,obj2,.., 以nil結(jié)束。
-(BOOL)containsObject:obj                          //確定數(shù)組中是否包含對(duì)象obj
-(NSUInterger)count                                //數(shù)組中元素的個(gè)數(shù)
-(NSUInterger)indexOfObject:obj                    //第一個(gè)包含obj元素的索引號(hào)
-(id)ObjectAtIndex:i                               //存儲(chǔ)在位置i的對(duì)象
-(void)makeObjectsPerformSelector:(SEL)selector    //將selector提示的消息發(fā)送給數(shù)組中的每一個(gè)元素
-(NSArray*)sortedArrayUsingSelector:(SEL)selector  //根據(jù)selector提定的比較方法對(duì)數(shù)組進(jìn)行排序
-(BOOL)writeToFile:path atomically:(BOOL)flag      //將數(shù)組寫入指定的文件中。如果flag為YES,則需要先創(chuàng)建一個(gè)臨時(shí)文件

二、NSMutableArray是動(dòng)態(tài)數(shù)組,可以動(dòng)態(tài)增加數(shù)組中的元素,同樣NSMutableArray是NSArray的子類。
實(shí)例:

//用arrayWithCapacity創(chuàng)建一個(gè)長(zhǎng)度為5的動(dòng)態(tài)數(shù)組
NSMutableArray *nsma = [MSMutableArray arrayWithCapacity:5];
for(int i=0;i<=50;i++) {
   if( i%3 == 0 ) {
       [nsma addObject:[NSNumber numberWithInteger:i]];   //用addObject給數(shù)組nsma增加一個(gè)對(duì)象
    }
}

//輸出數(shù)組中的元素

for(int i=0;i<[nsma count];i++) {
   NSLog(@"%li",(long)[[nsma objectAtIndex] integerValue]);
}

NSMutableArray不可變數(shù)組

NSMutableArray常用方法:

+(id)array //創(chuàng)建一個(gè)空數(shù)組
+(id)arrayWithCapacity:size //創(chuàng)建一個(gè)容量為size的數(shù)組
-(id)initWithCapacity:size //初始化一個(gè)新分配的數(shù),指定容量為size
-(void)addObject:obj //將obj增加到數(shù)組中
-(void)insertObject:obj atIndex:i //將obj插入數(shù)據(jù)的i元素
-(void)replaceObjectAtIndex:i withObject:obj //用obj替換第i個(gè)索引的對(duì)象
-(void)removeObject:obj //從數(shù)組中刪除所有是obj的對(duì)象
-(void)removeObjectAtIndex:i //從數(shù)組中刪除索引為i的對(duì)象
-(void)sortUsingSelector:(SEL)selector //用selector指示的比較方法進(jìn)行排序

歡迎加入我的iOS交流圈:
不管你是小白還是大牛歡迎入駐!!
分享內(nèi)容包括逆向安防、算法、架構(gòu)設(shè)計(jì)、多線程,網(wǎng)絡(luò)進(jìn)階,還有底層、音視頻、Flutter等等......
自己根據(jù)梳理網(wǎng)絡(luò)來(lái)的的開發(fā)經(jīng)驗(yàn)總結(jié)的學(xué)習(xí)方法,無(wú)償分享給大家。需要的話都可以自行來(lái)獲取下載。
+裙:196800191
) +群密碼:007956 或者是+ WX(XiAZHiGardenia)免費(fèi)獲??! 獲取面試資料 簡(jiǎn)歷模板 一起交流技術(shù)

> 若有不準(zhǔn)確的地方,歡迎評(píng)論指出。同時(shí)若是有好的題目或者文章也希望大家可以再評(píng)論區(qū)分享討論,小編會(huì)時(shí)刻關(guān)注大家的評(píng)論及時(shí)精選和頂置的哦~共同學(xué)習(xí),互相勉勵(lì)!

同時(shí)也感謝各位看官踴躍發(fā)言~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容