问题描述
在Core Data中,我有许多属性声明为Integer 64,然后可以通过NSNumber属性(默认情况下)进行访问。
是否存储很重要并通过以下方式访问这些值:
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?
如果我之前将所有值都存储为[NSNumber numberWithInt:]-我可以简单地更改现在存储/检索值的方式,还是需要保持一致性,以免对当前用户数据造成问题? / p>
我对同时在32位和64位iOS应用程序中工作特别感兴趣。
也可以-如果Core Data的值是Integer 32,Integer 16,Integer 64等,对您的答案会有影响?
您应该尽可能使用 NSInteger
。原因是它将独立于平台。在32位体系结构上, NSInteger
将是 int
,而在64位框架上是长
。
因此,您以前使用过 int
方法就可以了-它是两者中较小的子集。
出于相同的原因,您存储在Core Data数据库中的内容也可以。将值设置为Integer64的事实确保了 long
个数字也将在以后正确存储。
使用货币也可以,但有一些警告。如果您主要对美分感兴趣,而不是分数的几分之一,那么您显然可以将整数作为整数来跟踪。但是,如果您想进行更复杂的计算(可能涉及几分之一美分),例如某些会计方法或货币换算,请并存储这些结果,您将需要 float之类的东西
或(更好)是双重
。
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];
or by
NSNumber *mySetValue = [NSNumber numberWithInteger:someIntegerValue];
[myObject setMyNumberProperty:mySetValue];
NSInteger myRetrievedValue = [myObject.myNumberProperty 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?
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?
I am particularly interested in this working in both a 32 bit and 64 bit iOS app.
Also - does it make a difference to your answer if the Core Data value is Integer 32, Integer 16, Integer 64, etc?
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
.
Therefore, you are OK having used the int
-methods before - it is the smaller subset of the two.
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.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!