Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a core data Integer 64 property?

I want to have an Entity property in Core Data be a 64-bit integer. Since the model is going to run on iOS, and as far as I know these devices are not 64-bit, I figured that NSNumber was the way to go (core data gives you the option of objects or scalar properties for primitive types). I'm assuming that NSNumber will internally take care of keeping track of a suitable representation for 64 bits.

Now, I need to subtract 1 from this "64 bit" property in my entity at some point (in case you didn't guess, the 64 bit property is the max_id parameter in the Twitter API), but to do so, I first need to unbox the number inside the NSNumber property.

So should i get the intValue? longValue? unsignedIntValue? unsignedLongValue? long long? which one?

like image 348
SaldaVonSchwartz Avatar asked May 25 '12 23:05

SaldaVonSchwartz


People also ask

How do you use transformable Core Data?

To implement a Transformable attribute, configure it by setting its type to Transformable and specifying the transformer and custom class name in Data Model Inspector, then register a transformer with code before an app loads its Core Data stack.

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

Should I enable Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

What is Core Data in iOS?

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.


1 Answers

Since you already know the type (64 bit integer), you don't need to check for it.

To get a 64 bit integer out of a NSNumber, do one of the following:

NSInteger myInteger = [myNSNumber integerValue];
int64_t   myInteger = [myNSNumber integerValue];

In order to just add one to it, you can use something like this:

myNSNumber = [NSNumber numberWithInteger:[myNSNumber integerValue]+1]];

Note that iOS does have 64 bit data types like int64_t and NSInteger.

EDIT:
If the only reason that you are using NSNumber is to store the 64 bit integer, you can just declare the property like this in your model subclass and skip the unboxing/boxing altogether:

@property (nonatomic) int64_t myIntValue;

Note that core data does this by default if you select the Use scalar properties for primitive data types option of the Create NSManagedObject Subclass feature.

like image 109
lnafziger Avatar answered Sep 23 '22 06:09

lnafziger