0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSDictionary/NSMutableDictionary関連のメモ

Last updated at Posted at 2014-01-25
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);
    }];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?