let myPath = Bundle.main.path(forResource: "Settings", ofType: ".png")
print(myPath!)
当我尝试打印它时,为什么它会崩溃?
崩溃是著名的意外发现的 nil
。除非保证该值不是nil
。
该文件不存在,或者(最有可能)您的 type (扩展)是png
不是.png
let myPath = Bundle.main.path(forResource: "Settings", ofType: "png")
但是,如今与URL相关的API是可取的
let myURL = Bundle.main.url(forResource: "Settings", withExtension: "png")
我的简单猜测是我的路径是nil,所以它在零指针异常时崩溃。删除感叹号并使用:
print(myPath)
如果它打印了零,则有答案。