stringWithFormat

In my ios application ,

NSString *markup = [NSString stringWithFormat:@"%@", content];

content using utf-8 encoding , is equal to "------\U539f\U59cb\U901a\U8baf------"


In ios8.4.1 iphone, markup is equal to "------原始通讯------", the result is exactly what we need.

but in ios9 iphone, markup is messy code.


Other about NSUTF8StringEncoding encoding, there is similar problem , such as:


CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);

CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);

You seem to be having two somewhat unrelated problems here:

  • an issue with

    +stringWithFormat:
    (A)
  • an issue with text encoding names (B)

With regards A, I don’t really understand your question. Can you post a concrete snippet of code that illustrates the difference between iOS 8 and 9?

With regards B, consider this snippet:

CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
NSLog(@"cfencstr = '%@'", (__bridge NSString *) cfencstr);

I ran this on both iOS 9 (GM seed) and iOS 8 (8.4.1) and both produced the same output, namely:

2015-09-14 10:29:27.921 xxx[478:144205] cfencstr = 'utf-8'

What is the problem you’re seeing?

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

eskomo, thanks a lot for your reply.


CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);

CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);

NSLog(@"cfencstr = '%@'", (__bridge NSString *) cfencstr);


const char *enc = CFStringGetCStringPtr(cfencstr, 0);

in iOS 9 (GM seed), enc is NULL;

in iOS 8 (8.4.1), enc is "utf-8"

because of this error, lead to chinese display problem,


why is the problem? thanks !!

From the documentation for CFStringGetCStringPtr():

This function either returns the requested pointer immediately, with no memory allocations and no copying, in constant time, or returns NULL. If the latter is the result, call an alternative function such as the CFStringGetCString function to extract the characters.


Whether or not this function returns a valid pointer or NULL depends on many factors, all of which depend on how the string was created and its properties. In addition, the function result might change between different releases and on different platforms. So do not count on receiving a non-NULL result from this function under any circumstances.

(Emphasis added.)


You must always be prepared for CFStringGetCStringPtr() to return NULL. If it does, you have to call another function to get the characters, such as CFStringGetCString().

What Ken Thomases said plus…

I strongly recommend that you avoid

CFStringGetCStringPtr
unless you have very specific needs. It’s much better to bridge the result back to an NSString and, from there, using the
UTF8String
property (or if you need a specific encoding
-cStringUsingEncoding:
). For example:
static NSString * IANACharSetNameForNSStringEncoding(NSStringEncoding nsEnc) {
    CFStringEncoding cfEnc = CFStringConvertNSStringEncodingToEncoding(nsEnc); 
    return (__bridge NSString *) CFStringConvertEncodingToIANACharSetName(cfEnc); 
}

In fact, I recommend that you avoid CFString entirely except in situations, like this, where it provides some functionality that’s not provided by NSString.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
stringWithFormat
 
 
Q