NSString簡(jiǎn)單細(xì)說(shuō)(三)—— NSString初始化

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.04.19

前言

前面我寫(xiě)了簡(jiǎn)單細(xì)說(shuō)(一)簡(jiǎn)單細(xì)說(shuō)(二),一中主要是介紹了NSString的主要API架構(gòu),二中主要是對(duì)21種初始化方法進(jìn)行了介紹了,都是入門(mén)級(jí)的,相信大家都可以看得懂,好幾天沒(méi)寫(xiě)這方面的東西了,今天有點(diǎn)時(shí)間,所以我就接著寫(xiě)了。感興趣的可以看我上面幾篇。
1. NSString簡(jiǎn)單細(xì)說(shuō)(一)—— NSString整體架構(gòu)
2. NSString簡(jiǎn)單細(xì)說(shuō)(二)—— NSString的初始化

NSString初始化

前面兩篇也講到了NSString的初始化,這里接著講初始化,但是這里的初始化是從文件中加載。

一、+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;

我們直接看代碼

    /**
     *1. + (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
     *
     *  @param path :A path to a file..
     *  @param enc :The encoding of the file at path.
     *  @param error :If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
     *
     *  @return :A string created by reading data from the file named by path using the encoding, enc. If the file can’t be opened or there is an encoding error, returns nil..
     */

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
    NSError *error;
    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"str---%@---%p---%@",str,str,error);

我們運(yùn)行下看結(jié)果。

2017-04-19 00:38:22.309 NSString你會(huì)用嗎?[2166:58609] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x600000243420---(null)

結(jié)論:簡(jiǎn)單不說(shuō)。


二、- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;

看代碼

    /**
     *2. - (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;;
     *
     *  @param path :A path to a file..
     *  @param enc :The encoding of the file at path.
     *  @param error :If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
     *
     *  @return :An NSString object initialized by reading data from the file named by path using the encoding, enc. The returned object may be different from the original receiver. If the file can’t be opened or there is an encoding error, returns nil.
     */
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
    NSError *error;
    NSString *str = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"str---%@---%p---%@",str,str,error);

看結(jié)果

2017-04-19 00:50:20.511 NSString你會(huì)用嗎?[2355:67660] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x6000000520c0---(null)

結(jié)論:簡(jiǎn)單不說(shuō)。


三、+ (instancetype)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;

看代碼

    /**
     *3. + (instancetype)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
     *
     *  @param path :A path to a file..
     *  @param enc :The encoding of the file at path.
     *  @param error :If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
     *
     *  @return A string created by reading data from the file named by path. If the file can’t be opened or there is an encoding error, returns nil.
     */
    
    //該方法就是自動(dòng)判斷encode,如果打開(kāi)成功,就把encode放在enc里面,返回給調(diào)用者,可以先聲明一個(gè)NSStringEncoding類(lèi)型,其實(shí)就是NSUInteger,然后送指針給方法。
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
    NSError *error;
    NSStringEncoding encoding;
    NSString *str = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
    NSLog(@"str---%@---%p---%@",str,str,error);
    if (!error) {
        NSLog(@"encoding---%ld",encoding);
    }

看結(jié)果

2017-04-20 23:50:47.771 NSString你會(huì)用嗎?[836:14392] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x600000053ce0---(null)
2017-04-20 23:50:53.045 NSString你會(huì)用嗎?[836:14392] encoding---4

結(jié)論:看見(jiàn)返回的是4,4對(duì)應(yīng)的編碼類(lèi)型就是NSUTF8StringEncoding。
大家可能疑惑為什么返回的是0,其實(shí)這是個(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 */
};


四、- (instancetype)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;

看代碼

    /**
     *4. - (instancetype)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
     *
     *  @param path :A path to a file..
     *  @param enc :The encoding of the file at path.
     *  @param error :If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
     *
     *  @return An NSString object initialized by reading data from the file named by path. The returned object may be different from the original receiver. If the file can’t be opened or there is an encoding error, returns nil..
     */
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
    NSError *error;
    NSStringEncoding encoding;
    NSString *str = [[NSString alloc] initWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
    NSLog(@"str---%@---%p---%@",str,str,error);
    if (!error) {
        NSLog(@"encoding---%ld",encoding);
    }

看結(jié)果

2017-04-20 23:54:06.808 NSString你會(huì)用嗎?[907:17025] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x60000004d350---(null)
2017-04-20 23:54:06.809 NSString你會(huì)用嗎?[907:17025] encoding---4

結(jié)論:和三中說(shuō)的是一種原理,就不多說(shuō)了。

后記

我們上面介紹了四種從文件加載字符串的方法,其實(shí)過(guò)程都差不多,就是打開(kāi)文件,并根據(jù)不同的編碼方式,初始化字符串,這些并不難,但是我還是總結(jié)出來(lái)了,可以給初學(xué)者看看。謝謝大家對(duì)我的支持。

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

相關(guān)閱讀更多精彩內(nèi)容

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