objective-cのいろいろな反復処理
DESCRIPTION
2014.3.12 iOS_LTTRANSCRIPT
Objective-Cの いろいろな反復処理
2014.3.12 @iOS_LT
•小川航佑 Kosuke Ogawa • @koogawa • 趣味と仕事の両方でiOSアプリを開発
コレクション (NSArray, NSDictionary, NSSet) の反復処理にはいろいろある
例えば
forループ
Array
Array
object = aobject = bobject = c
Dictionary
Dictionary
key = key2, value = val2key = key1, value = val1key = key3, value = val3
•インデックスにアクセスしやすい •複雑になりがち •一時変数を作る必要がある(Dictionaryの場合)
高速反復処理
Array
Array
object = aobject = bobject = c
Array
object = aobject = bobject = c
object = cobject = bobject = a
Dictionary
Dictionary
key = key2, value = val2key = key1, value = val1key = key3, value = val3
•シンプルに書ける •インデックスにアクセスできない(Array)
•値も必要な場合は余分なステップが必要になる(Dictionary)
ブロックベースの 反復処理
Array
Array
object = a, idx = 0object = b, idx = 1object = c, idx = 2
Array
Array
object = a, idx = 0
Dictionary
Dictionary
key = key2, object = val2key = key1, object = val1key = key3, object = val3
NSEnumerationReverse
NSEnumerationReverse
object = c, idx = 2object = b, idx = 1object = a, idx = 0
NSEnumerationReverse
NSEnumerationReverse
key = key2, object = val2key = key1, object = val1key = key3, object = val3
•インデックスにアクセスできる(Array)
•キーと値が一緒に手に入る(Dictionary)
•字数はやや増える
• 項目48「forループではなく、ブロックの反復処理を使う」を参考
• ¥3,200+税 • おすすめ
Effective Objective-c 2.0
ありがとう ございました