NSNumber vs Int
Asked Answered
C

3

10

If integers cannot be written to a dictionary and then to a .plist, but NSNumbers can is it better to use NSNumbers throughout the app, rather than needing to convert every-time saving or loading a dictionary from a .plist?

Calais answered 12/7, 2011 at 10:24 Comment(3)
Note that memory allocation is expensive. You might want to make your own class similar to NSNumber that would be mutable.Gadolinium
Prefer NSInteger over int. This is more portable among various versions Of OS X.Hugely
You can's simply substitute an NSNumber (an object) for an int (a "scalar value"). It would be incredibly awkward to keep every numerical quantity in an NSNumber and convert from/to for every computation. Using NSInteger instead of int, on the other hand, is a reasonable thing to do.Wynn
F
8

As a generalization: Just stick with POD types until you need to use an object based representation, such as NSNumber. The performance is much better with the PODs, but you'll need NSNumber in some cases.

In some cases, it may make sense to use NSNumber instead -- this is typically when you reuse a NSNumber often -- this is to avoid making a ton of duplicate NSNumbers. Such occurrences are practical only rarely beyond serialization and generic objc interfaces (bindings, transformers, dictionaries).


Update/Details: The ObjC runtime will in some cases, on some architectures, and on some OS versions substitute a tagged pointer representing NSNumbers of specific type and domain. Although the internal representation has changed since originally written a few years back, here is a good introduction to the subject: http://objectivistc.tumblr.com/post/7872364181/tagged-pointers-and-fast-pathed-cfnumber-integers-in. Where this can be used, it saves you from slow operations like allocations, locking, and ref count ops. Nevertheless, tagged pointers are incapable of representing every number and it introduces overhead, so you should still favor basic builtins over NSNumber as a default. Tagged pointers are a great optimization where applicable, but are far from competing with the builtins when you just need a number.

Fluker answered 12/7, 2011 at 10:33 Comment(2)
The great thing is that KVC supports autoboxing, i.e. conversion between scalar types and NSNumber, so there's little need to use NSNumber in properties.Hyphenate
You might want to revise this considering the implications of 64-bit devices.Superabound
S
0

NSNumber is object inherited from NSValue wrapper object.

int is not object.

if use NSNumber u can get more and more function to utilize with them.

http://developer..com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html

NSNumber is a class that helps you to store numeric types as object. It has methods to convert between different types and methods to retrieve a string representation of your numeric value. If you use a variable day of type NSNumber* the way you did in your example, you are not modifying the value of day but its memory address.

Stream answered 12/7, 2011 at 10:29 Comment(1)
Your url is missing an apple in the middle.Wang
B
0

It all depends on your need. But, if the API requires you to use int, you should use int. It it asks you to use NSNumber you should use NSNumber.

For example, if you are using a UISegmentedControl, and you want to select a segment, then,

[segmentedControl setSelectedSegmentIndex:aIntVar]; // Can not use NSNumber here
// or
[segmentedControl setSelectedSegmentIndex:[aNumber intValue]];
Blasting answered 12/7, 2011 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.