cheznead
cheznead

Reputation: 2799

Is UTF8String as a message necessary to prepare a string object ahead of conversion to upper/lower case?

What I understand about UTF8String as a message is that it is a non-class message, and it is used to convert an NSString object to a C string which can be printed by using the %s format specifier.

I think it is used in other contexts other than when changing an NSString object into a C string ahead of then changing it to upper or lower case, for example.

But is it absolutely necessary to use it to convert a string object into a C string before you convert the NSString object to uppercase or lowercase? Also, if it isn't, as the code below suggests, why is it used in first example and not in the second?

Appreciate any help in understanding this... don't want to skip by stuff as a learner.

The code that made me wonder about it is this which I came across in the excellent Objective-C book by S. Kochan:

NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;

...

res = [str1 uppercaseString];
NSLog [@"Uppercase conversion: %s", [res UTF8String]);

It isn't used in the conversion to lowercase example:

res = [str1 lowercaseString];
NSLog (@"Lowercase conversion: %@", res];

Upvotes: 0

Views: 66

Answers (2)

gnasher729
gnasher729

Reputation: 52538

Calling UTF8String when you don't actually want a C string with characters in UTF-8 encoding is a waste of code, time, and space. It doesn't give any benefit whatsoever. The line

NSLog [@"Uppercase conversion: %s", [res UTF8String]);

is just stupid.

NSLog [@"Uppercase conversion: %@", res);

is simpler and doesn't waste the space for pointlessly allocating a UTF-8 string.

Upvotes: 1

Ken Thomases
Ken Thomases

Reputation: 90571

The -UTF8String method returns a C-style string (a pointer to a buffer of characters, terminated by a null character) in the UTF-8 encoding. That's its purpose and any attempt to use it for other purposes is misguided.

Whether calling -UTFString somehow modifies the receiver is an implementation detail that you shouldn't concern yourself with. (Before anybody objects that NSString is immutable, that just means that the string it represents doesn't change. It doesn't necessarily mean that the object is bit-wise constant. It could do internal caching, for example, without violating immutability.)

Conversion to lower- or upper-case is completely unrelated to calling -UTFString.

If you're choosing between:

NSLog(@"some string: %s", [res UTF8String]);

and:

NSLog(@"some string: %@", res);

prefer the latter. The former tries to dictate how the string should be converted to a character stream, forces a conversion to UTF-8 which may not be necessary, etc. The latter expresses your intent more directly and simply and leaves the greatest latitude to the framework to implement things correctly and efficiently.

Only call -UTF8String if a situation arises where you need a C-style string in UTF-8 encoding.

Upvotes: 1

Related Questions