一:字典 NSDictionary
概述:OC中的字典是以鍵值對的形式來存儲數據的 (key value),字典當中的key值是唯一的;
本小結知識點:
1: 創建字典類
2:快捷創建字典類
3:字典中可以存儲任意數據類型
4:獲得字典的長度
5:從字典中取值
6:遍歷字典
詳細內容請看下面代碼:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//字典類:NSDictionary,字典是以鍵值對的形式來存儲數據的 key value,字典當中的key值是唯一的,不能重複,value無所謂
//1:創建字典類,並且是不可變的
//兩個參數:分別對應value (rebwenyi)和 key (name)
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"renwenyi",@"name", nil];
NSLog(@"dict 結果:%@",dict);
//2:創建快捷方式
//格式:@{key:value};
NSDictionary *dict1 = @{@"one":@"1",@"two":@"2"};
NSLog(@"dict1 = %@",dict1);
//3:字典中可以存儲任意數據類型
NSDictionary *dict2 =
@{
@"name" :@"renwenyi",
@"number":[NSNumber numberWithInt:20],
@"dict" :dict1
};
//打印出來不一定是按照存入的順序
NSLog(@"dict2 = %@",dict2);
//4:獲得字典的長度
NSLog(@"dict2字典長度= %zi",dict2.count);//相當於key值的個數
//5:從字典中取值
NSString *s1 = [dict2 objectForKey:@"name"];
NSLog(@"s1代表的value = %@",s1);
NSNumber *numm = [dict2 objectForKey:@"number"];
NSLog(@"num = %@",numm);
//6:遍歷字典
//①:for循環遍歷
//思考:爲什麼要使用for循環呢?由上面的例子我們可以看出可以根據key來取值,字典裏面可能有很多的key,所以我們就可以用for循環遍歷取出所有的value
NSArray *allkeys = [dict2 allKeys];
for (int i = 0 ; i<allkeys.count; i++) {
//如果你的字典中存儲有多種不同的類型,那麼最好用id類型來接收它
id obj = [dict2 objectForKey:[allkeys objectAtIndex:i]];
NSLog(@"obj = %@",obj);
}
//②:枚舉器
NSEnumerator *enumerator = [dict2 objectEnumerator];
id value;
while (value = [enumerator nextObject ]) {
NSLog(@"value 爲%@",value);
}
}
return 0;
}
二:可變字典 NSMutableDictionary
概述:NSMutableDictionary繼承自 NSDictionary,是可變的
本小知識點:
1:創建可變字典
2:向字典中添加元素
3:向字典中刪除元素
4:向字典中添加對象
5:遍歷字典
詳細內容請看下面代碼:
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//可變字典 NSMutableDictionary 繼承自 NSDictionary
//1:創建一個可變字典
NSMutableDictionary *mutdict = [[NSMutableDictionary alloc]init];
//2:向字典中添加元素
[mutdict setObject:@"one" forKey:@"1"];
[mutdict setObject:@"two" forKey:@"2"];
[mutdict setObject:@"three" forKey:@"3"];
[mutdict setObject:@"four" forKey:@"4"];
NSLog(@"mutdict = %@",mutdict);
//3:刪除字典中的元素
[mutdict removeObjectForKey:@"4"];
NSLog(@"mutdict = %@",mutdict);
//4:刪除全部元素
[mutdict removeAllObjects];
NSLog(@"mutdict = %@",mutdict);
//5:向字典中添加對象(創建一個student對象)
Student *stu1 = [[Student alloc]initWithAge:24 andName:@"renwenyi"];
Student *stu2 = [[Student alloc]initWithAge:20 andName:@"xiaoming"];
Student *stu3 = [[Student alloc]initWithAge:30 andName:@"xijinping"];
//注意:在向字典中存儲數據的時候一定要保證key值是唯一的
//把對象加入到字典中
[mutdict setObject:stu1 forKey:@"s1"];
[mutdict setObject:stu2 forKey:@"s2"];
[mutdict setObject:stu3 forKey:@"s3"];
NSLog(@"mudict = %@",mutdict);
//6:遍歷可變字典
//①:for循環遍歷
//注意:同數組一樣,遍歷的時候不要刪除其中的元素
NSArray *allkeys = [mutdict allKeys];
for (int i=0 ; i<allkeys.count; i++) {
Student *student = [mutdict objectForKey:[allkeys objectAtIndex:i]];
NSLog(@"student : %@",student);
} //②:枚舉器
NSEnumerator *enumerator = [mutdict objectEnumerator];
Student *value;
while (value = [enumerator nextObject]) {
NSLog(@"value : %@",value);
}
//增強for循環不合適,因爲你取出來的對象還是字典類型,還需要再找key值,通過key找value,麻煩了
}
return 0;
}
下面是student類的.m文件
#import "Student.h"
@implementation Student
-(id)initWithAge:(int)age andName :(NSString *)name
{
if (self = [super init]) {
_age = age;
_name = name;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"age = %i, name = %@", _age ,_name];
}
@end