views:

209

answers:

3

Hi everyone, I'm using the data with core data and get an NSSet of NSNumber... my question is how can I do to change easily all the objects into int values? or can I directly get the int values from the database?

Thanks

+5  A: 

NSSet cannot store primitive values such as int, that's why NSNumber is used. Often you can just use NSNumber without doing any casting. If you do need the int value call [mynumber intValue].

Benedict Cohen
Thanks... I was hoping for a direct solution like perform selection!
ncohen
+1  A: 

Keep the NSSet as it is.

You can get the int values of an NSNumber easily.

int intValue = [myNSNumber intValue];
Brock Woolf
A: 

This does not answer your question, but it's related and it's insanely useful.

Let's say I have a collection (NSArray, NSSet, etc) of objects of a certain type, and these objects have a method/property called displayValue that somehow transforms the object into a more user-friendly format for displaying. To transform my collection of objects into a collection of their displayValues, I only need to do:

NSArray * objects = ...;
NSArray * objectDisplayValues = [objects valueForKey:@"displayValue"];

The only caveat is that if displayValue returns a primitive (int, float, a struct, etc), it will be boxed into the appropriate NSValue container.

So theoretically you could do:

NSArray * numbers = ...;
NSArray * intValues = [numbers valueForKey:@"intValue"];

But since the intValue would be boxed into an NSNumber, you'd be right back where you started.

As I said, this isn't exactly an answer, but it's dead useful.

Dave DeLong