How to use core integer 64 property?

I want the Entity property in Core Data to be a 64 bit integer. Since the model is going to work on iOS, and as far as I know, these devices are not 64-bit, I decided that NSNumberthis is the way to go (the main data gives you the possibility of objects or scalar properties for primitive types). I assume that I NSNumberwill internally take care to keep 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 haven’t guessed, the 64 bit property is the max_id parameter in the Twitter API), but for that, I must first remove the number inside the NSNumber property.

So should I get intValue? longValue? unsignedIntValue? unsignedLongValue? long long time? Which one of?

+5
source share
3 answers

Since you already know the type (an integer of 64 bits), you do not need to check it.

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

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

To just add it to it, you can use something like this:

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

Please note that iOS has , has 64-bit data types, such as int64_tand NSInteger.

EDIT:
If the only reason you are using NSNumberis to save a 64-bit integer, you can simply declare a property like this in a subclass of the model, and opt out of unpacking / boxing altogether:

@property (nonatomic) int64_t myIntValue;

, , NSManagedObject Subclass.

+7

NSNumber:

-(int64_t) int64value
{
    if (sizeof(short) == 8)
        return [self shortValue];
    if (sizeof(int) == 8)
        return [self intValue];
    if (sizeof(long) == 8)
        return [self longValue];
    if (sizeof(long long) == 8)
        return [self longLongValue];

    return -1; // or throw an exception
}
0

To get the type C contained in NSNumber, use objCType

Example

NSNumber *myFloat = [NSNumber numberWithFloat:5.5f];
NSLog(@"%s", [myFloat objCType]);

"F" will be printed as it contains a float value.

Also check out @encode (), which will return a type C character.

Example

NSNumber *myFloat = [NSNumber numberWithFloat:5.5f];
if (strcmp(myFloat) == @encode(float)) { 
    NSLog(@"This is a float");
}

Besides

NSNumber *myFloat = [NSNumber numberWithFloat:5.5f];
CFNumberType numberType = CFNumberGetType((CFNumberRef)myFloat);
0
source

All Articles