標籤:資料 string 類 set cti name
// ********************不可變最字典*****************
/* NSDictionary * dic = [NSDictionary dictionaryWithObject:@"張三" forKey:@"name" ];
NSLog(@"%@", dic);//便利構造器建立字典
//字典裡一個key只有一個vlaue, 但是一個value可以有好幾個key
NSDictionary * dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan", @"name", @"boy", @"sex", @"toyke", @"address", nil];//初始化建立字典
NSLog(@"%@", dic1);
NSArray * value = [[NSArray alloc] initWithObjects:@"張三", @"李四", @"王五", @"趙六", nil];
NSArray * key = [[NSArray alloc] initWithObjects:@"name3", @"name4", @"name5", @"name6", nil];
NSDictionary * nk = [[NSDictionary alloc] initWithObjects:value forKeys:key];//初始化+數組建立字典
NSLog(@"%@", nk);
NSLog(@"%lu", [nk count]);//擷取索引值對的個數
NSString * result = [nk objectForKey:@"name3"];//根據鍵來獲得相對應的值
NSLog(@"%@", result);
NSString * re = [nk objectForKey:@"name6"];
NSString * re1 = [nk objectForKey:@"name4"];
NSString * re3 = [nk objectForKey:@"name5"];
NSLog(@"%@, %@, %@", re, re1, re3);
NSArray * cou = [nk allKeys];//獲得所有的鍵
for (int i = 0; i < [cou count]; i++) {
NSString * key = [cou objectAtIndex:i];//獲得數組中的鍵
NSString * value = [nk objectForKey:key];//用鍵來獲得值
NSLog(@"%@", value);
}
NSArray * cou1 = [nk allKeys];
for (int i = 0; i < [cou1 count]; i++) {
NSLog(@"%@", [nk objectForKey:[cou objectAtIndex:i]]);
}
NSArray * values = [nk allValues];//獲得所有的值存放在數組中
for (int i = 0; i < [values count]; i++) {
id c = [values objectAtIndex:i];
NSLog(@"%@", c);
}*/
// ********************可變最字典*****************
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:1];
[dict setObject:@"zhangsan" forKey:@"name"];
[dict setObject:@"boy" forKey:@"sex"];
[dict setObject:@"18" forKey:@"age"];
NSLog(@"%@", dict);
[dict setObject:@"paoniu" forKey:@"hobby"];
NSLog(@"%@", dict);
NSDictionary * dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan1", @"name1", @"boy1", @"sex1", @"toyke1", @"address", nil];//初始化建立字典
[dict setValuesForKeysWithDictionary:dic1];
[dict removeObjectForKey:@"name1"];//移除索引值為name1的值
NSNumber * num = [NSNumber numberWithInt:24];//用number把數字轉成對象
[dict setObject:num forKey:@"age"];
NSNumber * score = [NSNumber numberWithFloat:88.8];
[dict setObject:score forKey:@"score"];
[dict setObject:[NSNumber numberWithDouble:999.999] forKey:@"fight"];
NSLog(@"%@", dict);
float sco = [[dict objectForKey:@"score"] floatValue];//把對象轉成基礎資料型別 (Elementary Data Type)
NSLog(@"%g", sco);