版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.04.21 |
前言
前面我简单的写了写NSString的初始化,写了三篇,都不难,但是可以对新手有一定的小帮助,对于大神级人物可以略过这几篇,NSString本来就没有难的,都是细枝末节,忘记了查一下就回了,没有技术难点。
1. NSString简单细说(一)—— NSString整体架构
2. NSString简单细说(二)—— NSString的初始化
3. NSString简单细说(三)—— NSString初始化
下面接着说NSString的初始化,这里说的是从URL进行初始化。
NSString从URL初始化
一、+ (instancetype)stringWithContentsOfURL:(NSURL ***)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
看代码
/**
*1. + (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
*
* @param url :The URL to read.
* @param enc :he encoding of the data at url.
* @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];
NSURL *URL = [NSURL fileURLWithPath:filePath];
NSError *error;
NSString *str = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
NSLog(@"str---%@---%p---%@",str,str,error);
看结果
2017-04-21 00:21:35.966 NSString你会用吗?[1351:29759] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x60800004bc70---(null)
结论:简单不多说了。
二、- (instancetype)initWithContentsOfURL:(NSURL ***)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
// *2.- (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
// *
// * @param url :The URL to read.
// * @param enc :he encoding of the data at url.
// * @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];
NSURL *URL = [NSURL fileURLWithPath:filePath];
NSError *error;
NSString *str = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
NSLog(@"str---%@---%p---%@",str,str,error);
看结果
2017-04-21 00:37:50.460 NSString你会用吗?[1535:36973] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x600000054670---(null)
结果:这里很简单就不多说了。
三、+ (instancetype)stringWithContentsOfURL:(NSURL ****)url usedEncoding:(NSStringEncoding ***)enc error:(NSError * _Nullable *)error;
看代码
// *3.+ (instancetype)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
// *
// * @param url :The URL to read.
// * @param enc :Upon return, if url is read successfully, contains the encoding used to interpret the data
// * @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 url. If the URL can’t be opened or there is an encoding error, returns nil.
// */
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
NSURL *URL = [NSURL fileURLWithPath:filePath];
NSStringEncoding encode;
NSError *error;
NSString *str = [NSString stringWithContentsOfURL:URL usedEncoding:&encode error:&error];
NSLog(@"str---%@---%p---%@",str,str,error);
if (!error) {
NSLog(@"encoding---%ld",encode);
}
看结果
2017-04-21 00:53:09.080 NSString你会用吗?[1869:49496] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x600000056c20---(null)
2017-04-21 00:53:09.081 NSString你会用吗?[1869:49496] encoding---4
结论:这里和我第三篇写的类似了,这里就是自动编码方式读取字符串,如果没有错误就会返回所用的编码方式。4对应的就是UTF-8编码。
四、- (instancetype)initWithContentsOfURL:(NSURL ****)url usedEncoding:(NSStringEncoding ***)enc error:(NSError * _Nullable *)error;
看代码
// *4.- (instancetype)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
// *
// * @param url :The URL to read.
// * @param enc :Upon return, if url is read successfully, contains the encoding used to interpret the data
// * @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 url. If the URL can’t be opened or there is an encoding error, returns nil.
// */
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
NSURL *URL = [NSURL fileURLWithPath:filePath];
NSStringEncoding encode;
NSError *error;
NSString *str = [[NSString alloc] initWithContentsOfURL:URL usedEncoding:&encode error:&error]; ;
NSLog(@"str---%@---%p---%@",str,str,error);
if (!error) {
NSLog(@"encoding---%ld",encode);
}
看结果
2017-04-21 00:51:24.220 NSString你会用吗?[1835:48052] str---AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---0x608000246ae0---(null)
2017-04-21 00:51:24.221 NSString你会用吗?[1835:48052] encoding---4
结论:4对应的就是NSStringUTF8编码。
后记
这里就不多说了,和第三篇类似,都是很简单的,这是给新手看的,这篇是从URL加载,和三相比只是来源路径不同,其他的都是类似的。