如何将变量与 NSArray 内的 NSDictionary 内的对象进行比较?

How to compare a variable to an object inside an NSDictionary that is inside an NSArray?

我需要将 NSString 变量与存储在 NSMutableArray 内的 NSDictionary 中的对象进行比较

我的 NSMutableArray 包含 Manu NSDictionaries,每个 NSDictionaries 都有两个 NSString 对象

在我向数组添加更多整体之前,我想确保它不在其中,但那时我只知道其中一个对象名称

例如

NSDictionary 保存 @"foo" 键 @"bar" 和 @"Jongel" 键 @"fibbel"

现在我想构建一个新的 NSDictionary 并将其插入到数组中,但前提是 NSString @"foo" 不在数组中的字典中

我找不到任何有关如何在数组中寻址 NSDictionary 的文档,我可以找到有关如何使用 NSArray *myArray = dict[0] 在 nsdictionary 中查找数组然后循环遍历的文档,但这就是我需要的我需要走另一条路

如何做到这一点?

这是我正在尝试的方法,但我做不对

+ (void) splitOutCategories:(NSMutableArray *)reveProducts {
    if([NWTillHelper isDebug] == 1) {
        NSLog(@"%s entered with array count = %lu", __PRETTY_FUNCTION__, reveProducts.count);
    }

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

    for (NSDictionary *dictProduct in reveProducts) {
        NSMutableDictionary *dictReveCollections = [[NSMutableDictionary alloc] init];
        NSArray *arrLocales = dictProduct[@"locales"];
        for (NSDictionary *dictLocale in arrLocales) {
            NSArray *arrCategories = dictLocale[@"categories"];
            NSArray *arrImages = dictLocale[@"images"];
            if(arrCategories.count < 1) {
                // Nothing to do
            } else if (arrCategories.count == 1) {
                if(![reveCollections containsObject:arrCategories[0]]) {
                    // Here we need to build the entire dict and insert into the array
                    NSString *fcTitle = arrCategories[0];
                    NSString *fcImageUrl = arrImages[0];
                    [dictReveCollections setObject:fcTitle forKey:@"fcTitle"];
                    [dictReveCollections setObject:fcImageUrl forKey:@"fcImageUrl"];
                    [reveCollections addObject:dictReveCollections];
                }
            } else if (arrCategories.count > 1) {
                if(![reveCollections containsObject:arrCategories[1]]) {
                    NSString *fcTitle = arrCategories[1];
                    NSString *fcImageUrl = nil;
                    if(arrImages.count < 1) {
                        fcImageUrl = @"https://xxxxyyyyy.png";
                    } else {
                        fcImageUrl = arrImages[0];
                    }
                    [dictReveCollections setObject:fcTitle forKey:@"fcTitle"];
                    [dictReveCollections setObject:fcImageUrl forKey:@"fcImageUrl"];
                    [reveCollections addObject:dictReveCollections];
                }
            }
        }
    }
    NSLog(@"reveCollectionsArray = \r\n%@", reveCollections);
    NSLog(@"reveCollectionsArrayCount = %lu", reveCollections.count);
}

Before I add more entires to the Array I want to make sure its not already in there

此处的解决方案取决于具体需要独特的内容。正如您所描述的,听起来只有 NSDictionary key/value 对的值需要是唯一的("foo"、"Jongel")。如果是这种情况,那么您可以通过使用 NSMutableSet 跟踪您已经接受的值来非常轻松解决这个问题。

这样的方法可行:

@interface CollectionExample ()
@property (strong, nonatomic) NSMutableArray *reveCollections;
@property (strong, nonatomic) NSMutableSet *storedValues;
@end

@implementation CollectionExample

- (instancetype)init {
    self = [super init];
    if (self) {
        _storedValues = [NSMutableSet set];
        _reveCollections = [NSMutableArray array];
    }
    return self;
}

- (BOOL)valueAlreadyExists:(NSString *)newValue {
    return [self.storedValues containsObject:newValue];
}

- (void)addNewKeyValuePair:(NSString *)key value:(NSString *)value {
    if ([self valueAlreadyExists:value]) {
        // handle this however you'd like
        return;
    }
    NSDictionary *dict = @{key: value};
    [self.reveCollections addObject:dict];
    [self.storedValues insertObject:value];
}

@end

但是,如果需要唯一的是 key/value ,那么您必须遍历数组 and 遍历每个字典中的每个 key/value 对。这样的事情应该有效:

- (BOOL)valueAlreadyExists:(NSString *)newKey value:(NSString *)newValue {
    BOOL foundPair = NO;
    for (NSDictionary *dict in self.reveCollections) {
        [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([newKey isEqualToString:key] && [newValue isEqualToString:obj]) {
                foundPair = YES;
                *stop = YES;
                return;
            }
        }];
    }
    return foundPair;
}