ios - 为什么我没有得到 NSString 的输出

标签 ios objective-c xcode

我有一个方法 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;
}

我正在通过以下方式访问此方法:

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

国家信息_:

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
)

为什么会这样?

最佳答案

我认为 currencyCode 有一个带换行符的 NSString
试试这个

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

希望对您有所帮助。

关于ios - 为什么我没有得到 NSString 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160256/

相关文章:

ios - Iphone 传递对象(导航栏上的按钮返回)

objective-c - 文档和 Nib 文件 : What does apple mean by "Document"?

objective-c - ARC 禁止发送 'retain' 问题的显式消息

ios - 为什么更改 UIImageView 内容模式会影响其在 Xcode6 中的自动布局?

ios - 从小部件(今日扩展)打开主机应用程序时未调用 AppDelegate 函数

ios - NSMutableArray count 说只有一个元素在里面,其实它们更多

ios - IA5STRING ASN.1编码格式: in-app purchase receipt validation and decoding

ios - 向 UITableView 插入行时的行为

ios - GIDSignInButton 为空

ios - UIWebView loadHtmlString 无法在设备上运行