NSBundle.mainBundle().URLForResource(“bach1”, withExtension: “jpg”) returning null

后端 未结 5 699
执笔经年
执笔经年 2021-01-02 10:28
NSBundle.mainBundle().URLForResource(\"bach1\", withExtension: \"jpg\")

The above code is returning null.

In order to check if the file exi

5条回答
  •  醉话见心
    2021-01-02 10:49

    Your problem is that NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") returns an optional NSURL. You need to use if let to unwrap it and extract your file path from the returned url as follow:

    if let resourceUrl = NSBundle.mainBundle().URLForResource("bach1", withExtension: "jpg") {
        if NSFileManager.defaultManager().fileExistsAtPath(resourceUrl.path!) {
            print("file found")
        }
    }
    

提交回复
热议问题