gpt4 book ai didi

ios - 根据 NSString 值的匹配从 NSMutableDictionary 中删除条目

转载 作者:行者123 更新时间:2023-12-01 18:58:01 24 4
gpt4 key购买 nike

在对 Instagram API 进行网络调用后,我得到了一个带有以下键/值结构的 responseDictionary NSDictionary 委托(delegate):

{
data = (
{
bio = "Los Angeles/Orange County Realtor\U00ae \n\U6d1b\U6749\U77f6\U623f\U5730\U4ea7\U7ecf\U7eaa\U4eba\nCall/Text/WhatsApp: (310) 717-1321\nEmail: Jxxxcom\nWeChat (\U5fae\U4fe1): xx";
"full_name" = "xx yy (\U7530\U4f73\U6dfc) Rx Realty";
id = 25354408;
"profile_picture" = "http://scontent-a.cdninstagram.com/hphotos-xpa1/outbound-distillery/t0.0-20/OBPTH/profiles/profile_xxx_75sq_1391378894.jpg";
username = jxxi;
website = "http://www.Jxghty.com";
},

profile_picture 键通常有一个包含 anonymousUser 的 NSString 值(对于没有设置任何个人资料图片的用户)。

我希望从我的 responseDictionary 中删除这些条目,如下所示:
        //Create mutable copy of IG responseDictionary
NSMutableDictionary *dictCleanAvatars = [responseDictionary mutableCopy];
NSLog(@"Log dictCleanAvatars after mutableCopy IG response: %@", dictCleanAvatars);

NSArray *keys = [dictCleanAvatars allKeys]; //get all the keys
NSUInteger k2 = [dictCleanAvatars count];
NSLog(@"k2 in dictCleanAvatars before cleanup is: %lu", (unsigned long)k2);

for (int i = 0; i<k2; i++)
{

if ([[dictCleanAvatars objectForKey:[keys objectAtIndex:i]] isKindOfClass:[NSString class]])
{
//if its an NSString - don't want an exception if its another type of object
NSLog(@"Yes, objectAtIndex:i us Kind ofClass NSString for i = %d", i);

if ([[dictCleanAvatars objectForKey:[keys objectAtIndex:i]] rangeOfString:@"anonymousUser"].location != NSNotFound)
{
NSLog(@"Yes, anonymousUser identified in objectAtIndex:i for i = %d", i);
//if object has the key word im looking for
[dictCleanAvatars removeObjectForKey:[keys objectAtIndex:i]]; //remove the key
NSLog(@"That's dictCleanAvatars after loop %d: %@", i, dictCleanAvatars);
}
}
}

但这不起作用。

会重视更多经验丰富的 iOS 开发人员的反馈。

最佳答案

如果您尝试构建一个包含 data 中所有内容的数组键的数组,但省略了 profile_picture 的字典包含字符串“AnonymousUser”,您可以使用 NSPredicate :

NSArray *dataArray = responseDictionary[@"data"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not (profile_picture contains 'AnonymousUser')"];
NSArray *filteredArray = [dataArray filteredArrayUsingPredicate:predicate];

或者您可以使用 predicateWithBlock :
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject[@"profile_picture"] rangeOfString:@"AnonymousUser"].location == NSNotFound;
}];

顺便说一句,如果您已经有一个可变数组,您还可以使用 filterUsingPredicate 从中删除条目,使用上述谓词:
NSMutableArray *mutableDataArray = [responseDictionary[@"data"] mutableCopy];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not (profile_picture contains 'AnonymousUser')"];
[mutableDataArray filterUsingPredicate:predicate];

另一方面,如果您不想从字典数组中删除整个字典,而是想简单地删除 profile_picture 的出现。对于存在“AnonymousUser”的情况,您要确保不仅数组是可变的,而且其组成字典也是可变的。

最简单的方法是指定 NSJSONReadingMutableContainers解析 JSON 时的选项。然后你可以遍历 NSMutableDictionary条目,删除 profile_picture带有 profile_picture 的条目其中有“AnonymousUser”:
NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSMutableArray *mutableDataArray = responseDictionary[@"data"];

for (NSMutableDictionary *dictionary in mutableDataArray) {
NSString *profilePicture = dictionary[@"profile_picture"];
if ([profilePicture rangeOfString:@"AnonymousUser"].location != NSNotFound) {
[dictionary removeObjectForKey:@"profile_picture"];
}
}

但是,如果您不能指定 NSJSONReadingMutableContainers当您解析 JSON 并使用不可变集合时,您需要创建一个可变副本。不幸的是,一个简单的 mutableCopy数组的值不会使成员字典本身可变,但您可以使用 Core Foundation 调用 CFPropertyListCreateDeepCopy使用可变条目创建一个可变数组,然后您可以对其进行修改:
NSMutableArray *mutableDataArray = CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)responseDictionary[@"data"], kCFPropertyListMutableContainers));

那么就可以使用上面的 for循环,遍历这个数组的字典条目,删除有问题的 profile_picture条目。

关于ios - 根据 NSString 值的匹配从 NSMutableDictionary 中删除条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com