高效使用NSString Category相关的NSNumberFormatter
我正在编写一个需要 NSNumberFormatter 的 NSString 类别。每当我的类别用于以某种方式打印字符串或在我的应用程序中初始化格式化程序并传递它时,我就陷入了初始化和释放之间的困境。 NSNumberFormatter 首先运行几个配置调用,以使其正常工作,而在我的应用程序的各个位置重复这些调用似乎是错误的。尽管我可以在主应用程序委托中创建一个格式化程序,并让任何人在使用我的一个类别添加项时获取它,但我认为这更像是一种黑客行为,而不是一种优雅的解决方案。那么,在 NSString Category 的便捷方法中创建和销毁 NSNumberFormatter 听起来足够好吗?
干杯, 克雷格
I'm writing an NSString category that requires an NSNumberFormatter. I'm stuck between initing and releasing one every time my category is used to print a string in a certain way or initing the formatter in my app and passing it through. The NSNumberFormatter has a couple of config calls run on it first to make it work just right and repeating that in various places in my app just seems wrong. And though I could create one formatter in the main app delegate and let anyone grab it when using one of my category additions, I think that's more of a hack than an elegant solution. So, does creating and destroying an NSNumberFormatter in an NSString Category's convenience method sound good enough?
Cheers,
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
D'oh,答案似乎是类别中的静态变量。不知何故,我意识到 Objective-C 无法做到这一点。但它可以,这就是我正在使用的。
D'oh the answer seems to be a static variable in the category. Somehow I'd gotten hold of the idea that Objective-C couldn't do that. But it can so that's what I'm using.