0

I have the following code:

document?.open { success in
        if success {
            print("file opened")
            self.consumed = self.document?.consumed
        } else {
            print("file read failure")
        }
    } 

As can be seen, I have a print for both success and failure. But neither prints.

The reasonable question is the file is not there, not readable or something. Therefore, I have added before this code the following:

if document != nil {
        print("not nil")
        let asString = document?.fileURL.path
        let toast = FileManager.default.fileExists(atPath: asString!)
        if toast {
            print("file exist")
        } else {
            print("file does not exist")
        }
    } else {
        print("nil")
    }
 if FileManager.default.isReadableFile(atPath: (document?.fileURL.path)!)
    {
        print("readable")
    }
    else
    {
        print("not readable")
    }

This indicates for me that it is: not nil, exists, and readable.

Is there sometime I am missing to have this completion code not execute? It would be great to be able to open the file, but I think the fact that it is not operating is the first step.

Of note about the file. It is a file that is read from the bundle and copied to Documents directory. I have looked at the attributes of the file and have found nothing odd.

jz_
  • 338
  • 2
  • 14
  • The method `open` may be buggy. – OOPer Aug 03 '18 at 12:49
  • either your `document` is _actually_ `nil` or you are using a _beta_ SDK what cannot be considered reliable because it is _beta_. – holex Aug 03 '18 at 13:16
  • But my test proved it is not nil. – jz_ Aug 03 '18 at 13:28
  • The code crashes reliably if `document` is `nil`. – vadian Aug 03 '18 at 13:34
  • One more additional question. Could it be an issue with the copy? I was looking for some from of release to make sure the file is available. That does not seem to be necessary in Swift. Regardless, I am suspicious that the copy is not completing or somehow corrupting the file. However, so far evaluation of attributes shows nothing, – jz_ Aug 03 '18 at 13:57

1 Answers1

0

Try to replace document?.open with document!.open, or use a guard let to really make sure document is not nil. Cause if document is nil the whole call is being skipped.

Here you can read up on Optional Chaining.

Fabian
  • 5,040
  • 2
  • 23
  • 35
  • document!.open give no different behavior. Not sure I know how to use the guard with a completionHandler. – jz_ Aug 03 '18 at 13:47
  • @jz_ Hm thats weird. The guard would just have checked that document is not nil. What type is document? – Fabian Aug 03 '18 at 13:49
  • Purpose,document is a JSON file saved as a custom UTI format. Is that what you are asking. I guess, one check would be to go back to a JSON ext and read that way, See if the custom designation is the problem. – jz_ Aug 03 '18 at 14:00
  • You lost me on UTI format, but if the open does custom opening stuff that could be the culprit too of course. – Fabian Aug 03 '18 at 14:02
  • Basically you can declare your own extension type. – jz_ Aug 03 '18 at 14:03
  • And open() parses that for you? If it works by parsing as JSON it could limit the scope of what could have gone wrong. – Fabian Aug 03 '18 at 14:04
  • If it would execute. But it does not even begin to execute. I am following a lecture from the Stanford iTunes series. Seems to work there, but does not even execute for me. – jz_ Aug 03 '18 at 14:06
  • @jz_ That sounds pretty frustrating. I found reports from 2013 and 2015 where happened sth similar, but whether its related or not dunno. Good luck! – Fabian Aug 03 '18 at 14:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177357/discussion-between-jz-and-purpose). – jz_ Aug 03 '18 at 14:12