Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Foundation Framework NSNumber

NSNumber, NSString, etc.

Why do we need NSNumber, NSString, NSArray... when we can just simply define and use an int, float, string etc.?

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

All of these new variable types are Objective-C objects in the Cocoa framework. NSNumber is a class that defines a bunch of different kinds of numbers (similar to int and float). These numbers are more optimized for things like memory and device compatibility. NSString is the Cocoa object for something that doesn't exist in C - a string. In C, you must use a character array for a string, which is definitely not the best way to deal with things. NSString is there to make your life easier when dealing with strings. NSArray is similar to a C array, but it can hold objects (the most common data type in Objective-C), which a C array cannot. All of these objects add various methods and properties that their C counterparts (if they exist) don't have, like NSString's stringByAppendingString:, which creates a new string by putting one string on the end of another.

Great, makes sense. Thanks!