swift – NSBundle.mainBundle().URLForResource(“bach1”,withExtension:“jpg”)返回null

swift – NSBundle.mainBundle().URLForResource(“bach1”,withExtension:“jpg”)返回null,第1张

概述NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") 上面的代码返回null. 为了检查文件是否存在,我使用下面的代码: let fileManager = NSFileManager.defaultManager()if fileManager.fileExistsAtPath(savepath) { pr
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")    }}
总结

以上是内存溢出为你收集整理的swift – NSBundle.mainBundle().URLForResource(“bach1”,withExtension:“jpg”)返回null全部内容,希望文章能够帮你解决swift – NSBundle.mainBundle().URLForResource(“bach1”,withExtension:“jpg”)返回null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1051488.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存