Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, October 1, 2013

iOS Adding an Object to a NSArray

So, you want to add an object to a NSArray, do ya?

// This code won't do what you want, don't use it (see below)
NSArray *myArray = [[NSArray alloc] init];
[myArray add...]; // well, crap.

Collections in iOS tend to be immutable. References of type NSArray & NSDictionary (and the like) are intended to be used to pass data around and store in memory, but not change them. In this way, they are well suited for modular code design because their references don't allow for monkeying with their contents when you pass them off. They are also useful in multi-threaded applications, where things can be removed from lists as their being iterated over, which is bad. Ultimately, they encourage good code design.
Want an array that you can manipulate? Well what you're looking for is:
NSMutableArray *myArray = [][NSMutableArray alloc] init];
[myArray addObject:@"This is a string!"]; // yay, now we can add stuff!

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates