Func fileExists finds .json but not .jpg files

The func fileExists finds .json file but not .jpg files in the same directory!

(User-names were find/replaced with “Name” & “name”)

What am I missing?

This new swift user Thanks You!


CODE:


func fileExists(url: URL) -> Bool {

do {

let fileManager = FileManager.default

try fileManager.fileExists(atPath: String(contentsOf: url))

} catch {

return false

}

return true

}



fileprivate func _guaranteeImage(name: String) -> _ImageDictionary.Index {

if let index = images.index(forKey: name) { return index }

if fileExists(url: trailDataURL) {print("\(trailDataURL)")}

let url = trailFileURL.appendingPathComponent("\(name).jpg")//TODO not working!

print("\(url)")

if fileExists(url: url) {

images[name] = ImageStore.reloadImage(url: url)

} else {

images[name] = ImageStore.loadImage(name: name)

}

return images.index(forKey: name)!

}


DIRECTORY form terminal:


Name-iMac:~ name$ cd ///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents

Name-iMac:Documents name$ ls -l

total 848

-rw-r--r-- 1 name staff 63243 Feb 5 16:49 chilkoottrail.jpg

-rw-r--r-- 1 name staff 52721 Feb 5 15:08 margarette-falls.jpg

-rw-r--r-- 1 name staff 309101 Feb 5 16:48 silversalmoncreek.jpg

-rw-r--r-- 1 name staff 1249 Feb 6 07:41 trailData.json

Name-iMac:Documents name$




PRINT RESULTS (from iPhone 8 Simulator):


file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/D00EAD3D-B118-4C8B-A90D-177D9419D989/LocalTrails.app/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/D00EAD3D-B118-4C8B-A90D-177D9419D989/LocalTrails.app/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/0AF1262C-7894-4408-A873-D91B17A3B6E4/Documents/chilkoottrail.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/D00EAD3D-B118-4C8B-A90D-177D9419D989/LocalTrails.app/chilkoottrail.jpg


CODE (only logic reversed then App re-run):


