KAR KAR - 3 months ago 6
iOS Question

Why I am not Getting output of NSString

I have one method initWithNSDictionary :

- (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;
}


I am accessing this method by :

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


After that I am printing this value in label by :

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


When I am logging it in console I get :

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

Currency Code = Currency Code = (
BRL
)


And on actual device screen in label I am only getting :

Country Name = (


I want output like :

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";
}
)


And Country code prints :

code (
BR
)


why this is happening?

Answer

I think currencyCode has a NSString with newline character
Try this

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

hope this is helpful.