@chinese-ppmt
2017-12-18T09:48:38.000000Z
字数 1048
阅读 2318
_cmd
和@()
iOS'Special
直到今天(
2017-12-18
)我才明白iOS开发中_cmd
和@()
。惭愧,又高兴!
_cmd
在Objective-C
的方法中表示当前方法的selector
,正如同self
表示当前方法调用的对象实例一样。
🌰例:
UIControl
添加Category
防止btn
重复点击。
_cmd在runtime的objc_getAssociatedObject当做key参数,简直是完美。原因看雷纯锋个人很佩服他
的答案:
我的解释: 把非OC对象(必须非OC对象的)包装成OC对象(NSNumber类型)
实例证明如下:
![]()
注意看XCode
的错误提示:Illegal type 'NSString *' used in a boxed expression
.
![]()
注意看XCode
的黄色警告提示:Incompatible pointer types initializing 'NSString *' with an expression of type 'NSNumber'
。
🌰例: 某商品从服务器获取价格为
@"435.5"
,用户提交订单时选择快递,快递费已知是20
元,用程序计算总价,并显示出来(要求:435.5显示435.5,435.50显示435.5,435.56显示435.56,435.566显示435.57)。
之前的代码
NSString *priceStr = @"435.5";
CGFloat priceFloat = [priceStr floatValue];
CGFloat newPriceFloat = priceFloat + 20;
NSString *newPriceStrFormat = [NSString stringWithFormat:@"%f",newPriceFloat];
//打印结果:455.500000
//下面的自己联想去,😜
if(){
//......
}else{
//......
}
现在的代码
修改上面的newPriceStrFormat
为newPriceStrValue
NSString *newPriceStrValue = [@(newPriceFloat) stringValue];
//打印结果:455.5
总结:处理价格(NSString * 与 float相互转换)的小数点,可优先考虑@().