Skip to content

Instantly share code, notes, and snippets.

@katsuhide
Created June 10, 2013 00:18
Show Gist options
  • Save katsuhide/5745835 to your computer and use it in GitHub Desktop.
Save katsuhide/5745835 to your computer and use it in GitHub Desktop.
NSDictionary周り
// 空の可変ディクショナリ生成例
NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
// 値とキー値を指定した生成例
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"hoge" forKey:@"Key"];
// 値とキー値を複数指定する生成例
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"hoge", @"name",
@"999-9999-9999", @"tel",
@"東京都", @"address",nil];
// 値とキー値を、それぞれNSArray型で指定した生成例
NSArray *key = [NSArray arrayWithObjects:@"name", @"tel", @"address", nil];
NSArray *value =
[NSArray arrayWithObjects:@"hoge", @"999-9999-9999", @"東京都", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:value forKeys:key];
// 指定した要素を取得する
id yoso = [dic objectForKey:@"hoge"];
// ディクショナリからキーだけの配列を取得する
NSArray *ar = [dic allKeys];
// ディクショナリから要素だけの配列を取得する
NSArray *ar = [dic allValues];
// 要素を追加
NSArray *ary = [str componentsSeparatedByString:@"="];
NSString *key = [ary objectAtIndex:0];
NSString *value = [ary objectAtIndex:1];
[dic setValue:value forKey:key];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment