iOSiOS面试

NSString简单细说(二十二)—— 与路径相关(一)

2017-08-21  本文已影响11人  刀客传奇

版本记录

版本号 时间
V1.0 2017.08.21

前言

前面我简单的写了些NSString的初始化,写了几篇,都不难,但是可以对新手有一定的小帮助,对于大神级人物可以略过这几篇,NSString本来就没有难的,都是细枝末节,忘记了查一下就会了,没有技术难点,下面我们继续~~~
1. NSString简单细说(一)—— NSString整体架构
2. NSString简单细说(二)—— NSString的初始化
3. NSString简单细说(三)—— NSString初始化
4. NSString简单细说(四)—— 从URL初始化
5. NSString简单细说(五)—— 向文件或者URL写入
6. NSString简单细说(六)—— 字符串的长度
7. NSString简单细说(七)—— 与C字符串的转化
8. NSString简单细说(八)—— 识别和比较字符串
9. NSString简单细说(九)—— 字符串的合并
10. NSString简单细说(十)—— 字符串的分解
11. NSString简单细说(十一)—— 字符串的查找
12. NSString简单细说(十二)—— 字符串的替换
13. NSString简单细说(十三)—— 字符串的分行和分段
14. NSString简单细说(十四)—— 字符串位置的计算
15. NSString简单细说(十五)—— 字符串转化为propertyList
16. NSString简单细说(十六)—— 画字符串
17. NSString简单细说(十七)—— 字符串的折叠和前缀
18. NSString简单细说(十八)—— 字符串中大小写子母的变换
19. NSString简单细说(十九)—— 根据映射获取字符串
20. NSString简单细说(二十)—— 获取字符串的数值
21. NSString简单细说(二十一)—— 字符串与编码
这一篇我们说一下与路径相关

与路径相关

一、+ (NSString *)pathWithComponents:(NSArray<NSString *> *)components;

下面看一下参数,这个参数是一个数组。它是一个字符串的集合,创建绝对路径,使用/作为第一个部分,同时包含尾随路径分隔符,使用空字符串作为最后的部分。该方法不会清除创建的路径,使用stringByStandardizingPath解析空的部分,相对父路径。该方法的返回值就是根据数组中的对象顺序,用路径分隔符将他们分开。

下面看代码。

    /**
     * 1. + (NSString *)pathWithComponents:(NSArray<NSString *> *)components;
     *
     *  @param encoding:An array of NSString objects representing a file path. To create an absolute path, use a slash mark (“/”) as the first component. To include a trailing path divider, use an empty string as the last component.
     *
     *  @return:A string built from the strings in components by concatenating them (in the order they appear in the array) with a path separator between each pair.
     *
     */

    NSArray *strArr = @[@"Iam",@"a",@"winner"];
    NSString *str = [NSString pathWithComponents:strArr];
    NSLog(@"str1 = %@",str);

下面看输出结果。

2017-07-01 23:17:35.190 NSString你会用吗?[1362:52972] str1 = Iam/a/winner

结论:这个方法还好。


二、@property(readonly, copy) NSArray<NSString *> *pathComponents;

这个方法和方法一正好是可逆的,这个方法返回的是路径组成的各个部分。返回的组成部分和它们在路径中的顺序是一样的,如果字符串以路径分隔符开始或者结束,那么第一个部分或者最后一个部分是分隔符自己,下面看代码。

    /**
     * 2.@property(readonly, copy) NSArray<NSString *> *pathComponents;
     */
    
    NSString *str1 = @"Ttt/hello/China";
    NSArray *arr1 = str1.pathComponents;
    NSLog(@"arr1 = %@",arr1);
    
    NSString *str2 = @"/hello/China";
    NSArray *arr2 = str2.pathComponents;
    NSLog(@"arr2 = %@",arr2);
    
    NSString *str3 = @"hello";
    NSArray *arr3 = str3.pathComponents;
    NSLog(@"arr3 = %@",arr3);

下面看输出结果。

2017-07-01 23:31:12.036 NSString你会用吗?[1528:63957] arr1 = (
    Ttt,
    hello,
    China
)
2017-07-01 23:31:12.037 NSString你会用吗?[1528:63957] arr2 = (
    "/",
    hello,
    China
)
2017-07-01 23:31:12.038 NSString你会用吗?[1528:63957] arr3 = (
    hello
)

结论:简单易懂。


三、- (NSUInteger)completePathIntoString:(NSString * _Nullable *)outputName caseSensitive:(BOOL)flag matchesIntoArray:(NSArray<NSString *> * _Nullable *)outputArray filterTypes:(NSArray<NSString *> *)filterTypes;

很多解释可以从官方文档或者示例代码给出。

下面看一下参数。

参数列表

这个方法的作用其实就是:找到满足一定条件的文件扩展名,例如,给定一个文件夹~/Demo包含以下文件ReadMe.txt 、readme.html 、readme.rtf 、recondite.txt 、test.txt

你可以找到可能的路径~/Demo/r,如下所示。

NSString *partialPath = @"~/Demo/r";
NSString *longestCompletion;
NSArray *outputArray;
 
unsigned allMatches = [partialPath completePathIntoString:&longestCompletion
    caseSensitive:NO
    matchesIntoArray:&outputArray
    filterTypes:nil];
 
// allMatches = 3
// longestCompletion = @"~/Demo/re"
// outputArray = (@"~/Demo/readme.html", "~/Demo/readme.rtf", "~/Demo/recondite.txt")