if !fileExists(url: url) {


PRINT RESULTS (from iPhone 8 Simulator):


file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/trailData.json

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/chilkoottrail.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5FEF88CA-E84E-4F4B-981B-EEDDF061FB95/Documents/chilkoottrail.jpg

Additional info:

The .jpg files were copied in terminal from the project-resources to the App-directory, but the .json file was put there by the App.

There is a fileExists problem (at least for me)!


Had to work around this fileExists problem by ignoring the supposed non-existence of the .jpg files, and by changing myloadDirImage to return nil when it can’t load the files.


Relavent code:


func fileExists(url: URL) -> Bool {

do {

let fileManager = FileManager.default

try fileManager.fileExists(atPath: String(contentsOf: url))

} catch {

return false

}

return true

}


fileprivate func _guaranteeImage(name: String) -> _ImageDictionary.Index {

if let index = images.index(forKey: name) { return index }

let url = trailFileURL.appendingPathComponent("\(name).jpg")

//fileExists is unable to fine .jpg files in the App Directory!!! TODO

if fileExists(url: trailDataURL) {print("\(trailDataURL) exists")} else {print("\(trailDataURL) NOexists")}

if fileExists(url: url) {print("\(url) exists")} else {print("\(url) NOexists")}


let image = ImageStore.loadDirImage(url: url)

if image != nil {

images[name] = image

} else {

images[name] = ImageStore.loadImage(name: name)

ImageStore.copyImage(name: name)

}

return images.index(forKey: name)!

}


fileExists Can’t find the .jpgs:


file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/margarette-falls.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/silversalmoncreek.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/chilkoottrail.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5CD9C0D0-12E5-426B-8C9A-D31B9D40AB8C/Documents/chilkoottrail.jpg


But the .jpgs were there, and then deleted:


Name-iMac:Documents name$ ls -l

total 848

-rw-r--r-- 1 name staff 63243 Feb 5 15:14 chilkoottrail.jpg

-rw-r--r-- 1 name staff 52721 Feb 5 15:13 margarette-falls.jpg

-rw-r--r-- 1 name staff 309101 Feb 5 15:13 silversalmoncreek.jpg

-rw-r--r-- 1 name staff 1249 Feb 6 07:41 trailData.json

Name-iMac:Documents name$ rm *.jpg

Name-iMac:Documents name$ ls -l

total 8

-rw-r--r-- 1 name staff 1249 Feb 6 07:41 trailData.json

Name-iMac:Documents name$


No .jpgs in Documents, so the app loads from the Bundle and then copys then to Documents.



file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/margarette-falls.jpg NOexists

load file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/margarette-falls.jpg

copy file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/margarette-falls.jpg -> file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/silversalmoncreek.jpg NOexists

load file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/silversalmoncreek.jpg

copy file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/silversalmoncreek.jpg -> file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/chilkoottrail.jpg NOexists

load file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/chilkoottrail.jpg

copy file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Bundle/Application/7DF72A83-2F9E-4692-BF68-C715960CD2AF/LocalTrails.app/chilkoottrail.jpg -> file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/DE02E870-B97D-477A-9EBE-6F65F81EC691/Documents/chilkoottrail.jpg


Name-iMac:Documents name$ ls -l

total 848

-rw-r--r-- 1 name staff 63243 Feb 5 15:14 chilkoottrail.jpg

-rw-r--r-- 1 name staff 52721 Feb 5 15:13 margarette-falls.jpg

-rw-r--r-- 1 name staff 309101 Feb 5 15:13 silversalmoncreek.jpg

-rw-r--r-- 1 name staff 1249 Feb 6 07:41 trailData.json

Name-iMac:Documents name$


fileExists Can’t find the .jpgs that the App copied to Documents:


file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/margarette-falls.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/margarette-falls.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/silversalmoncreek.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/silversalmoncreek.jpg

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/trailData.json exists

file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/chilkoottrail.jpg NOexists

loadDir file:///Users/name/Library/Developer/CoreSimulator/Devices/3AD300AB-EA56-4877-AD85-C117E5CE240F/data/Containers/Data/Application/5606123A-7240-4692-9B67-CCE8BAE09CCC/Documents/chilkoottrail.jpg

What happens when you put a jpeg in the Directory by the app ?


I would bet it is seen.


If so, that hints it is a question or rights to the directory.

hi,


you wrote this for a function


func fileExists(url: URL) -> Bool {
    do {
        let fileManager = FileManager.default
        try fileManager.fileExists(atPath: String(contentsOf: url))
    } catch {
        return false
    }
    return true
}


i'm not sure what you're asking here. FileManager.default.fileExists(atPath:) does not throw -- it simply returns true or false.


instead, you're asking to read the String content of the incoming url to determine the filepath, with String(contentsOf: url). the file may well exist, but String(contentsOf: url) will probbaly throw for a jpg file.


don't you just want the following?


func fileExists(url: URL) -> Bool {
  return FileManager.default.fileExists(atPath: url.path)
}


some clarification may be needed going forward, here.


hope that helps,

DMG

For me atleast, “FileManager.fileExists” is finding my .json file in the App-directory but but NOT the .jpg files. I have solved by problem by not using it for the .jpg files.


But there appears to be a problem with “FileManager.fileExists”. The problem is made clearer in my Feb 6, 2020 11:29 AM post.


I don’t recommend that anybody use my Swift coding as an example to follow.

But there appears to be a problem with

FileManager.fileExists
. The problem is made clearer in my Feb 6, 2020 11:29 AM post.

No,

fileExists(atPath:)
works just fine [1]. I’m sorry to say this but the code you posted on 6 Feb is simply wrong. I’m including a copy here, formatted as code (using the
<>
button), so I can reference the line numbers:
func fileExists(url: URL) -> Bool {
    do {
        let fileManager = FileManager.default
        try fileManager.fileExists(atPath: String(contentsOf: url))
    } catch {
        return false
    }
    return true
}

The specific problem is line 4. It should look like this [2]:

return fileManager.fileExists(atPath: url.path)

Your code is trying to load a string from the file at the URL. As you might imagine, this works if the file contains JSON and fails if the file contains a JPEG image. So, there are three cases here:

  • The file does not exist — In this case the

    String(contentsOf:)
    call fails with a ‘file not found’ error. This failure throws a Swift error. This is caught at line 6, which then returns false. This is the correct answer, but only by coincidence. You never even called
    fileExists(atPath:)
    in this case.
  • A string-like file exists — In this case the

    String(contentsOf:)
    call works. Your code then calls
    fileExists(atPath:)
    with completely the wrong path (it uses the contents of the JSON file as a path!), but that doesn’t matter because it ignores the result from that function. It then falls through to line 8 and returns true. Again, you have the correct answer, but only by coincidence.
  • The file exists but is not string-like — In this case the

    String(contentsOf:)
    fails with a ‘data format error’. This failure throws a Swift error like in the first case, but here the resulting false is incorrect.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Well, it has intrinsic TOCTTOU problems, but those are not relevant to this discussion.

[2] Once you make this change there’s no need for the

do
/
catch
, so the whole function boils down to this:
func fileExists(url: URL) -> Bool {
    return FileManager.default.fileExists(atPath: url.path)
}
Func fileExists finds .json but not .jpg files
 
 
Q