首页 > 解决方案 > Xcode 错误 kCFNumberFormatterCurrencyStyle

问题描述

我正在尝试在地图上显示属性的应用程序中格式化 123,123 美元的美国货币。数字 show $(null) 应该显示 #

使用类型为“NSInteger”(又名“long”)的表达式初始化“NSInteger *”(又名“long *”)的不兼容整数到指针转换

从枚举类型'enum CFNumberFormatterStyle'到不同枚举类型'NSNumberFormatterStyle'的隐式转换(又名'enum NSNumberFormatterStyle')

cell.imgViewPropertyType.image = [UIImage imageNamed:@"for_sale"];
NSInteger *intPrice = [theProperty.priceSale integerValue];
NSNumber *tempPrice = [NSNumber numberWithInteger:intPrice];
NSString *price = [NSNumberFormatter localizedStringFromNumber:tempPrice numberStyle:kCFNumberFormatterCurrencyStyle];

标签: objective-c

解决方案


试试这个。请注意,这intPrice不是指针,因此您需要声明为NSInteger intPrice而不是NSInteger *intPrice. 这就是您遇到错误的原因。

cell.imgViewPropertyType.image = [UIImage imageNamed:@"for_sale"];
NSInteger intPrice = [theProperty.priceSale integerValue];
NSNumber *tempPrice = [NSNumber numberWithInteger:intPrice];

正如 rmaddy 在下面指出的那样,您也可以使用下面的行。

NSNumber *tempPrice = @(intPrice);

关于关于CFNumberFormatterStylelocalizedStringFromNumber接受NSNumberFormatterStyle而不是类型的第二个错误CFNumberFormatterStyle。您正试图在那里传递错误类型的样式。尝试使用NSNumberFormatterCurrencyStyle.

NSString *price = [NSNumberFormatter localizedStringFromNumber:tempPrice numberStyle:NSNumberFormatterCurrencyStyle];

推荐阅读