似て非なる objectForKey, valueForKey, valueForKeyPath
objectForKey
Dictionary, NSDictionary, またそれを継承するクラスの固有のメソッド。 key を引数に与えることで、それに相当する object を返してくれる
valueForKey
KVC (key-value coding) に則って、 key から value を返してくれるメソッド。 Dictionary, NSDictionary にかぎらず、 NSArray などでも使える。
objective-c において気をつけてほしいのは、key にアクセスする際に @""
のように @
を使用すること。
@
をつけないと、
If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key.
のように、super クラスのメソッドを読んでしまい、意図せぬ値にアクセスしてしまう。
参考
https://developer.apple.com/reference/foundation/nsdictionary http://blog.kishikawakatsumi.com/entry/20100319/1268946432
valueForKeyPath
department.manager.name のように、. でアクセスする方法。 例として、json の返り値のように Dictionary の入れ子になっているものにアクセスする際に使える。
参考 https://developer.apple.com/jp/documentation/KeyValueCoding.pdf