CJADSLKeyPath 是一个易于使用的 Category,用于使用特殊的 keyPath 访问值。
只需将 NSObject+DSLKeyPath.h.h
文件拖放到您的 XCode 项目中。对于全局使用,您可以在项目的 -Prefix.pch
文件中导入 NSObject+DSLKeyPath.h.h
。
首先看看示例项目以获取更多示例。
NSDictionary *object = @{
@"test" : @"foo",
@"nested" : @{
@"name" : @"bar",
@"items" : @[
@{ @"value1" : @"0" , @"value2" : @"foo"},
@{ @"value1" : @"1" , @"value2" : @"foo"},
@{ @"value1" : @"2" , @"value2" : @"foo"},
@{ @"value1" : @"3" , @"value2" : @"foo"},
]
}
};
NSArray *values = [object valueForDSLKeyPath:@"nested.items[*].value1"];
NSLog(@"%@", values);
// prints out: 0, 1, 2, 3
NSString *value = [object valueForDSLKeyPath:@"nested.items[0].value1"];
NSLog(@"%@", value);
// prints out: 0
MIT 许可证