我需要将NSString变量与存储在NSMutableArray中的NSDictionary中的对象进行比较
我的NSMutableArray包含每个都有两个NSString对象的Manu NSDictionaries
在我向数组添加更多内容之前,我想确保它不在那里,但在那时我只知道其中一个对象名称
例如
NSDictionary持有@" foo"对于关键@" bar"和@" Jongel"关键@" fibbel"
现在我想构建并在数组中插入一个新的NSDictionary,但仅限于NSString @" foo"不在数组内的dict里面
我找不到任何文档如何在数组中处理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);
}
答案 0 :(得分:0)
在向数组中添加更多整体之前,我想确保其尚未存在
这里的解决方案取决于确切需要唯一的内容。如您所述,这听起来像只有NSDictionary键/值对的值需要唯一(“ 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
但是,如果唯一的键/值对 是唯一的,则必须遍历数组,并遍历数组中的每个键/值对每个字典。这样的事情应该起作用:
- (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;
}