版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.30 |
前言
前面我簡(jiǎn)單的寫(xiě)了些NSString的初始化秒裕,寫(xiě)了幾篇,都不難枚碗,但是可以對(duì)新手有一定的小幫助逾一,對(duì)于大神級(jí)人物可以略過(guò)這幾篇,NSString本來(lái)就沒(méi)有難的肮雨,都是細(xì)枝末節(jié)遵堵,忘記了查一下就會(huì)了,沒(méi)有技術(shù)難點(diǎn),下面我們繼續(xù)~~~
1. NSString簡(jiǎn)單細(xì)說(shuō)(一)—— NSString整體架構(gòu)
2. NSString簡(jiǎn)單細(xì)說(shuō)(二)—— NSString的初始化
3. NSString簡(jiǎn)單細(xì)說(shuō)(三)—— NSString初始化
4. NSString簡(jiǎn)單細(xì)說(shuō)(四)—— 從URL初始化
5. NSString簡(jiǎn)單細(xì)說(shuō)(五)—— 向文件或者URL寫(xiě)入
6. NSString簡(jiǎn)單細(xì)說(shuō)(六)—— 字符串的長(zhǎng)度
7. NSString簡(jiǎn)單細(xì)說(shuō)(七)—— 與C字符串的轉(zhuǎn)化
8. NSString簡(jiǎn)單細(xì)說(shuō)(八)—— 識(shí)別和比較字符串
9. NSString簡(jiǎn)單細(xì)說(shuō)(九)—— 字符串的合并
10. NSString簡(jiǎn)單細(xì)說(shuō)(十)—— 字符串的分解
11. NSString簡(jiǎn)單細(xì)說(shuō)(十一)—— 字符串的查找
12. NSString簡(jiǎn)單細(xì)說(shuō)(十二)—— 字符串的替換
13. NSString簡(jiǎn)單細(xì)說(shuō)(十三)—— 字符串的分行和分段
14. NSString簡(jiǎn)單細(xì)說(shuō)(十四)—— 字符串位置的計(jì)算
15. NSString簡(jiǎn)單細(xì)說(shuō)(十五)—— 字符串轉(zhuǎn)化為propertyList
16. NSString簡(jiǎn)單細(xì)說(shuō)(十六)—— 畫(huà)字符串
17. NSString簡(jiǎn)單細(xì)說(shuō)(十七)—— 字符串的折疊和前綴
18. NSString簡(jiǎn)單細(xì)說(shuō)(十八)—— 字符串中大小寫(xiě)子母的變換
19. NSString簡(jiǎn)單細(xì)說(shuō)(十九)—— 根據(jù)映射獲取字符串
20. NSString簡(jiǎn)單細(xì)說(shuō)(二十)—— 獲取字符串的數(shù)值
這一篇我們說(shuō)一下字符串與編碼陌宿。
字符串與編碼
一锡足、+ (NSStringEncoding)stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary<NSStringEncodingDetectionOptionsKey,id> *)opts convertedString:(NSString * _Nullable *)string usedLossyConversion:(BOOL *)usedLossyConversion;
下面我們看一下這幾個(gè)參數(shù)。
下面我們?cè)敿?xì)說(shuō)一下
- data:一個(gè)NSData對(duì)象壳坪,包含具有特定編碼方式的字節(jié)舶得。
- opts:決定字符串編碼時(shí)使用的選項(xiàng),是一個(gè)字典爽蝴,其中鍵為NSStringEncodingDetectionOptionsKey沐批。
NSStringEncodingDetectionSuggestedEncodingsKey
NSStringEncodingDetectionDisallowedEncodingsKey
NSStringEncodingDetectionUseOnlySuggestedEncodingsKey
NSStringEncodingDetectionAllowLossyKey
NSStringEncodingDetectionFromWindowsKey
NSStringEncodingDetectionLossySubstitutionKey
NSStringEncodingDetectionLikelyLanguageKey
- string:如果能夠決定一種字符串編碼,則會(huì)返回根據(jù)特定編碼由data轉(zhuǎn)化來(lái)的字符串對(duì)象蝎亚。
- usedLossyConversion:是否使用有損轉(zhuǎn)化九孩,如果一種字符串的編碼被確定,就會(huì)返回一種BOOL值发框,這個(gè)BOOL值與是否使用有損轉(zhuǎn)化有關(guān)躺彬。
-return:返回值為NSStringEncoding,或者如果字符串編碼不能確定的話就會(huì)返回0缤底。
下面還是直接看代碼顾患。
/**
* 1. + (NSStringEncoding)stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary<NSStringEncodingDetectionOptionsKey,id> *)opts convertedString:(NSString * _Nullable *)string usedLossyConversion:(BOOL *)usedLossyConversion;
*
* @param data:An NSData object containing bytes in an encoding to be determined.
* @param opts:Options to use when attempting to determine the string encoding. See String Encoding Detection Options for a full list of supported options.
* @param string:If a string encoding could be determined, upon return contains an NSString object constructed from data using the determined string encoding.
* @param usedLossyConversion:If a string encoding could be determined, upon return contains a BOOL value corresponding to whether lossy conversion was used.
*
* @return:An NSStringEncoding value, or 0 if a string encoding could not be determined.
*
*/
const void *bytes = "ADDSDDSD";
BOOL isLossy = YES;
NSData *data = [NSData dataWithBytes:bytes length:5];
[NSString stringEncodingForData:data encodingOptions:NSStringEncodingDetectionAllowLossyKey convertedString:@"MMMMM" usedLossyConversion:&isLossy];
這個(gè)運(yùn)行需要在MRC下使用,但是運(yùn)行起來(lái)還是崩潰了个唧,是下面這個(gè)問(wèn)題江解,這個(gè)方法我還不怎么會(huì),知道的請(qǐng)和我說(shuō)下徙歼,謝謝犁河。
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x108b22710'
結(jié)論:希望知道的@我。
二魄梯、- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding;
該方法會(huì)返回一個(gè)bool值桨螺,用來(lái)說(shuō)明字符串是否可以進(jìn)行無(wú)損轉(zhuǎn)換。
- 返回值:如果可以進(jìn)行編碼無(wú)損轉(zhuǎn)化會(huì)返回YES酿秸,在轉(zhuǎn)化的過(guò)程中灭翔,如果字符要被改變或者刪除就會(huì)返回NO。如果你打算轉(zhuǎn)化字符串辣苏,方法
dataUsingEncoding:...
失敗的時(shí)候會(huì)返回nil肝箱,可以避免提前調(diào)用這個(gè)方法失敗。
下面看代碼稀蟋。
/**
* 2. - (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding;
*
* @param encoding:A string encoding. For possible values, see NSStringEncoding.
*
* @return:YES if the receiver can be converted to encoding without loss of information. Returns NO if characters would have to be changed or deleted in the process of changing encodings.
*
*/
NSString *str = @"ABCDEFGHI";
BOOL isSuccess = [str canBeConvertedToEncoding:NSUTF8StringEncoding];
NSLog(@"isSuccess = %d",isSuccess);
下面看輸出結(jié)果煌张。
2017-06-30 23:31:56.639 NSString你會(huì)用嗎?[1955:83829] isSuccess = 1
結(jié)論:這個(gè)方法還算簡(jiǎn)單退客。
三骏融、- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
這個(gè)方法返回的是一個(gè)根據(jù)給定編碼方式的NSData對(duì)象链嘀,本方法和dataUsingEncoding:allowLossyConversion:
中第二個(gè)參數(shù)傳入NO是一樣的,下面看代碼档玻。
/**
* 3. - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
*
* @param encoding:A string encoding. For possible values, see NSStringEncoding.
*
* @return:The result of invoking dataUsingEncoding:allowLossyConversion: with NO as the second argument (that is, requiring lossless conversion).
*
*/
NSString *str = @"ABCDEFGHI";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"data = %@",data);
NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str1 = %@",str1);
下面看輸出結(jié)果怀泊。
2017-06-30 23:43:53.368 NSString你會(huì)用嗎?[2123:92867] data = <41424344 45464748 49>
2017-06-30 23:43:53.369 NSString你會(huì)用嗎窃肠?[2123:92867] str1 = ABCDEFGHI
結(jié)論:這個(gè)方法也還好包个。
四刷允、- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;
下面看一下參數(shù)
- encoding:編碼方式冤留,是一個(gè)枚舉值就不多說(shuō)了。
- flag:如果是YES树灶,則在轉(zhuǎn)換中允許字符串被移除和替換纤怒。
- return:包含特定編碼格式的NSData對(duì)象,如果標(biāo)志位為NO天通,和不能進(jìn)行無(wú)損轉(zhuǎn)化泊窘,例如重音符和大小寫(xiě),就會(huì)返回nil像寒。
注意:如果標(biāo)志位為YES烘豹,并且不能進(jìn)行無(wú)損轉(zhuǎn)化時(shí),一些字符串可能在轉(zhuǎn)換中被移除和替換诺祸,例如携悯,在NSUnicodeStringEncoding和NSASCIIStringEncoding轉(zhuǎn)換中,字符‘á’ 會(huì)變成 ‘A’筷笨,失去重音憔鬼。
下面還是看代碼。
/**
* 4. - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;
*
* @param encoding:A string encoding. For possible values, see NSStringEncoding.
*
* @return:The result of invoking dataUsingEncoding:allowLossyConversion: with NO as the second argument (that is, requiring lossless conversion).
*
*/
NSString *str = @"ABCDEFGHI";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str1 = %@",str1);
NSData *data1 = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *str2 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"str2 = %@",str2);
下面看輸出結(jié)果
2017-06-30 23:56:42.384 NSString你會(huì)用嗎胃夏?[2259:102572] str1 = ABCDEFGHI
2017-06-30 23:56:42.385 NSString你會(huì)用嗎轴或?[2259:102572] str2 = ABCDEFGHI
注意:自己看吧。
五仰禀、+ (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding;
該方法會(huì)根據(jù)某個(gè)編碼照雁,返回一個(gè)人可讀的字符串。
下面看代碼答恶。
/**
* 5. + (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding;
*
* @param encoding:A string encoding. For possible values, see NSStringEncoding.
*
* @return:A human-readable string giving the name of encoding in the current locale.
*
*/
NSString *str = @"ABCDEFGHI";
NSString *str1 = [NSString localizedNameOfStringEncoding:NSUTF8StringEncoding];
NSLog(@"str1 = %@",str1);
下面看結(jié)果
2017-07-01 00:01:48.972 NSString你會(huì)用嗎饺蚊?[2344:107655] str1 = Unicode (UTF-8)
注意:這個(gè)好理解吧。
六亥宿、@property(readonly, copy) NSString *description;
該方法返回的是NSString對(duì)象卸勺。
下面直接看代碼。
/**
* 6.@property(readonly, copy) NSString *description;
*/
NSString *str1 = @"T中國(guó)helloChina";
NSString *str = str1.description;
下面看輸出結(jié)果烫扼。
2017-07-01 00:07:49.462 NSString你會(huì)用嗎曙求?[2487:113889] str = T中國(guó)helloChina
注意:很簡(jiǎn)單。
七、@property(readonly) NSStringEncoding fastestEncoding;
該方法會(huì)返回最快的編碼方式悟狱。該方法適用于字符串中字符的檢索静浴,編碼方式對(duì)于空格可能不是有效的。
下面看代碼挤渐。
/**
* 7.@property(readonly) NSStringEncoding fastestEncoding;
*/
NSString *str1 = @"T中國(guó)helloChina";
NSStringEncoding code = str1.fastestEncoding;
NSLog(@"code = %d",code);
下面看輸出結(jié)果苹享。
2017-07-01 00:09:53.333 NSString你會(huì)用嗎?[2532:116355] code = 10
下面看這個(gè)枚舉值
typedef NSUInteger NSStringEncoding;
NS_ENUM(NSStringEncoding) {
NSASCIIStringEncoding = 1, /* 0..127 only */
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8, /* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11, /* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding = 12, /* WinLatin1 */
NSWindowsCP1253StringEncoding = 13, /* Greek */
NSWindowsCP1254StringEncoding = 14, /* Turkish */
NSWindowsCP1250StringEncoding = 15, /* WinLatin2 */
NSISO2022JPStringEncoding = 21, /* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding = 30,
NSUTF16StringEncoding = NSUnicodeStringEncoding, /* An alias for NSUnicodeStringEncoding */
NSUTF16BigEndianStringEncoding = 0x90000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding = 0x94000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF32StringEncoding = 0x8c000100,
NSUTF32BigEndianStringEncoding = 0x98000100, /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding = 0x9c000100 /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
結(jié)論: NSUnicodeStringEncoding就是最快的編碼方式浴麻。
八得问、@property(readonly) NSStringEncoding smallestEncoding;
該編碼方式也許不是最快的獲取字符的方法,但是對(duì)空格有效的软免,這個(gè)屬性獲取字符時(shí)會(huì)消耗一段時(shí)間宫纬。
下面看代碼。
/**
* 8.@property(readonly) NSStringEncoding smallestEncoding;
*/
NSString *str1 = @"T中國(guó)helloChina";
NSStringEncoding code = str1.smallestEncoding;
NSLog(@"code = %d",code);
下面看輸出結(jié)果膏萧。
2017-07-01 00:19:58.240 NSString你會(huì)用嗎漓骚?[2663:123886] code = 10
結(jié)論:和七的方法返回的編碼方式是一樣的。
后記
未完榛泛,待續(xù)~~~