Skip to content
NSStrings and NSNumber
step 1/5

Reading — step 1 of 5

Learn

~1 min readBasics

NSString is the Cocoa string type. Literal: @"...":

#import <Foundation/Foundation.h>

NSString *name = @"Ada";
NSString *upper = [name uppercaseString];
NSUInteger len = [name length];

NSString *combined = [NSString stringWithFormat:@"%@ is %lu chars", name, len];
printf("%s\n", [combined UTF8String]);

Note the *NSString is always used as a pointer (it's a class, instances live on the heap).

%@ is NSString's format specifier. Useful for printing an OC object's description.

UTF8String converts NSString to a const char * so printf's %s can print it.

NSNumber boxes primitive values:

NSNumber *n = @42;        // boxed integer
int raw = [n intValue];   // unbox

With automatic reference counting (ARC) enabled (Clang's default), you don't write retain/release — the compiler manages reference counts. Modern OC code looks much cleaner.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…