NDictionary
NSDictionary *dic = @{@"手":@"手袋", @"足":@"靴下", @"頭":@"帽子"};
NSLog(@"%@", dic[@"足"]);
// 追加.
NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithCapacity:1];
mdic[@"first"] = @"1st";
mdic[@"second"] = @"2nd";
mdic[@"third"] = @"3rd";
NSLog(@"%@", mdic);
// 変更.
mdic[@"second"] = @"SECOND";
mdic[@"fourth"] = @"FOURTH";
NSLog(@"%@", mdic);
// 削除.
[mdic removeObjectForKey:@"fourth"];
NSLog(@"%@", mdic);
[mdic removeAllObjects];
NSLog(@"%@", mdic);
NSLog(@"%d", mdic.count);
// ループ.
for (NSString *key in dic) {
id value = dic[key];
NSLog(@"%@ => %@", key, value);
}
// キーをソートしてループ.
dic = @{@"3":@"GHI", @"1":@"ABC", @"2":@"DEF"};
NSArray *keys = [dic keysSortedByValueUsingSelector:@selector(compare:)];
for (NSString *key in keys) {
NSString *value = dic[key];
NSLog(@"%@ => %@", key, value);
}
// ブロック使用.
[dic enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
NSLog(@"%@ => %@", key, obj);
}];