我在這行代碼,iOS-初始化自定義類使用JSON返回nil
Country *country = [[Country alloc] initWithNSDictionary:jsonObject];
我得到一個元素的JSONObject但爲什麼國家在零?
我在項目中有國家級文件,我已經通過它做了一個對象。
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;
}
在問題中增加'initWithNSDictionary'方法。第二件事改變你的問題的標題。它應該與您的查詢或問題有關! – Lion
更改標題。你有解決方案嗎? @KetanParmar – KAR
你的'initWithNSDictionary' mehod做什麼?據說,任何人都可以給答案! – Lion