为什么我没有获得NSString的输出

3
我有一个名为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
)

为什么会出现这种情况?

1
货币以数组形式出现..您应该将其转换为字符串。 - Saurabh Jain
3个回答

1

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

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

希望这很有帮助。


谢谢。我通过添加这个得到了所需的输出。@jagdish - KAR
这更像是一个“小技巧”,而不是一个真正的解决方案。修改对象的“description”以获取信息并不是一个好的解决方案。 - Larme
通过使用描述,输出与上面相同。所以我不得不找到新的方法 @Larme - KAR
这是因为jsonObject是一个NSArray而不是NSDictionary。问题在于您没有正确理解JSON的结构。 - Larme

1
实际上,CountryInfo_ 是一个数组,而不是 NSDictionary。因此,我们需要传递数组,而不是字典。
Country *country = [[Country alloc] initWithNSDictionary:jsonObject];

尝试传递
Country *country = [[Country alloc] initWithNSDictionary:jsonObject[0]];

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"];

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Koushik
如果countryCode是一个数字,可以尝试使用以下代码:self.code = [NSString stringWithFormat:@" %@ ", [countryInfo_ objectForKey:@"countryCode"]];,将self.code转换为NSString类型。 - Koushik
我在我的问题中添加了两个日志。@Koushik Gounder - KAR
尝试将其转换为NSString,但输出结果相同。@Koushik Gounder - KAR
当您记录一个字典时,应该像这样 Country Info = {},而不是像这样 Country Info = ( {} )。对于一个数组,Nslog的值将像这样:Country Info = ( {},{}) - Koushik
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接