目錄
- NSArray和NSMutableArray
- NSDictionary和NSMutableDictionary
NSArray和NSMutableArray
NSArray類用來(lái)存儲(chǔ)對(duì)象的有序列表,我們可以在NSArray中放入任意類型的對(duì)象。
NSArray類有兩個(gè)限制:
- 只能存儲(chǔ)OC對(duì)象序仙,而不能存儲(chǔ)原始的C語(yǔ)言基礎(chǔ)的數(shù)據(jù)類型,例如:int店展、float挥唠、enum液走、struct等医吊。
- NSArray中不能存儲(chǔ)nil(對(duì)象的零值或NULL值)
下面通過(guò)代碼看一下NSArray類常用的使用方法:
int main(int argc, const char * argv[]) {
@autoreleasepool {
//通過(guò)類方法arrayWithObjects:創(chuàng)建一個(gè)新的NSArray钱慢。
//其中對(duì)象列表以逗號(hào)分隔,在列表結(jié)尾添加nil代表列表結(jié)束卿堂。
//這就是不能在數(shù)組中存儲(chǔ)nil的一個(gè)原因束莫。
NSArray *array=[NSArray arrayWithObjects:@"one",@123, @"two",nil];
//等價(jià)于上面一行代碼
//這種代碼書寫方式叫做字面量語(yǔ)法,使用該語(yǔ)法時(shí)草描,不必在結(jié)尾處特意補(bǔ)上nil麦箍。
NSArray *array2=@[@"one",@123, @"two"];
//獲取數(shù)字包含的對(duì)象個(gè)數(shù)
[array count];
//獲取特定索引處的對(duì)象
[array2 objectAtIndex:1];
//等價(jià)于上面代碼語(yǔ)法
//array2[0];
/*輸出結(jié)果:
index 0 has one
index 1 has 123
index 2 has two
*/
for (NSInteger i=0; i<[array count]; i++) {
NSLog(@"index %ld has %@",(long)i,[array objectAtIndex:i]);
}
//將字符串分割成數(shù)組
NSString *string=@"123,456,789";
NSArray *array3=[string componentsSeparatedByString:@","];
/*輸出結(jié)果:
index 0 has 123
index 1 has 456
index 2 has 789
*/
for (NSInteger i=0; i<[array3 count]; i++) {
NSLog(@"index %ld has %@",(long)i,[array3 objectAtIndex:i]);
}
//將數(shù)組元素拼接成字符串
NSString *string1=[array3 componentsJoinedByString:@";"];
//輸出結(jié)果:123;456;789
NSLog(@"%@",string1);
}
return 0;
}
NSArray創(chuàng)建的是不可變對(duì)象的數(shù)組。一旦創(chuàng)建了一個(gè)包含特定數(shù)量的對(duì)象的數(shù)組陶珠,我們既不能添加任何元素也不能刪除任何元素。
為了彌補(bǔ)NSArray類不足享钞,便有了NSMutableArray可變數(shù)組類揍诽,這樣就可以隨意地添加或刪除數(shù)組中對(duì)象了。
int main(int argc, const char * argv[]) {
@autoreleasepool {
//NSMutableArray可以通過(guò)類arrayWithCapacity來(lái)創(chuàng)建可變數(shù)組栗竖。
NSMutableArray *array=[NSMutableArray arrayWithCapacity:10];
for (NSInteger i=0; i<4; i++) {
//在數(shù)組末尾添加對(duì)象
[array addObject:@1];
}
//刪除特定索引處的對(duì)象
[array removeObjectAtIndex:0];
}
return 0;
}
NSDictionary和NSMutableDictionary
NSDictionary能在給定的關(guān)鍵字(通常是一個(gè)NSString字符串)下存儲(chǔ)一個(gè)數(shù)值(可以是任意類型的OC對(duì)象)暑脆,然后就可以用這個(gè)關(guān)鍵字來(lái)查找相應(yīng)的數(shù)據(jù)。
字典使用的是鍵查詢的優(yōu)化方式狐肢。它可以立即找出要查詢的數(shù)據(jù)添吗,而不需要遍歷整個(gè)數(shù)組。對(duì)于頻繁的查詢和大型的數(shù)據(jù)集來(lái)說(shuō)份名,使用字典比數(shù)組要快得多碟联。
int main(int argc, const char * argv[]) {
@autoreleasepool {
WZKPerson *person1=[WZKPerson new];
WZKPerson *person2=[WZKPerson new];
WZKPerson *person3=[WZKPerson new];
WZKPerson *person4=[WZKPerson new];
//dictionaryWithObjectsAndKeys:后面的參數(shù)先是要存儲(chǔ)的對(duì)象妓美,然后才是關(guān)鍵字;
NSDictionary *persons=[NSDictionary dictionaryWithObjectsAndKeys:
person1,@"p1",
person2,@"p2",
person3,@"p3",
person4,@"p4",
nil];
//或者可以采用字面量語(yǔ)法鲤孵,關(guān)鍵字在前壶栋,數(shù)值在后,并且關(guān)鍵字和數(shù)值之間用冒號(hào)分開(kāi)
NSDictionary *persons2=@{@"p1":person1,@"p2":person2,@"p3":person3,@"p4":person4};
WZKPerson *person=[persons objectForKey:@"p1"];
//等價(jià)于
WZKPerson *newPerson=persons2[@"p1"];
}
return 0;
}
NSMutableDictionary類允許隨意添加和刪除字典元素普监。
int main(int argc, const char * argv[]) {
@autoreleasepool {
WZKPerson *person1=[WZKPerson new];
WZKPerson *person2=[WZKPerson new];
WZKPerson *person3=[WZKPerson new];
WZKPerson *person4=[WZKPerson new];
//使用setObject:forKey:方法為字典添加元素
NSMutableDictionary *persons=[NSMutableDictionary dictionary];
[persons setObject:person1 forKey:@"p1"];
[persons setObject:person2 forKey:@"p2"];
[persons setObject:person3 forKey:@"p3"];
[persons setObject:person4 forKey:@"p4"];
//根據(jù)Key移除某特定對(duì)象
[persons removeObjectForKey:@"p1"];
//移除所有對(duì)象
[persons removeAllObjects];
}
return 0;
}
對(duì)于字典中已有的關(guān)鍵字使用setObject:forKey:方法贵试,那么這個(gè)方法將會(huì)用新值替換掉原有的數(shù)值。