首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法来创建和更新对象的NSMutableDictionary数组

方法来创建和更新对象的NSMutableDictionary数组
EN

Stack Overflow用户
提问于 2018-02-14 09:09:32
回答 1查看 38关注 0票数 0

我一直在开发一个Objective-C iOS应用程序。它从我们的JSON API中提取了一些数据,我一直在尝试通过cherrypicking对象来格式化一些我想要退出的对象。

我写了一个静态版本,可以很好地使用我的UITableView,但是我需要写一个方法来更好地处理事情。

代码语言:javascript
复制
self.propertyData = @{@"Address": @[ @{ @"Street": _tempPropertyData[@"address"][@"line1"] },
                                   @{ @"City": _tempPropertyData[@"address"][@"line2"] },
                                   @{ @"County": _tempPropertyData[@"area"][@"countrysecsubd"] },
                                   @{ @"Subdivision": _tempPropertyData[@"area"][@"subdname"] },
                                   @{ @"Municipality": _tempPropertyData[@"area"][@"munname"] },
                                   @{ @"Tax Area": _tempPropertyData[@"area"][@"taxcodearea"] }],

                    @"Mortgage": @[ @{ @"Lender": _tempPropertyData[@"mortgage"][@"lender"][@"lastname"] },
                                    @{ @"City": _tempPropertyData[@"mortgage"][@"lender"][@"city"] },
                                    @{ @"State": _tempPropertyData[@"mortgage"][@"lender"][@"state"] },
                                    @{ @"Zip code": _tempPropertyData[@"mortgage"][@"lender"][@"zip"] },
                                    @{ @"Loan Amount": _tempPropertyData[@"mortgage"][@"amount"] },
                                    @{ @"Loan Date": _tempPropertyData[@"mortgage"][@"date"] },
                                    @{ @"Interest Rate": _tempPropertyData[@"mortgage"][@"interestrate"] },
                                    @{ @"Loan Type": _tempPropertyData[@"mortgage"][@"loantypecode"] },
                                    @{ @"Deed Type": _tempPropertyData[@"mortgage"][@"deedtype"] },
                                    @{ @"Term": _tempPropertyData[@"mortgage"][@"term"] },
                                    @{ @"Maturity Date": _tempPropertyData[@"mortgage"][@"duedate"] }
                                    ]};

我想创建一个函数来创建每个部分,方法是调用

代码语言:javascript
复制
[self addJsonData:@"Address" Key:@"Street" Value:_tempPropertyData[@"address"][@"line1"]];

无论我做了什么,我似乎都无法与之匹敌,无法创造,无法达到我的目标。我只是因为某些原因不能理解它。self.propertyData是一个NSMutableDictionary,我的理解是我已经创建了一个带有对象数组的字典,但我猜不是。

代码语言:javascript
复制
-(void)addJsonData:(NSString*)parent Key:(NSString*)key Value:(NSString*)value {

    if([self.propertyData objectForKey:parent]) {
        // Yes Dictionary with that parent
        NSDictionary *aDic= [NSDictionary dictionaryWithObjectsAndKeys:value, key, nil];
        NSMutableArray *innerDictArray = self.propertyData[parent];
        [innerDictArray addObject:aDic];
        [self.propertyData setObject:innerDictArray forKey:parent];

    } else {
        // No Dictionary with that parent
        NSDictionary *aDic= [NSDictionary dictionaryWithObjectsAndKeys:value, key, nil];
        NSMutableArray *innerDictArray = [NSMutableArray arrayWithObjects:aDic,nil];
        [self.propertyData setObject:innerDictArray forKey:parent];
    }
//    NSLog(@"%@", self.propertyData);
}

如何创建和更新与我试图从静态版本创建的字典的格式相匹配的字典?

EN

回答 1

Stack Overflow用户

发布于 2018-02-14 14:02:11

答案是我忘了初始化NSMutableDictionary。这个方法本身工作得很好。

代码语言:javascript
复制
self.propertyData = [[NSMutableDictionary alloc] init];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48778190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档