Objective-Cでデータ構造を表す場合、主に、NSArray、NSSet、NSDictionaryとその派生クラスを使う。変わり種として、NSIndexPath, NSIndexSet, NSMutableIndexSetなどのクラスもあるが、本記事では前述の3クラスとサブクラスについて解説する。
NSArray:配列
NSArrayは、複数のオブジェクトを、「順序を維持して」格納する。オブジェクトにはNSUInteger(unsigned int)型のインデックスが振られ、「インデックス指定でオブジェクトを操作」する。
NSArray
使用例
NSArrayは不変なので、生成時に中身を指定する必要がある。
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one",@"two", @"three", nil];
//中身を確認
NSLog(@"%@", array);
//要素を1つずつ取り出す
NSLog(@"%@", [array objectAtIndex:0]);
NSLog(@"%@", [array objectAtIndex:1]);
NSLog(@"%@", [array objectAtIndex:2]);
NSLog(@"%@", [array objectAtIndex:3]);
//要素数をカウント
NSLog(@"count:%d", [array count]);
//for inを使ってループ
for(NSString * string in array){
NSLog(@"forin: %@", string);
}
実行結果
zero,
one,
two,
three
)
2012-03-11 12:06:26.171 NSArray[13477:f803] zero
2012-03-11 12:06:26.171 NSArray[13477:f803] one
2012-03-11 12:06:26.171 NSArray[13477:f803] two
2012-03-11 12:06:26.172 NSArray[13477:f803] three
2012-03-11 12:06:26.172 NSArray[13477:f803] count:4
2012-03-11 12:06:26.173 NSArray[13477:f803] forin: zero
2012-03-11 12:06:26.173 NSArray[13477:f803] forin: one
2012-03-11 12:06:26.174 NSArray[13477:f803] forin: two
2012-03-11 12:06:26.174 NSArray[13477:f803] forin: three
NSMutableArray
NSMutaleArrayは、NSArrayに要素の追加や削除機能を追加したクラス。NSArrayを継承したクラスなので、引数としてNSArrayクラスが指定されている場合も、NSMutableArrayクラスのオブジェクトを指定できる。
使用例
空の状態で初期化し、後からオブジェクトを追加できます。
NSMutableArray *array = [[NSMutableArray alloc] init];
//中身を確認➞空です。
NSLog(@"%@", array);
//文字列を追加
[array addObject:@"zero"];
[array addObject:@"one"];
[array addObject:@"two"];
[array addObject:@"three"];
NSLog(@"%@", array);
//0番目の要素を除去
[array removeObjectAtIndex:0];
NSLog(@"%@", array);
実行結果
)
2012-03-11 12:13:07.740 NSMutableArray[13563:f803] (
zero,
one,
two,
three
)
2012-03-11 12:13:07.740 NSMutableArray[13563:f803] (
one,
two,
three
)
NSSet:集合
NSSetは、複数のオブジェクトを、「順序を維持せず」格納する。NSSetは同一オブジェクトを複数所持できない。※NSSetの派生クラスであるNSCountedSetは同一オブジェクトを複数所持できる
NSSet
NSSetは不変なので、生成時に中身を設定します。
使用例
実行結果
NSSetは順序が維持されません。
one,
three,
five,
two,
four
)}
2012-03-11 12:19:50.852 NSSet[13668:f803] one
2012-03-11 12:19:50.856 NSSet[13668:f803] three
2012-03-11 12:19:50.856 NSSet[13668:f803] five
2012-03-11 12:19:50.857 NSSet[13668:f803] two
2012-03-11 12:19:50.857 NSSet[13668:f803] four
NSMutableSet
NSMutableSetは、Setの生成後に要素を追加削除できます。
NSMutableSet * mutableSet = [set mutableCopy];
NSLog(@"%@", mutableSet);
[mutableSet addObject:@"four"];//既に同じ要素が存在するので無視される
[mutableSet addObject:@"five"];//これは追加できる
for(NSString * string in mutableSet){
NSLog(@"%@", string);
}
実行結果
同じ要素は2つ以上追加しようとしても無視されます。
one,
three,
two,
four
)}
2012-03-11 12:27:28.144 NSMutableSet[13780:f803] one
2012-03-11 12:27:28.145 NSMutableSet[13780:f803] three
2012-03-11 12:27:28.146 NSMutableSet[13780:f803] five
2012-03-11 12:27:28.146 NSMutableSet[13780:f803] two
2012-03-11 12:27:28.147 NSMutableSet[13780:f803] four
NSCountedSet
NSCountedSetは、同じ要素を複数回追加できます。
サンプルプログラム
NSLog(@"%@", countedSet);
[countedSet addObject:@"four"];
[countedSet addObject:@"five"];
for(NSString * string in countedSet){
NSLog(@"%@ %d", string, [countedSet countForObject:string]);
}
NSLog(@"%@", countedSet);
実行結果
2012-03-11 12:37:44.282 NSContedSet[13971:f803] one 1
2012-03-11 12:37:44.282 NSContedSet[13971:f803] three 1
2012-03-11 12:37:44.283 NSContedSet[13971:f803] five 1
2012-03-11 12:37:44.283 NSContedSet[13971:f803] two 1
2012-03-11 12:37:44.284 NSContedSet[13971:f803] four 2
2012-03-11 12:37:44.284 NSContedSet[13971:f803] <NSCountedSet: 0x6a54e60> (one [1], three [1], five [1], two [1], four [2])
NSDictionary:ハッシュマップ
NSDictionaryは、複数のオブジェクトを「順序を維持せず」格納する。オブジェクトにはNSString型のキーが割り当てられ、「キー指定でオブジェクトを操作」する。いわゆるハッシュマップやキーバーリュー型のデータ構造である。
NSDictionary
キーと値をそれぞれNSArrayで指定してNSDictionaryを初期化します。
サンプルプログラム
dictionaryWithObjects:[NSArray arrayWithObjects:@"一", @"二", @"三", nil]
forKeys:[NSArray arrayWithObjects:@"one", @"two", @"three", nil]];
NSLog(@"%@", dictionary);
for(NSString * key in dictionary){
NSLog(@"%@ => %@", key, [dictionary objectForKey:key]);
}
実行結果
NSDictionaryは値の順序を保持しません。
one = "\U4e00";
three = "\U4e09";
two = "\U4e8c";
}
2012-03-11 12:45:15.722 NSDictionary[14153:f803] one => 一
2012-03-11 12:45:15.722 NSDictionary[14153:f803] two => 二
2012-03-11 12:45:15.723 NSDictionary[14153:f803] three => 三
NSMutableDictionary
空のNSMutableDictionaryを生成し、オブジェクトの追加と削除を実行します。
サンプルプログラム
//オブジェクトを3つ追加
[dictionary setValue:@"first" forKey:@"one"];
[dictionary setValue:@"second" forKey:@"two"];
[dictionary setValue:@"third" forKey:@"three"];
NSLog(@"%@", dictionary);
//オブジェクトにアクセス
NSLog(@"%@", [dictionary valueForKey:@"one"]);
//オブジェクトを除去
[dictionary removeObjectForKey:@"one"];
NSLog(@"%@", dictionary);
実行結果
one = first;
three = third;
two = second;
}
2012-03-11 12:51:45.764 NSMutableDictionary[14220:f803] first
2012-03-11 12:51:45.765 NSMutableDictionary[14220:f803] {
three = third;
two = second;
}
まとめ
Objective-Cの代表的なコレクション型には、NSArray、NSDictionary、NSSetとその派生クラスがある。生成後に要素を増減させる必要がある場合はMutableクラスを使えばよい。これらのクラスを活用することで効率良くソフトウェア開発を実現できる。