从磅转换为公斤,可能使用 Core Data
在我的应用程序中,数字显示为磅。我需要添加一个选项来使用公斤。有谁知道该怎么做?我正在使用核心数据来存储数字。我知道我可以通过使用 if 语句然后进行转换来手动完成这一切,但必须有更好的方法。
编辑 事实上,我认为我遇到了一个很大的问题。它并不像转换数字那么简单。因为我正在使用选择器,并且我想提供国际英制支持,所以选择器应该显示以千克为单位的完整、合理的数字。如果我只是将数字转换为千克,我将得到人们不知道如何使用的十进制数字。有什么建议吗?
In my app, numbers are displayed as pounds. I need to add an option to use kilograms instead. Does anyone know how to go about this? I'm using Core Data to store numbers. I know I can do it all manually by using an if
statement then doing a conversion, but there must be better way.
EDIT
Actually, I think I ran into a huge problem. It isn't as simple as converting numbers. Because I am using a picker and I want to offer international imperial support, The picker should display whole, sensible numbers in kg. If I just convert the numbers to kg, I will get decimal numbers which people will not know how to use. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
https://github.com/davedelong/DDUnitConverter
非常简单。
https://github.com/davedelong/DDUnitConverter
Pretty simple.
我建议使用自定义 NSFormatter 将数字转换为字符串以进行显示。您始终以一种格式存储数据,同时提供以任何格式显示数据的能力。您将需要一个简单的设置(通过 NSUserDefaults) 告诉格式化程序要使用什么格式。这样做的一些好处是您只需在显示数字时处理这个问题。由于您已经必须将数字转换为字符串才能显示它们,因此对代码的更改很少。此外,您根本不必更改核心数据存储,因为设置存储在应用程序的首选项中。您甚至可以子类化 NSNumberFormatter 以在转换后自动格式化数字。以下是一个示例实现:
有关自定义格式化程序的更多信息,请参阅 创建自定义格式化程序。
I would suggest using a custom NSFormatter to convert your numbers into strings for display. You would always store your data in one format, while providing the ability to display it in any format. You will need a simple setting (stored via NSUserDefaults) to tell the formatter what format to use. Some benefits of this are that you only have to deal with this while displaying the numbers. Since you already have to convert the numbers to strings to display them, there will be very few changes to your code. Also, you won't have to change your core data stores at all, since the setting is stored in the app's preferences. You can even subclass NSNumberFormatter for automatic formatting of the number after conversion. Here is an example implementation:
For more information about custom formatters, see Creating a Custom Formatter.
有很多方法可以做到这一点。
我必须对您的应用程序做出一些假设,但如果您想选择(全局)从磅更改为公斤,那么我可能会在您的应用程序中显示一个设置对话框,以便用户可以坚持自己的决定以查看以磅或公斤为单位的东西,然后我会将其保留在您的应用程序的设置中,并使用此设置作为您的应用程序初始化的一部分。然后,无论您在哪里显示磅或公斤的值,您只需要检查用户定义的设置并相应地调整值(可能还有应用程序中的任何标签),这就像检查代码一样简单从核心日期检索值
检查设置将其保留为全局可访问的状态;这可以是您的应用程序委托中的成员,一个包含设置的单例,或者只是在您需要检查它的地方请求该设置。然后,在访问磅数据的代码中,检查设置,如果其设置为千克,则进行计算并将其从数据访问代码返回到视图。
there are so many ways you could do this.
I'd have to make some assumptions about your app, but if you want to have the option to change (globally) from lbs to kgs then i would probably have a settings dialog in your app so that a user can persist their decision to see things in lbs or kgs, i would then persist this in settings for your app and use this setting as part of your app's initialization. then, wherever you show a value of lbs or kgs you just need to check for the user defined setting and adjust the values accordingly (and probably any labels in the app as well) this would be as simple as having a check in the code that retrieves the values from core date
Check setting persist this as a globally accessible piece of state; this could be a member in your app delegate, a singleton that contains settings or simply requesting the setting wherever you need to check it. Then, in code that access the lbs data, check the setting, if its set to kg do the calculation and return that back from the data access code to the view.
将其编码为始终使用单位和缩放作为变量可能是一个好主意。
单位和缩放比例为“磅”和 1.0,或“千克”和 0.45359237,或其他。
It may be a good idea to code it to work always with the units and scaling as variables.
The units and scaling would be "lbs" and 1.0, or "kgs" and 0.45359237, or whatever.