Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data, NSNumber, Integer 32 and Integer 64

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?

like image 863
SAHM Avatar asked Nov 04 '13 10:11

SAHM


1 Answers

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.

like image 71
Mundi Avatar answered Sep 29 '22 06:09

Mundi