How to tell Core Data's NSNumber property's type
Asked Answered
Z

4

7

I have an iOS 4 project using Core Data. When I design the Core Data Model, the attributes have Integer 64, Integer 32, Integer 16, Decimal, Double, Float, and Boolean.

But in the generated NSManagedObject subclasses, they are all NSNumber*. So when I use it, how can I tell if that NSNumber is a long, a double, a float, or a BOOL?

Zippel answered 21/4, 2011 at 14:36 Comment(0)
G
8
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:myManagedObjectContext];
NSAttributeDescription *attribute = [[entity attributesByName] objectForKey:@"myAttribute"];
if ([attribute attributeType] == NSInteger32AttributeType) {
    // We have an Integer32
    // ...
}

Check the NSAttributeDescription.h header for other valid attribute types besides NSInteger32AttributeType.

Ganiats answered 21/4, 2011 at 15:4 Comment(1)
There's a right bracket missing in the if statement. I tried to edit but was denied since there's a 6 character minimum for edits, but maybe you (Ole) can edit it since you're the original author.Esp
O
2

NSNumber is toll-free bridged with CFNumber, and CFNumber can do this:

NSNumber *someNum = [myManagedObject someNum];
CFNumberType t = CFNumberGetType((CFNumberRef)someNum);

The CFNumberType enum value tells you the type actually used by the number class to store your value. But the type is not guaranteed to be what you said it should be when you created the number, so if you want to know what type the model expects, you should do as Ole Begemann suggested and get the attribute type of the AttributeDescription.

Oldfangled answered 21/4, 2011 at 15:17 Comment(0)
S
0

I'm not sure there is a way to know that, but to make your life easier you can create a Category for each Core Data Entity, and add new setters and getters that match the actual type of your attributes. On those setters/getters you have to make the data conversions and invoke the generated Core Data setters/getters.

Stoneham answered 21/4, 2011 at 14:53 Comment(1)
the reason why I want to do this, is I need to parse some CSV-like files. I have hundreds of different Core Data Entities, I really need some common way other then subclass each entity.Zippel
G
0

The Core Data layer can only deal with objects, the different selections you see is how it will be stored in the underlying database. The NSNumber class can "convert" freely between the above values. Just select what makes sense to do in your case. Is is a bool? then save it as such (takes less space in the db) do you need high precision do double etc. When you at runtime deal with the values NSNumber will take care of everything for you. So a bool accessed in code looks like this:

if([[NSManagedObject isActive] boolValue])

or setting:

[[NSManagedObject setIsActive:[NSNumber numberWithBool:YES]];
Glynas answered 21/4, 2011 at 15:8 Comment(1)
I think this is not what I asked for... I'll go for @Ole Begemann'sZippel

© 2022 - 2024 — McMap. All rights reserved.