美文网首页
URL与路径

URL与路径

作者: 愤怒小鸟飞呀飞 | 来源:发表于2018-10-17 19:13 被阅读0次

主要解释以下两者区别
[NSURL URLWithString:filePath]
[NSURL fileURLWithPath:filePath];

一、概念
URL :协议头 + 主机地址 + 资源路径 如果访问的地址是本机的,那么主机地址可以省略;路径是不用包含协议头和主机的

路径:?
二、测试

    NSDictionary *dic = @{@"name":@"xiaoqiang"};
    NSString *documentPath = [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()]; 
    NSString *filePath =  [NSString stringWithFormat:@"%@/name.plist",documentPath];
    
    NSString *fileURl =  [NSString stringWithFormat:@"file:///%@",filePath];
    //file:////Users/huihuadeng/Library/Developer/CoreSimulator/Devices/9F943056-7AD1-4A4E-B9E9-AE186A833AE6/data/Containers/Data/Application/188085CE-E77D-40CA-B419-566B2FCBD50A/Documents/name.plist
    NSURL *url1 = [NSURL URLWithString:fileURl];
//:file:///Users/huihuadeng/Library/Developer/CoreSimulator/Devices/9F943056-7AD1-4A4E-B9E9-AE186A833AE6/data/Containers/Data/Application/188085CE-E77D-40CA-B419-566B2FCBD50A/Documents/name.plist
    NSURL *url2 = [NSURL fileURLWithPath:filePath];
///Users/huihuadeng/Library/Developer/CoreSimulator/Devices/9F943056-7AD1-4A4E-B9E9-AE186A833AE6/data/Containers/Data/Appl ... me.plist
    NSURL *url3 = [NSURL URLWithString:filePath];
    
//    [dic writeToFile:filePath atomically:YES];//正常
    
//    [dic writeToURL:url1 error:nil];//正常
//   [dic writeToURL:url2 error:nil];//正常
  [dic writeToURL:url3 error:nil];//非正常

如上所述,推测 [NSURL URLWithString:filePath]; API仅仅是把string转化为URL对象,不做任何处理
[NSURL fileURLWithPath:filePath];会在路径前添加协议头 file:///

三、参考链接:https://www.jianshu.com/p/6997fd50f754

相关文章

  • URL与路径

    主要解释以下两者区别[NSURL URLWithString:filePath][NSURL fileURLWit...

  • dianjo增删改查

    url views html url配置路径 页面的跳转 转换与url的重定向

  • SpringMVC跳转和URL相关问题

    搞明白了url相对路径,根路径的问题,跳转的时候url到底该怎么写就容易理解和记忆了。 url路径的问题 1. 当...

  • 页面间通信

    url 传参 index.js 路径后可以带参数。参数与路径之间使用 ? 分隔,参数键与参数值用 = 相连,不同参...

  • ImageLoader使用Tips

    1.URL 设置不同头部对应不同URL: 网络路径 http://site.com/image.png 本地路径...

  • 应用跳转

    应用程序跳转 从weixin跳转到QQ1、给应用程序配置URL2、URL:协议头+路径(URL可以没有路径)3、相...

  • 3.nodejs的URL模块

    1.引入 var url = rquire("url") 2.相关方法使用 url.parse("地址路径" , ...

  • 04.背景图像效果

    背景图像效果: 1 URL路径: 1.1 在通过 link 标签引入css文件时,在css中使用 url 路径应...

  • 对url路径中的参数进行加密

    url路径参数加密 测试结果

  • vue-router的实现原理

    URL:协议://主机:端口/路径?查询 URL: scheme://localhost:port/path?qu...

网友评论

      本文标题:URL与路径

      本文链接:https://www.haomeiwen.com/subject/sxguoftx.html