简体   繁体   中英

Change an NSSet of NSNumber into an NSSet of int

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

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] .

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.

Keep the NSSet as it is.

You can get the int values of an NSNumber easily.

int intValue = [myNSNumber intValue];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM