NSDictionary枚举第二级

时间:2015-09-11 12:18:13

标签: objective-c nested nsdictionary

考虑以下NSDictionary: enter image description here

展开后,看起来像这样:enter image description here

我想创建每个“功能”键的NSObject,“几何”和“类型”作为属性,但我无法理解

[myDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){

的方法。是否有办法通过给出一个关键名称来指示入口级别?

我做了很多搜索,但我找到的东西似乎都不合适。我希望你对我有耐心。

2 个答案:

答案 0 :(得分:2)

以下是我创建要素对象的方法:

@interface Feature : NSObject
@property (nonatomic, strong) NSString *geometry; //case is important 
@property (nonatomic, strong) NSString *type; //case is important 
@end

然后我会使用KVC创建它。所以我需要实现方法setValue:forUndefinedKey以避免崩溃:

@implementation Feature

- (void) setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"key %@ does not exist", key);
}

@end

然后我假设你想要一个特征对象数组:

NSMutableArray *featureArray = [[NSMutableArray alloc] init];

并填写此数组:

NSMutableArray *featureArray = [[NSMutableArray alloc] init];
[dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    NSLog(@"key: %@", key);
    if ([obj isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)obj;
        for (NSDictionary *childDic in arr) {
            Feature *f = [[Feature alloc] init];
            [childDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull childKey, id  _Nonnull childObj, BOOL * _Nonnull childStop) {
                [f setValue:childObj forKey:childKey];
            }];
            [featureArray addObject:f];
        }
    }
}];

希望这有帮助。

答案 1 :(得分:0)

如果你知道存储的密钥,你可以这样做:

`dispose = function() {
if (settings.debug) {
    console.info('place.videojs_element.dispose()');
} /*Target the Player Element*/
var player = videojs(settings.element.id + '_player'); /*Pause Video*/
player.pause(); /*Wait for third party scripts to stop listening!!! <-- Important*/
setTimeout(function() { /*Dispose of the player*/
    player.dispose();
    /*I have a new video element waiting to be placed ( this code is proprietary )*/
    var epi = new EPI();
    epi.place.videojs_element(settings, data); /*Wait time 600ms*/
}, 600); /*Destroy the old video element <--- Important */
$('#' + settings.element.id).empty();