调整字符串以显示单个或复数单词

时间:2014-08-28 00:42:17

标签: ios objective-c nsmutablearray

我正在显示图片所拥有的数量。我想避免典型的'1喜欢',但没有成功。这是我目前的代码。问题是,如果我使用此代码,应用程序崩溃,虽然它对我来说很好。数据源是NSMutableArray,只包含数字。

if ([[mainDelegate.imagesLikes objectAtIndex:indexPath.section] isEqualToString:@"1"]) {
    likesLabel.text = [NSString stringWithFormat:@"%@ %@", [mainDelegate.imagesLikes objectAtIndex:indexPath.section], @"Like"];
} else {
    likesLabel.text = [NSString stringWithFormat:@"%@ %@", [mainDelegate.imagesLikes objectAtIndex:indexPath.section], @"Likes"];
}

崩溃日志:

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000000023
2014-08-28 02:27:12.146 App[8886:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000000023'

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您获得的值不是NSString而是__NSCFNumber。尝试做这样的事情:

 if ([[mainDelegate.imagesLikes objectAtIndex:indexPath.section] integerValue] == 1) {
    likesLabel.text = [NSString stringWithFormat:@"%@ %@", [mainDelegate.imagesLikes objectAtIndex:indexPath.section], @"Like"];
} else {
    likesLabel.text = [NSString stringWithFormat:@"%@ %@", [mainDelegate.imagesLikes objectAtIndex:indexPath.section], @"Likes"];
}