Shared NSDateFormatter - Best Practices?

久未见 提交于 2019-12-04 02:35:39

Normally, you would just not worry about it. Obj-C will let you fiddle with the juicy insides of almost anything. Even @private doesn't protect against -valueForKey:_thatFunPrivateIvar. And if all else fails, you can just invoke runtime functions.

However, the simplest workaround here would be to expose an API that internally uses cached formatters, but that provides no access to the formatters it is using. Your code would then use +[Formatter shortTimeStringFromDate:] to do what your sample code is doing now. The formatter in question could be lazily allocated, and you could use purgeable memory so cached formatters could be cleared in a LRU fashion under memory pressure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!