首页 > 代码库 > stringByAppendingPathComponent和stringByAppendingString 的区别

stringByAppendingPathComponent和stringByAppendingString 的区别

 

stringByAppendingPathComponent和stringByAppendingString 的区别

 

stringByAppendingPathComponent

 

 
  1. NSString * filePath3 = [documentsPath stringByAppendingPathComponent:@"Dictionary"];  

 

stringByAppendingString

 


 
  1. NSString * filePath = [documentsPath stringByAppendingString:@"/hello.txt"];  

两者比较  

1、后者需要加"/" ,前者不需要加

2、前者路径打印出来:/Users/MyMacBook/Library/Developer/CoreSimulator/Devices/C1B4AFE9-2058-40A6-B868-101E166FF961/data/Containers/Data/Application/79CB8A9F-1000-4360-A9EA-801ADF1E0044/Documents/DataBase.sqlite
      后者路径打印出来:/Users/MyMacBook/Library/Developer/CoreSimulator/Devices/C1B4AFE9-2058-40A6-B868-101E166FF961/data/Containers/Data/Application/79CB8A9F-1000-4360-A9EA-801ADF1E0044/Documents

 

stringByAppendingPathComponent

就是将前面的路径格式和后面的普通的字符串格式链接在一起,并且以路径格式返回。

例如:获取程序家目录NSString *path =NSHomeDirectory();获取后的path是"/root/User/";执行

path = [path stringByAppendingPathComponent:@"abc.txt"];后path为“/root/User/abc.txt”。




stringByAppendingPathComponent和stringByAppendingString 的区别