在平常的开发中,数字格式化为字符串放到容器中是一个很常见的场景.但objc的方法要写的都太长了(尤其是参数多的时候),经常是要么出现巨长的一段([container setObject:[NSString stringWithFormat:@"%ld",(NSUInteger)100] forKey:key]
),要么为了适配32位精度丢失(Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'
)。
偶然发现NSValue的一个属性 => @property(readonly, copy) NSString *stringValue;
解释是这样的:
Summary: The number object's value expressed as a human-readable string. Discussion: The string is created by invoking descriptionWithLocale: where locale is nil.
于是上面的两个问题就可以这么做:
container[key] = @(100).stringValue