上面所有以r开头的子文件就都找到了。这里filterTypes传递为nil表示不限制文件搜索的类型,如果想要限制就给一个文件类型的数组就可以了。

NSArray *filterTypes = @[@"txt", @"rtf"];
 
unsigned textMatches = [partialPath completePathIntoString:&outputName
    caseSensitive:NO
    matchesIntoArray:&outputArray
    filterTypes:filterTypes];
// allMatches = 2
// longestCompletion = @"~/Demo/re"
// outputArray = (@"~/Demo/readme.rtf", @"~/Demo/recondite.txt")

上面这个例子,限制了文件类型为txtrtf,所以找到的子文件类型也就是这两种类型的。

还有的地方需要注意:

结论:很少用,但是值得一看。


四、@property(readonly) const char *fileSystemRepresentation;

这个属性是只读的就是文件路径的C字符串,下面还是直接看代码。

- (void)demoFileSystemRepresentation
{
    NSString *str = @"lily";
    
    NSString *f1 = [NSHomeDirectory() stringByAppendingPathComponent:@"ReadMe.txt"];
    NSString *f2 = [NSHomeDirectory() stringByAppendingPathComponent:@"readme.html"];
    NSString *f3 = [NSHomeDirectory() stringByAppendingPathComponent:@"readme.rtf"];
    NSString *f4 = [NSHomeDirectory() stringByAppendingPathComponent:@"recondite.txt"];
    NSString *f5 = [NSHomeDirectory() stringByAppendingPathComponent:@"test.txt"];
    
    [str writeToFile:f1 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [str writeToFile:f2 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [str writeToFile:f3 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [str writeToFile:f4 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [str writeToFile:f5 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    
    const char *path = [f1 fileSystemRepresentation];
    NSLog(@"%s", path);
}

下面看输出结果

2017-08-21 17:25:44.175894+0800 JJOC[10757:5124305] /var/mobile/Containers/Data/Application/9F04B6FB-2A0D-4719-B5BC-8DE4D89DFFB9/ReadMe.txt

这里还有几点需要注意:

结论:这个我也没用过。


五、- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;

下面我们看一下参数和返回值:

下面我们就看代码。

- (void)demoGetFileSystemRepresentation
{
    char filenameBuffer[13];
    BOOL success;
    success = [@"/mach_kernel" getFileSystemRepresentation:filenameBuffer maxLength:12];
    // success == NO
    NSLog(@"success = %d", success);
    
    // Changing the length to include the NULL character does work
    success = [@"/mach_kernel" getFileSystemRepresentation:filenameBuffer maxLength:13];
    // success == YES
    NSLog(@"success = %d", success);
}

下面我们看输出结果

2017-08-21 17:45:00.791624+0800 JJOC[10762:5126205] success = 0
2017-08-21 17:45:00.791672+0800 JJOC[10762:5126205] success = 1

这里还有几点需要注意:

结论:还算好理解吧。


六、@property(getter=isAbsolutePath, readonly) BOOL absolutePath;

这个属性的目的就是判断是否是绝对路径,它是只读的。下面我们看一下实例代码。

- (void)demoAbsolutePath
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSLog(@"path = %@", path);
    NSLog(@"是绝对路径?%d", path.absolutePath);
}

下面看输出结果

2017-08-21 18:11:22.758044+0800 JJOC[10770:5132186] path = /var/containers/Bundle/Application/E265D8CD-F47F-452B-A4C7-8651DB10ACB3/JJOC.app/1.gif
2017-08-21 18:11:22.758131+0800 JJOC[10770:5132186] 是绝对路径?1

还有几个方面需要注意:

结论:这个还是会用到的。


七、@property(readonly, copy) NSString *lastPathComponent;

这个属性就是获取路径等分隔符分开的最后的一部分。下面还是看代码。

- (void)demoLastPathComponent
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSLog(@"path = %@", path);
    NSLog(@"路径?%@", path.lastPathComponent);
}

下面看输出结果

2017-08-21 18:21:20.559206+0800 JJOC[10778:5133700] path = /var/containers/Bundle/Application/B1E0B625-F0A7-412C-9D44-D24C7BDFF0AD/JJOC.app/1.gif
2017-08-21 18:21:20.559309+0800 JJOC[10778:5133700] 路径?1.gif

还有几点需要注意:

输入 输出
/tmp/scratch.tiff scratch.tiff
/tmp/scratch scratch
/tmp/ tmp
scratch/// scratch
/ /

结论:这个需要好好理解,我总用这个。


八、@property(readonly, copy) NSString *pathExtension;

下面我们还是要先看一下代码。

- (void)demoPathExtension
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSLog(@"path = %@", path);
    NSLog(@"%@", path.pathExtension);
}

下面看输出结果

2017-08-21 18:32:47.461214+0800 JJOC[10781:5134801] path = /var/containers/Bundle/Application/85335E84-49E1-4079-AA84-1A017D801FD6/JJOC.app/1.gif
2017-08-21 18:32:47.461336+0800 JJOC[10781:5134801] gif

这里还有几点需要注意:

输入 输出
/tmp/scratch.tiff tiff
.scratch.tiff tiff
/tmp/scratch/ an empty string
/tmp/ an empty string
/tmp/scratch..tiff/ tiff

结论:这个也总会用到。

后记

未完,待续~~

上一篇 下一篇

猜你喜欢

热点阅读