2016-08-26 167 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]]; 
相關問題