NSString简单细说(三)—— NSString初始化

版本记录

版本号时间
V1.02017.04.19

前言

前面我写了简单细说(一)简单细说(二),一中主要是介绍了NSString的主要API架构,二中主要是对21种初始化方法进行了介绍了,都是入门级的,相信大家都可以看得懂,好几天没写这方面的东西了,今天有点时间,所以我就接着写了。感兴趣的可以看我上面几篇。
1. NSString简单细说(一)—— NSString整体架构
2. NSString简单细说(二)—— 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);

我们运行下看结果。

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

结论:简单不说。


二、- (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);

看结果

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

结论:简单不说。


三、+ (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.
     */
    
    //该方法就是自动判断encode,如果打开成功,就把encode放在enc里面,返回给调用者,可以先声明一个NSStringEncoding类型,其实就是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);
    }

看结果

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

结论:看见返回的是4,4对应的编码类型就是NSUTF8StringEncoding。
大家可能疑惑为什么返回的是0,其实这是个枚举值,让我们好好看看编码方式的大家庭。

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);
    }

看结果

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

结论:和三中说的是一种原理,就不多说了。

后记

我们上面介绍了四种从文件加载字符串的方法,其实过程都差不多,就是打开文件,并根据不同的编码方式,初始化字符串,这些并不难,但是我还是总结出来了,可以给初学者看看。谢谢大家对我的支持。



作者:刀客传奇
链接:https://www.jianshu.com/p/93aba7cb6c96
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值