犀利吐槽
1.同樣都是“文件和目錄操作",java中,就用java.util.File一個類,就封裝了很多API,而Object-C搞了這麼多類和函數。具體原因,有待分析啊。
2.明明是NSString,字符串操作,怎麼出現了”pathComponents“等操作文件路徑相關的方法,很奇怪的趕腳。
3.stringByAppendingString,這函數的名字有點長啊。
4.總體感覺,Object-C的語法比Java復雜一些,碼代碼的效率低了不少。
/
/ // main.m // FilePathUtil // // Created by fansunion on 15/11/29. // Copyright (c) 2015年 demo. All rights reserved. // #import//演示文件路徑API int main(int argc, const char * argv[]) { @autoreleasepool { NSString *fileName =@"path.m"; NSFileManager *fm; NSString *path,*tempDir,*extention,*homeDir,*fullPath; NSArray *components; fm =[NSFileManager defaultManager]; //臨時目錄 tempDir = NSTemporaryDirectory(); NSLog(@"The tempDir is %@",tempDir); //提取基本目錄 path =[fm currentDirectoryPath]; NSLog(@"The base dir is %@",[path lastPathComponent]); //fileName在當前目錄中的完整路徑 //這個地方有個問題 //本地輸出”/Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m“ //Debug和path.m之間沒有”分隔符“/",而書本中的例子是有的 //最好還是手動加上,Java中也是沒有這個分隔符,需要手動加上的 fullPath =[path stringByAppendingString:fileName]; NSLog(@"The fullPath is %@",fullPath); //獲得文件擴展名 extention = [fullPath pathExtension]; NSLog(@"The extentions is %@",extention); //獲得用戶的主目錄 homeDir = NSHomeDirectory(); NSLog(@"The home directory is %@",homeDir); //拆分路徑為各個組成部分 components = [homeDir pathComponents]; for(path in components){ NSLog(@"%@",path); } } return 0; }
程序輸出
2015-11-29 13:43:30.550 FilePathUtil[2861:179163] The tempDir is /var/folders/4q/5ylpds9n5n97bq_r41qvly4w0000gn/T/
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The base dir is Debug
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The fullPath is /Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The extentions is m
2015-11-29 13:43:30.552 FilePathUtil[2861:179163] The home directory is /Users/fansunion
2015-11-29 13:43:30.552 FilePathUtil[2861:179163] /
2015-11-29 13:43:30.552 FilePathUtil[2861:179163] Users
2015-11-29 13:43:30.553 FilePathUtil[2861:179163] fansunion
Program ended with exit code: 0