Core Data, NSNumber, Integer 32 and Integer 64
Asked Answered
C

1

5

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?

Callup answered 4/11, 2013 at 10:30 Comment(0)
H
8

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.

Howler answered 4/11, 2013 at 10:41 Comment(6)
Thank you. When you say 'whenever you can' I am a little confused - does this mean that I need to go back to all of the instances when I store or access my Core Data value and use numberWithInteger/integerValue - because I CAN do that - or is there a possible unnecessary cost to doing that? Is there time when it is fine - or desirable - to use numberWithInt/intValue? And does this answer change depending on if I have my Core Data attribute set as Integer 16, Integer 32, Integer 64 or whatever?Callup
Sorry for the lack of clarity. No, in view of what I say in the second paragraph you are fine. I thought you have used Integer64 throughout. You should be fine with Integer32 in most cases - Integer16 is really small though and will most likely fail some time if you are using it for IDs.Howler
I'm sorry, let me clarify. I HAVE used Integer64 throughout at this point but am considering going back and changing some to Integer32 since Integer64 may be overkill. But again, sorry, let me try to ask because I am still confused: do I need to go back to all of the instances when I store or access my Core Data value and use numberWithInteger/integerValue - because I CAN do that - or is there a possible unnecessary cost to doing that? Is there time when it is fine - or desirable - to use numberWithInt/intValue?Callup
IMO this would be unnecessary. That what I meant by writing "you are OK". ;-)Howler
So it really doesn't matter if I use one method or the other unless I need the retrieved value in a specific format? It's all just kind of interchangeable? I can store with NSNumber numberWithInt and retrieve with intValue or integerValue? When I compare database objects for NSNumber equality, I can use intValue or integerValue interchangeably?Callup
Yes, in essence that is true. NSNumber is anyway just an object wrapper around the primitive type. The only case where that would not be true is if you have really high numbers that need 64 bits and you are on using ints. Therefore: use NSInteger.Howler

© 2022 - 2024 — McMap. All rights reserved.