2016-08-26 170 views
3

的输出我有一个方法initWithNSDictionary:为什么我没有得到的NSString

- (id) 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 *country = [[Country alloc] initWithNSDictionary:jsonObject]; 

之后,我在标签印刷这个值:

countryCode.text = [NSString stringWithFormat:@"Country Code = %@", country.code]; 

当我登录到控制台时,我得到:

NSLog(@"Currency Code = %@", currencyCode.text); 

Currency Code = Currency Code = (
    BRL 
) 

而且在标签实际设备的屏幕上,我只得到:

Country Name = (

我希望输出这样的:

Currency Code = BRL 

CountryInfo_:

Country Info = (
     { 
     areaInSqKm = "8511965.0"; 
     capital = "Bras\U00edlia"; 
     continent = SA; 
     continentName = "America meridionale"; 
     countryCode = BR; 
     countryName = Brasile; 
     currencyCode = BRL; 
     east = "-32.392998"; 
     fipsCode = BR; 
     geonameId = 3469034; 
     isoAlpha3 = BRA; 
     isoNumeric = 076; 
     languages = "pt-BR,es,en,fr"; 
     north = "5.264877"; 
     population = 201103330; 
     south = "-33.750706"; 
     west = "-73.985535"; 
    } 
) 

和国家代码打印:

code (
    BR 
) 

为什么会发生这种情况?

+1

货币进来阵列的形式..你应该改变成字符串 –

回答

1

我认为CURRENCYCODE有NSString与换行符
试试这个

currencyCode.text = [[[currencyCode.text stringByReplacingOccurrencesOfString:@"\n" withString:@""] stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];` 

希望这是有帮助的。

+0

谢谢。我通过添加这个来获得所需的输出。 @jagdish – KAR

+0

这更多的是一个真正的解决方案“小黑客”。修改对象的-'description'来获取信息不是一个好的解决方案。使用描述 – Larme

+0

,输出与上面相同。所以我不得不寻找新的方法@Larme – KAR

0

尝试这样的事情

self.code = [[countryInfo_ objectAtIndex:0] objectForKey:@"countryCode"]; 
self.name = [[countryInfo_ objectAtIndex:0] objectForKey:@"countryName"]; 
self.continent = [[countryInfo_ objectAtIndex:0] objectForKey:@"continentName"]; 
self.region = [[countryInfo_ objectAtIndex:0] objectForKey:@"region"]; 
self.currencyCode = [[countryInfo_ objectAtIndex:0] objectForKey:@"currencyCode"]; 
self.population = [[countryInfo_ objectAtIndex:0] objectForKey:@"population"]; 
+0

这会导致应用程序崩溃。 @koushik Gounder – KAR

+0

那么这个'NSLog(@“Country Info =%@”,countryInfo _);''和'NSLog(@“code%@”,code);'print – Koushik

+0

也试试这个'self.code = [ NSString stringWithFormat:@“%@”,[countryInfo_ objectForKey:@“countryCode”]];'如果countryCode是一个数字将self.code作为NSString – Koushik

1

实际CountryInfo_是一个数组,而不是一个NSDictionary。因此,而不是通过

Country *country = [[Country alloc] initWithNSDictionary:jsonObject]; 

尝试通过

Country *country = [[Country alloc] initWithNSDictionary:jsonObject[0]]; 
相关问题