Adding Integer To NSMutableArray
I couldn’t find this anywhere, so I am asking just asking to make sure. And yes, I know its basic, but I figure I’d rather get that right before I make the mistake a million times:
If I do the following, does it cast the “0” to an NSNumber by default (if not, what Object type is it), or would I have to do the 2nd code?
(Or both of these could be wrong for what I’m trying to do. If so, let me know. They both compile so I am just wondering which one is “right” (or preferred) and why.)
NSMutableArray array = [[[NSMutableArray alloc] init]]; [array addObject: 0];
NSMutableArray array = [[NSMutableArray alloc] init]; [array addObject: [NSNumber numberWithInt: 0]];
- Confusing error: Optionality of map's variable changes it from single object to array
- How to append to an array that is a value in a Swift dictionary
- Counting unique items in array. (Swift)
- Function Array<Optional<T>> -> Optional<Array<T>>
- Reset array after playing the game?
- Using NSUserDefaults on arrays
3 Solutions Collect From Internet About “Adding Integer To NSMutableArray”
NSMutableArray doesn’t take primitive types.
The second option is correct.
You cannot put a primitive such as an
int into an
NSArray; you must construct an
NSNumber instance to hold it.
If I do the following, does it cast the “0” to an NSNumber by default (if not, what Object type is it)
No casting takes place. It’s not possible to simply “cast” a primitive into an object, and the primitive does not have an object type. Objects must be constructed by sending messages (except: see below), which happens at runtime; casting only has an effect during compilation.
The only reason this works is that you have chosen
0 to add to the array. This is the same value as
nil, which stands for “no object”. If you had chosen any other integer, you would have a crash on your hands when you ran the code, since the value would be used as a pointer to memory that doesn’t hold a valid object.
Interestingly, though, starting with Clang 3.1 or Apple’s LLVM 4.0,* there is some new syntatical sugar for creating objects from literals in code. We’ve always had literal
@"Lichtenstein". With the new compiler, other objects can likewise be created using the
In this case, to create an
NSNumber object from a literal integer, you can simply write:
*(not yet available in public Xcode)
As stated by the previous replies, you can only add objects (
id type) to container classes like
One class that can be helpful in this context is
NSValue, which serves as a container for non-object data types of C and objective-C. In addition to numerical data types, this can also contain structs and some objective-C primitive data types like
- Cannot dismiss email sheet invoked from UIDocumentInteractionController in iOS 8
- How to convert UTC date string to local time (systemTimeZone)
- How to get an error description when playback fails on MPMoviePlayerController
- The target … overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Pods.xcconfig
- Multiple skshapenode in one draw?
- Can't move UILabel's Y position with Autolayout “on” in view
- NSFetchedResultsController v.s. UILocalizedIndexedCollation
- Removing Shine/Gloss effect on the iPhone through UIPrerenderedIcon not working on device
- What kind of type have the range in for loop?
- Converting NSString to NSDate (and back again)
- Provisioning profiles getting invalidated after refreshing in “Accounts” tab of Xcode
- Rename a class in Xcode: Refactor… is grayed out (disabled). Why?
- cannot find an initializer for type 'String' that accepts an argument list of type '(format: String, argument: UInt32
- how can I get the scale factor of a UIImageView who's mode is AspectFit?
- How to add ARC for specific file?