Convert between NSString and NSData

Objective C Apr 16, 2020 Viewed 11 Comments 0

With Objective C in iOS, we can convert the value of NSString to NSData.

NSString* str = @"Hello World!";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSData to NSString .

NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

Using UTF-8 encoding.

Updated Apr 16, 2020