Pages

Saturday, March 27, 2010

Objective-C. Convert C-style string to NSString and vice versa

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Standard C-style string
const char *foo = "Hello, World!";

// Create NSString from a C string
NSString *bar = [NSString stringWithUTF8String:foo];

// Create a C string from an NSString
foo = [bar UTF8String];

[pool drain];
return 0;
}

No comments:

Post a Comment