加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – NSBundle.mainBundle().URLForResource(“bach1”,with

发布时间:2020-12-14 05:48:32 所属栏目:百科 来源:网络整理
导读:NSBundle.mainBundle().URLForResource("bach1",withExtension: "jpg") 上面的代码返回null. 为了检查文件是否存在,我使用下面的代码: let fileManager = NSFileManager.defaultManager()if fileManager.fileExistsAtPath(savepath) { println("exist")} 上
NSBundle.mainBundle().URLForResource("bach1",withExtension: "jpg")

上面的代码返回null.

为了检查文件是否存在,我使用下面的代码:

let fileManager = NSFileManager.defaultManager()

if fileManager.fileExistsAtPath(savepath) {
  println("exist")
}

上面的代码返回该文件存在于目录中.

所以我不明白为什么第一个代码返回null

你的问题是NSBundle.mainBundle().URLForResource(“bach1”,withExtension:“jpg”)返回一个可选的NSURL.如果要打开它并从返回的URL中提取文件路径,则需要使用以下内容:
if let resourceUrl = NSBundle.mainBundle().URLForResource("bach1",withExtension: "jpg") {
    if NSFileManager.defaultManager().fileExistsAtPath(resourceUrl.path!) {
        print("file found")
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读