Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

后端 未结 5 2059
温柔的废话
温柔的废话 2020-12-15 19:04

I want to use valueForKeyPath on my NSDictionary, but the problem is that one of the keys is a string that starts with the @ symbol. I have no cont

5条回答
  •  轮回少年
    2020-12-15 19:48

    Just to update this old question a little...

    The reason that these:

    [dict valueForKeyPath:@"key1.@specialKey.key3"]
    [dict valueForKeyPath:@"key1.@@specialKey.key3"]
    

    ...fail is that any "@" symbols in a key path are interpreted as being collection's operators as with:

    [dict valueForKeyPath:@"key1.@sum.key3"] // returns the sum of all 'key3' values
    [dict valueForKeyPath:@"key1.@avg.key3"] // returns the average of all 'key3' values
    

    The nested key calls:

    [[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]
    

    ... work because a single key is not processed as a key path.

提交回复
热议问题