iOS編碼問題-Unicode轉(zhuǎn)UTF-8

公司項(xiàng)目使用的icon是阿里圖標(biāo)庫的圖標(biāo)字體。
將圖標(biāo)下載到了項(xiàng)目中,在使用中發(fā)現(xiàn)"\u"后面必須跟4位16進(jìn)制(否則編譯報(bào)錯(cuò)),不能進(jìn)行拼接處理。
所以需要將"\\ue670"(內(nèi)容為一個(gè)整體Unicode的UTF-8字符串)轉(zhuǎn)化為一個(gè)UTF-8的一個(gè)字符。
哈哈,也不知道說明白沒有。
總之下面方法實(shí)現(xiàn)了該功能。

//Unicode轉(zhuǎn)UTF-8
+ (NSString*) replaceUnicode:(NSString*)aUnicodeString
{

    NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"]; 

    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""]; 

    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""]; 
    //tempStr3現(xiàn)在的值為  @"\"\\ue670\""  
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding]; 

    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData 

                                                           mutabilityOption:NSPropertyListImmutable  

                                                                    format:NULL 

                                                           errorDescription:NULL]; 
    //通過NSPropertyListSerialization將NSData數(shù)據(jù)對象歸檔提取成String,這時(shí)候的returnStr就是 @"\ue670"了
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];

}

其中使用到了NSPropertyListSerialization
官方解釋為
The NSPropertyListSerialization class provides methods that convert property list objects to and from several serialized formats. Property list objects include NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber objects. These objects are toll-free bridged with their respective Core Foundation types (CFData, CFString, and so on). For more about toll-free bridging, see “Interchangeable Data Types”.
翻譯為
NSPropertyListSerialization這個(gè)類提供了一些方法,專門用來轉(zhuǎn)換不同組織形式的list對象.list對象包括了NSData, NSString, NSArray, NSDictionary, NSDate, 以及 NSNumber.這些對象與衍生出他們的祖先對象有著一些聯(lián)系,對于toll-free bridging,請看“Interchangeable Data Types。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容