版本記錄
版本號 | 時間 |
---|---|
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;
很多解釋可以從官方文檔或者示例代碼給出。
下面看一下參數。
-
outputName
:包含滿足所有條件的最長的路徑。 -
flag
:如果是YES
就考慮路徑中的大小寫字母,反之不考慮。 -
outputArray
:輸出數組,包含滿足條件的所有子文件夾路徑。 -
filterTypes
:過濾滿足添加的輸出文件類型,如果傳遞為nil,表示不關心文件類型,也就是說可以輸出文件類型。
這個方法的作用其實就是:找到滿足一定條件的文件擴展名,例如,給定一個文件夾~/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")
上面這個例子,限制了文件類型為txt
和rtf
,所以找到的子文件類型也就是這兩種類型的。
還有的地方需要注意:
- 對于返回值,如果返回0,表示沒有找到;如果返回1,表示只能找到一個,如果很多條件都滿足匹配,那么返回的就是匹配的個數。
- 您可以通過將NULL作為outputArray檢查是否存在匹配,而無需檢索。請注意,此方法僅適用于文件路徑(不是例如URL的字符串表示)。
結論:很少用,但是值得一看。
四、@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
這里還有幾點需要注意:
- 返回的C字符串將被自動釋放,因為返回的對象將被釋放;如果需要在這個字符串生成的內存上下文以外存儲這個結果表達式,您的代碼應該復制這個結果表達式或使用方法
getFileSystemRepresentation:maxLength:
。 - 如果接收者無法在文件系統編碼中表示或者接收者沒有字符,則會引發
NSCharacter ConversionException
異常。 - 請注意,此方法僅適用于文件路徑(不適用于URL的字符串表示)。要轉換char *路徑(例如可以從C庫例程獲得)到NSString對象,請在
NSFileManager
上使用stringWithFileSystemRepresentation:length:method
方法。
結論:這個我也沒用過。
五、- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;
下面我們看一下參數和返回值:
-
cname
:它是一個字符類型的指針,返回時,包含表示接收器的C字符串,即為與系統無關的路徑加上NULL終止字節。 緩沖區的大小必須足夠大以容納maxLength
字節。 -
max
:緩沖區中返回的字符串中的最大字節數(包括終止NULL
字符,此方法添加)。 -
return
:如果緩沖區成功的被填充了文件系統表示,則為YES,否則為NO(例如,如果超過maxLength或者文件系統編碼中無法表示接收器)。
下面我們就看代碼。
- (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
這里還有幾點需要注意:
- 該方法通過將抽象路徑和擴展分隔符(分別為'/'和'.')替換為操作系統的等效項來操作。 如果特定于系統的路徑或擴展分隔符以抽象表示形式出現,則將其轉換為依賴于系統的字符(除非它們與抽象分隔符相同)。
- 請注意,此方法僅適用于文件路徑(不適用于URL的字符串表示)。
- 上面的示例說明了使用
maxLength
參數。 第一個方法調用返回失敗,因為字符串(@“/ mach_kernel”)的文件表示為12個字節長,并且作為maxLength參數(12)傳遞的值不允許添加NULL終止字節。
結論:還算好理解吧。
六、@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
還有幾個方面需要注意:
- 請注意,此方法僅適用于文件路徑(不是,例如,URL的字符串表示)。 該方法不會檢查文件系統是否存在該路徑(對于該任務,在
NSFileManager
中使用fileExistsAtPath
:或類似方法)。
結論:這個還是會用到的。
七、@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
這里還有幾點需要注意:
- 請注意,此方法僅適用于文件路徑(不是例如URL的字符串表示)。
- 路徑擴展是最后一個路徑組件的最后一個周期的部分,如果有的話。 擴展分隔符不包括在內。 下表說明了
pathExtension
對各種不同路徑的影響:
輸入 | 輸出 |
---|---|
/tmp/scratch.tiff | tiff |
.scratch.tiff | tiff |
/tmp/scratch/ | an empty string |
/tmp/ | an empty string |
/tmp/scratch..tiff/ | tiff |
結論:這個也總會用到。
后記
未完,待續~~