文件路徑相關的方法在NSPathUtilities中,主要是操作路徑
獲得一個路徑
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //獲得Document的路徑//---~~~/Application/43A22272-F08C-4078-93A9-CAF451BF9814/Documents //~~~是省略寫法NSArray *components = [documents pathComponents]; //路徑的每個節點的元素"/", Users, admin, Library, Developer, CoreSimulator, Devices, "87AB942E-1A11-479C-8010-1F5C36646658", data, Containers, Data, Application, "43A22272-F08C-4078-93A9-CAF451BF9814", DocumentsNSString *lastPathComponent = [documents lastPathComponent]; //最後的一個路徑節點元素 //---DocumentsNSString *stringByDeletingLastPathComponent = [documents stringByDeletingLastPathComponent];//刪除最後一個路徑節點元素 //---~~~/Application/3B14DAE5-8890-42E1-8DC1-664BB35B6333NSString *stringByAppendingPathComponent = [documents stringByAppendingPathComponent:@"new.txt"]; //添加一個路徑元素 //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/new.txtNSString *pathExtension = [stringByAppendingPathComponent pathExtension]; //文件的後綴名 //---txtNSString *stringByDeletingPathExtension = [stringByAppendingPathComponent stringByDeletingPathExtension]; //去掉後綴名 //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/newNSString *stringByAppendingPathExtension = [stringByDeletingPathExtension stringByAppendingPathExtension:@"txt"]; //添加後綴名 //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/new.txtNSString *stringByAbbreviatingWithTildeInPath = [documents stringByAbbreviatingWithTildeInPath]; //變成相對路徑,前面的都用波浪號省略 //--- ~/DocumentsNSString *stringByExpandingTildeInPath = [stringByAbbreviatingWithTildeInPath stringByExpandingTildeInPath]; //把相對路徑變成絕對路徑NSArray *stringsByAppendingPaths = [documents stringsByAppendingPaths:@[@"a",@"b",@"c.txt"]]; //批量添加多個路徑,得到一個路徑數組 //---~~~/Application/16474B22-B287-4F7C-9931-E7454B83822E/Documents/a //---~~~/Application/16474B22-B287-4F7C-9931-E7454B83822E/Documents/b //---~~~/Application/16474B22-B287-4F7C-9931-E7454B83822E/Documents/c.txt