【问题标题】:how to fetch Object from NSDictionary using ObjectForKey如何使用 ObjectForKey 从 NSDictionary 获取对象
【发布时间】:2013-03-07 18:27:47
【问题描述】:

我有 3 个NSMutableArrays:_parssedArrayOfName_parssedArrayOfbirthdate_CopyOFSelectedFaceBookFriends

_parssedArrayOfName 中有很多类似下面的名字

2013-03-07 13:15:40.003 birthdate reminder 2[1102:15803] asdas{
        (
        "Nishith Shah",
        "Rupal Shah",
        "Viral Nagori",
        "Malay Shah",
        "Heather Joy",
        "Jatin Patel",
        "Bhushan H Trivedi",
        "Umang Patel",
        "Harshal Arolkar",
        "Nida Shaikh",
        "Yuriko Ramirez",
        "Aysu Can",
        "Bhargav Khatana",
        "Rahul Vador",
        "Viral Dave",

_parssedArrayOfbirthdate 中有如下的生日

13-03-07 13:15:29.833 birthdate reminder 2[1102:15803]  this is what im here(
        (
        "<null>",
        "07/27",
        "06/11/1980",
        "08/22/1990",
        "<null>",
        "03/17/1985",
        "<null>",
        "10/17/1989",
        "<null>",
        "07/20",
        "12/08",
        "04/14/1992",
        "10/16",
        "<null>",

_CopyOFSelectedFaceBookFriends 是用户选择的朋友列表,只是说 Anand Kapadiya

我将 NSDictionary 中的所有姓名和出生率添加为键,并将生日作为值 然后我想使用ObjectForKey从这个字典中获取anand kapadiya生日:

但我得到空值我的代码如下请帮助我

注意:不同数组中的 Birthdate 和 Names 的数量相同,并且选择的数组值始终在 names 数组中

注意2:这可能是这个问题的原因吗?在选择器名称中没有“”,而在名称数组中所有名称都带有“”

注意3:你可以看到我的生日数组包含空值,这可能是问题吗?

NSArray *objArr = [[NSArray alloc] initWithArray:_parssedArrayOfbirthdate];
NSArray *keyArr =[[NSArray alloc] initWithArray:_parssedArrayOfName];
NSArray *selector =[[NSArray alloc] initWithArray:_CopyOFSelectedFaceBookFriends];     NSDictionary *dic = [[[NSDictionary alloc] autorelease] initWithObjects:objArr forKeys:keyArr];
NSLog(@"asdas%@",dic.description);
 NSMutableArray *matches = [NSMutableArray array];
 for (NSString *key in selector) {
 NSLog(@" see it%@",key);
  NSMutableArray *array1 = [dic objectForKey:key];
        NSLog(@" matched%@",array1);
        [matches addObject:array1];
        NSLog(@" matched%@",matches);

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    你的问题和这段代码很难理解。但是,我认为问题在于键字符串并不总是代表 dic 字典中的有效键。如果要检查选择器数组的哪些元素是有效键,则需要先使用 if 语句检查它们是否存在。

    您的代码中还有自动释放功能,但所有内存管理都应由 ARC 处理。

    试试这个:

    NSArray *birthdates = [[NSArray alloc] initWithArray:_parssedArrayOfbirthdate];
    NSArray *names =[[NSArray alloc] initWithArray:_parssedArrayOfName];
    NSArray *fbFriends =[[NSArray alloc] initWithArray:_CopyOFSelectedFaceBookFriends];    
    NSMutableArray *matches = [[NSMutableArray alloc] initWithCapacity:1];    
    
    NSDictionary *birthdayDictionary = 
    [[NSDictionary alloc] initWithObjects:birthdates forKeys:names];
    
    NSLog(@"asdas%@",birthdayDictionary.description);
    NSStrging *birthday;
    for (NSString *friend in fbFriends) {
        NSLog(@"see it %@",friend);
        if ([birthdayDictionary objectForKey:friend])
        {
            NSLog(@"matched %@",birthday);
            [matches addObject:birthday];
            NSLog(@"matched %@",matches);
        }
    }
    

    【讨论】:

    • 非常感谢您的回答,我会试试这个,如果问题仍然存在,我会回到这里
    • 先生的钥匙总是存在的,出生日期的数量和名字的数量总是相同的
    • 可能是这个问题的原因吗?在选择器名称中没有“”,而在名称数组中所有名称都带有“”
    • 人们我知道了,我的整个数组都在索引 0 处,愚蠢的我 :) 感谢大家
    • 我的回答说明了这一点,因为我没有 array1,而是将生日作为 NSString。是这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多