核心数据、NSNumber、整数 32 和整数 64 [英] Core Data, NSNumber, Integer 32 and Integer 64

查看:30
本文介绍了核心数据、NSNumber、整数 32 和整数 64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Core Data 中,我将许多属性声明为 Integer 64,然后通过 NSNumber 属性访问(这是默认设置).

In Core Data, I have many attributes declared as Integer 64, and then accessed through NSNumber properties (this is by default).

我是否通过以下方式存储和访问这些值是否重要:

Does it matter if I store and access these values by:

NSNumber *mySetValue = [NSNumber numberWithInt:someIntValue];
[myObject setMyNumberProperty:mySetValue];

int myRetrievedValue = [myObject.myNumberProperty intValue];

或由

NSNumber *mySetValue = [NSNumber numberWithInteger:someIntegerValue];
[myObject setMyNumberProperty:mySetValue];

NSInteger myRetrievedValue = [myObject.myNumberProperty integerValue];

?

有两种情况我想知道答案:1) 如果所需的值用于计算(它包含将转换为货币的数量或值)和 2) 如果该值只是一种基本上只会与自身进行比较并且不会用于任何计算的类型.可以在一种情况下使用 numberWithInt 和 intValue 而不是另一种情况,两种情况都可以,还是必须在两种情况下都使用 numberWithInteger 和 integerValue?

There are two case for which I would like to know the answer: 1) if the value needed is used for calculations (it holds a quantity or a value that will be converted to currency) and 2)if the value is just a type which will basically only be compared against itself and will not be used for any calculations. Is it okay to use numberWithInt and intValue in one case and not the other, both cases, or must numberWithInteger and integerValue be used in both cases?

此外,如果我之前将所有值存储为 [NSNumber numberWithInt:] 是否重要 - 我可以简单地更改我现在存储/检索值的方式,还是我需要保持一致性以免创建当前用户数据有问题?

Also, does it matter if I have previously stored all of the values as [NSNumber numberWithInt:] - can I simply change the way I store/retrieve the value now, or do I need to maintain consistency so as not to create a problem with current user data?

我对 32 位和 64 位 iOS 应用程序中的这项工作特别感兴趣.

I am particularly interested in this working in both a 32 bit and 64 bit iOS app.

此外 - 如果核心数据值为整数 32、整数 16、整数 64 等,这对您的答案有影响吗?

Also - does it make a difference to your answer if the Core Data value is Integer 32, Integer 16, Integer 64, etc?

推荐答案

你应该尽可能使用 NSInteger.原因是它将独立于平台.在 32 位架构上,NSInteger 将是 int,在 64 位架构上是 long.

You should be using NSInteger whenever you can. The reason is that it will be platform independent. On 32-bit architecture, an NSInteger will be an int, on 64-bit a long.

因此,您之前使用过 int 方法是可以的 - 它是两者中较小的子集.

Therefore, you are OK having used the int-methods before - it is the smaller subset of the two.

出于同样的原因,您存储在 Core Data 数据库中的内容也没有问题.将该值设置为 Integer64 的事实确保 long 数字将来也能正确存储.

What you have stored in your Core Data database is also OK for the same reason. The fact that you set the value to Integer64 ensures, that also long numbers will be stored correctly in the future.

用作货币也可以,但有一些注意事项.如果您主要对美分而不是美分的分数感兴趣,您显然可以将美分作为整数进行跟踪.但是,如果您想进行可能涉及几分之一的更复杂的计算,例如某些会计方法或货币转换,并存储这些结果,您将需要诸如 float 或(更好)double.

The use as currency is also OK, with some caveats. If you are mainly interested in cents, not fraction of cents, you can obviously just keep track of the cents as integers. However, if you want to do more complex calculations that could involve fractions of cents, such as some accounting methods or currency conversion, and store these results you would need something like a float or (better) a double.

这篇关于核心数据、NSNumber、整数 32 和整数 64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