Keypath for first element in embedded NSArray

后端 未结 2 1216
渐次进展
渐次进展 2020-12-16 01:52

This example is contrived, but it shows my point.

So, if I have an object graph like the following:

{
sex = male;
uid = 637650940;
work = ({
    empl         


        
2条回答
  •  囚心锁ツ
    2020-12-16 01:59

    @PauldeLange - Your answer and links were helpful.
    The following simpler version works too (at least as of Xcode 6)

    id name = [work valueForKeyPath: @"employer.name.@firstObject”];
    

    In the above 'firstObject' refers to the predefined method on NSArray. If the second object is needed, you can define the following:

    @implementation NSArray (CustomKVOOperators)
    
    - (id) secondObject {
        return [self count] >=2 ? self[1] : nil;
    }
    @end
    

    And use:

     id name = [work valueForKeyPath: @"employer.name.@secondObject”];
    

提交回复
热议问题