在研究NSFileManager的时候,涉及到文件路径,发现stringByAppendingPathComponent和stringByAppendingString的输出结果一样例如:
- NSString *homeDir=NSHomeDirectory();
- NSString *desktopTxt= [NSHomeDirectory() stringByAppendingPathComponent:@"/Desktop/test.txt"];
- NSString *desktopTxt2=[NSHomeDirectory() stringByAppendingString:@"/Desktop/test2.txt"];
-
- NSLog(@"homeDir=%@",homeDir);
- NSLog(@"desktopTxt=%@",desktopTxt);
- NSLog(@"desktopTxt2=%@",desktopTxt2);
复制代码 输出结果一样,都是:
- 2016-04-08 13:15:41.365 文件管理[1280:82126] homeDir=/Users/lf
- 2016-04-08 13:15:41.365 文件管理[1280:82126] desktopTxt=/Users/lf/Desktop/test.txt
- 2016-04-08 13:15:41.365 文件管理[1280:82126] desktopTxt2=/Users/lf/Desktop/test2.txt
- Program ended with exit code: 0
复制代码 仔细研究发现,stringByAppendingPathComponent会判断字符串,自动转换为路径,例如:
- NSString *desktopTxt= [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/test.txt"];
复制代码 字符串前面不加“/”,输出结果一样,仍旧为路径
- 2016-04-08 13:15:41.365 文件管理[1280:82126] desktopTxt=/Users/lf/Desktop/test.txt
复制代码 如果是stringByAppendingString少了“/”,则输出结果为:
- 2016-04-08 13:25:52.988 文件管理[1293:87318] desktopTxt2=/Users/lfDesktop/test2.txt
复制代码 路径不正确了。
|
|