Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

poppy11

macrumors newbie
Original poster
I have a string msMyString that was created by

NSMutableString *msMyString = [NSMutableString stringWithString😡"NoSpecialCharactersJustAnOrdinaryString"];

Both of the lines below work perfectly, and so I am not at all certain which is preferred, etc. I know about NSLog(@"%@",msMyString), but I am trying to sort through all of the various ways of converting back and forth between objects, and 'C' primitives. I believe that I put that correctly, but I assume that you know what I mean.

NSLog(@"%s",[msMyString cStringUsingEncoding:NSASCIIStringEncoding]);

NSLog(@"%s",[msMyString UTF8String]);


Thanks in advance.
 
I have a string msMyString that was created by

NSMutableString *msMyString = [NSMutableString stringWithString😡"NoSpecialCharactersJustAnOrdinaryString"];

Both of the lines below work perfectly, and so I am not at all certain which is preferred, etc. I know about NSLog(@"%@",msMyString), but I am trying to sort through all of the various ways of converting back and forth between objects, and 'C' primitives. I believe that I put that correctly, but I assume that you know what I mean.

NSLog(@"%s",[msMyString cStringUsingEncoding:NSASCIIStringEncoding]);

NSLog(@"%s",[msMyString UTF8String]);


Thanks in advance.

If at all possible, stay away from anything other than UTF8.

NSASCIIStringEncoding will break (return NULL) whenever there is a non-ASCII character in the string. There is no reason to take the risk. Only use cStringUsingEncoding: if you actually for whatever strange reason need a string in that particular encoding. Since NSLog expects UTF8 strings, any encoding that isn't a subset of UTF8 will produce strange results.
 
I have a string msMyString that was created by

NSMutableString *msMyString = [NSMutableString stringWithString😡"NoSpecialCharactersJustAnOrdinaryString"];

Both of the lines below work perfectly, and so I am not at all certain which is preferred, etc. I know about NSLog(@"%@",msMyString), but I am trying to sort through all of the various ways of converting back and forth between objects, and 'C' primitives. I believe that I put that correctly, but I assume that you know what I mean.

NSLog(@"%s",[msMyString cStringUsingEncoding:NSASCIIStringEncoding]);

NSLog(@"%s",[msMyString UTF8String]);


Thanks in advance.

not exactly, you encapsulate primitives inside object containers if that makes any sense? for the most part an "NSNumber" is the object to store primitive c types. for your question, the encoding really comes into play if you are parsing say a text file or some sort in UTF8 formatted file to an NSString object. a real world example would be a .csv file. those are usually in UTF8 encoding and converting to a NSString requires you specify that so it can properly "decode" that data and format it correctly as a string object. for your code example, you could have just as easily done this

Code:
 NSMutableString *msMyString = @"NoSpecialCharactersJustAnOrdinaryString";

and ended up with the same result. typically you would have no need to pipe out the various version of a NSString to a console as the regular "%@" option works fine for most cases. if you want to export that string out to a file or across the interwebs, that's when those encoding methods would come in handy.
 
for your code example, you could have just as easily done this

Code:
 NSMutableString *msMyString = @"NoSpecialCharactersJustAnOrdinaryString";

and ended up with the same result. typically you would have no need to pipe out the various version of a NSString to a console as the regular "%@" option works fine for most cases. if you want to export that string out to a file or across the interwebs, that's when those encoding methods would come in handy.

That should be NSString* msMyString = @"xxx";

The right hand side is not a mutable string, so you can't assign it to an NSMutableString*.
 
So I take it that the only reason that NSASCII... didn't fail is because it was purely ASCII text.


Thanks for the information!
 
NSASCIIStringEncoding will break (return NULL) whenever there is a non-ASCII character in the string.

Which is perfectly reasonable if non-ASCII characters are illegal in your particular usage (base64 decoding, hex input, airline record locator ID, etc.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.