【发布时间】:2014-08-13 17:39:57
【问题描述】:
在对 Instagram API 进行网络调用后,我返回了一个带有以下键/值结构的 responseDictionary NSDictionary 委托:
{
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 键通常有一个 NSString 值,其中包含anonymousUser(对于没有设置任何个人资料图片的用户)。
我希望从我的 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 开发者的反馈。
【问题讨论】:
-
“但这不起作用”不是对问题的描述。
-
为什么不使用 KVC 来查找和删除您的密钥之类的数据。字典.valueForKey(@"data.Anonymous") ?然后尝试使用相同的路径进行删除
标签: ios objective-c nsstring nsmutabledictionary