iOS文件路径相关的方法

文件路径相关的方法在NSPathUtilities中,主要是操作路径

得一个路径

 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //获得Document的路径<pre name="code" class="objc">//---~~~/Application/43A22272-F08C-4078-93A9-CAF451BF9814/Documents  //~~~是省略写法
 

 
NSArray *components = [documents pathComponents]; //路径的每个节点的元素<pre name="code" class="objc">     
     "/",
     Users,
     admin,
     Library,
     Developer,
     CoreSimulator,
     Devices,
     "87AB942E-1A11-479C-8010-1F5C36646658",
     data,
     Containers,
     Data,
     Application,
     "43A22272-F08C-4078-93A9-CAF451BF9814",
     Documents
 
NSString *lastPathComponent = [documents lastPathComponent]; //最后的一个路径节点元素
    //---Documents
 
NSString *stringByDeletingLastPathComponent = [documents stringByDeletingLastPathComponent];//删除最后一个路径节点元素
    //---~~~/Application/3B14DAE5-8890-42E1-8DC1-664BB35B6333
NSString *stringByAppendingPathComponent = [documents stringByAppendingPathComponent:@"new.txt"]; //添加一个路径元素
    //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/new.txt
NSString *pathExtension = [stringByAppendingPathComponent pathExtension];  //文件的后缀名
    //---txt
NSString *stringByDeletingPathExtension = [stringByAppendingPathComponent stringByDeletingPathExtension]; //去掉后缀名
    //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/new
NSString *stringByAppendingPathExtension = [stringByDeletingPathExtension stringByAppendingPathExtension:@"txt"]; //添加后缀名
    //---~~~/Application/20FED3FC-859A-4C56-945A-FFEEB17A93A2/Documents/new.txt
NSString *stringByAbbreviatingWithTildeInPath = [documents stringByAbbreviatingWithTildeInPath];  //变成相对路径,前面的都用波浪号省略
    //--- ~/Documents
NSString *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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值