我一直在开发一个Objective-C iOS应用程序。它从我们的JSON API中提取了一些数据,我一直在尝试通过cherrypicking对象来格式化一些我想要退出的对象。
我写了一个静态版本,可以很好地使用我的UITableView,但是我需要写一个方法来更好地处理事情。
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"] }
]};我想创建一个函数来创建每个部分,方法是调用
[self addJsonData:@"Address" Key:@"Street" Value:_tempPropertyData[@"address"][@"line1"]];无论我做了什么,我似乎都无法与之匹敌,无法创造,无法达到我的目标。我只是因为某些原因不能理解它。self.propertyData是一个NSMutableDictionary,我的理解是我已经创建了一个带有对象数组的字典,但我猜不是。
-(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);
}如何创建和更新与我试图从静态版本创建的字典的格式相匹配的字典?
发布于 2018-02-14 14:02:11
答案是我忘了初始化NSMutableDictionary。这个方法本身工作得很好。
self.propertyData = [[NSMutableDictionary alloc] init];https://stackoverflow.com/questions/48778190
复制相似问题