我的代码中有这一行,
Country *country = [[Country alloc] initWithNSDictionary:jsonObject];
我在jsonObject中得到一个元素,但为什么国家为nil?
我在项目中有一个国家类文件,我已经通过这个创建了一个对象。
JSON响应:
{"geonames": [{
"continent": "AS",
"capital": "Nuova Delhi",
"languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc",
"geonameId": 1269750,
"south": 6.747139,
"isoAlpha3": "IND",
"north": 35.504223,
"fipsCode": "IN",
"population": "1173108018",
"east": 97.403305,
"isoNumeric": "356",
"areaInSqKm": "3287590.0",
"countryCode": "IN",
"west": 68.186691,
"countryName": "India",
"continentName": "Asia",
"currencyCode": "INR"
}]}
initWithNSDictionary
方法
- (instancetype) initWithNSDictionary:(NSDictionary*)countryInfo_
{
self = [super init];
if(self) {
NSLog(@"Country Info = %@",countryInfo_);
self.code = [countryInfo_ valueForKey:@"countryCode"];
self.name = [countryInfo_ valueForKey:@"countryName"];
self.continent = [countryInfo_ valueForKey:@"continentName"];
self.region = [countryInfo_ valueForKey:@"region"];
self.currencyCode = [countryInfo_ valueForKey:@"currencyCode"];
self.population = [countryInfo_ valueForKey:@"population"];
}
NSLog(@"code %@", code);
return self;
}
由于你没有提供足够的信息,我在假设的基础上给你答复:
在你的Country
类initWithNSDictionary
方法应该定义这样的东西
-(instancetype)initWithNSDictionary : (NSDictionary*)dictionary{
self = [super init];
if (self) {
self.firstName = [dictionary objectForKey:@"firstName"]; //This is just example because you haven't add sufficient information in question
self.lastName = [dictionary objectForKey:@"lastName"];
}
return self;
}
然后您可以创建Country
的实例或对象,就像您在问题中创建的那样,
Country *country = [[Country alloc] initWithNSDictionary:jsonObject];
确保json对象具有您在initWithNSDictionary
方法中使用的键和值。我只是举了一个姓和名的例子。您应该根据您的json对象管理initWithNSDictionary
中的数据。
你的结构是:
-Dictionary
--Array
---Dictionary
这意味着你有一个包含字典数组的顶级字典。
你的错误是,你试图用顶层字典初始化你的对象,它没有针对你提到的值的键,因此它返回nil,而你的对象反过来也有nil值。
简单地说,你传递的是一个只有以下键和值对的字典:
Key: Geonames
Value: An Array
那么在你的init方法中,你假设它包含其他键,而实际上这些其他键只存在于数组中包含的字典中。下面的字典是:
{
"continent": "AS",
"capital": "Nuova Delhi",
"languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc",
"geonameId": 1269750,
"south": 6.747139,
"isoAlpha3": "IND",
"north": 35.504223,
"fipsCode": "IN",
"population": "1173108018",
"east": 97.403305,
"isoNumeric": "356",
"areaInSqKm": "3287590.0",
"countryCode": "IN",
"west": 68.186691,
"countryName": "India",
"continentName": "Asia",
"currencyCode": "INR"
}
因此,您需要从数组中获取上面提到的Dictionary,并使用它来初始化对象。要修复它,您需要修改您的initWithDictionary
方法(或者理想情况下,您调用initWithdictionary方法的方式,但现在让我们忘记它)。
- (instancetype) initWithNSDictionary:(NSDictionary*)countryInfo_
{
self = [super init];
if(self) {
NSLog(@"Country Info = %@",countryInfo_);
NSArray *internalArray = countryInfo_[@"geonames"]; //Now you got your array of dictionaries.
if([internalArray count]>0){
NSDictionary *internalDictionary = internalArray[0]; //Assuming there will always be only one dictionary in that array but if there are more, thats your design problem. You got your internal dictionary now
self.code = internalDictionary[@"countryCode"];
self.name = internalDictionary[@"countryName"];
self.continent = internalDictionary[@"continentName"];
self.region = internalDictionary[@"region"];
self.currencyCode = internalDictionary[@"currencyCode"];
self.population = internalDictionary[@"population"];
}
}
NSLog(@"code %@", code);
return self;
}
注意:这是假设在你的字典数组中总是有一个字典,或者至少只有你想要使用的第一个字典。这意味着有一些信息,你对我们隐瞒或一个真正大的设计缺陷在你的结束。
使用
Country *country = [[Country alloc] initWithNSDictionary:[jsonObject objectForKey:@"your key"]];
//[[NSDictionary alloc] initWithObjectsAndKeys:
//jsonObject ?: [NSNull null], @"jsonObject",nil]
将代码行改为
Country *country = [[Country alloc] initWithNSDictionary:(NSDictionary *)jsonObject];
和国家使用
中的访问变量[countryCode setText:[country.code description]];