November 2013
Beginner
325 pages
9h 47m
English
If you are using C strings in an Objective-C program, you will need to know how to make an NSString from a C string. The NSString class has a method for this:
char *greeting = "Hello!"; NSString *x = [NSString stringWithCString:greeting encoding:NSUTF8StringEncoding];
You can also get a C string from an NSString. This is a little trickier because NSString can handle some characters that certain encodings cannot. It is a good idea to check that the conversion can occur:
NSString *greeting = "Hello!";
const char *x = NULL;
if ([greeting canBeConvertedToEncoding:NSUTF8StringEncoding]) {
x = [greeting cStringUsingEncoding:NSUTF8StringEncoding];
}
You do not own the resulting C string; the system ...
Read now
Unlock full access