Convert String to URL (Why is resulting variable nil)

前端 未结 1 1519
后悔当初
后悔当初 2020-11-29 11:19

I\'m trying to create an URL variable from a string value. I don\'t understand why the resulting URL is nil

I have set up a new Xcode macOS project, placed a simple

相关标签:
1条回答
  • 2020-11-29 12:15

    You've found a bug in the debugger! It's easy to reproduce it:

    We have paused at a breakpoint inside the condition. So obviously url is not nil or we wouldn't be here at all.

    Another way to prove this is to po url in the console (see right-bottom of this screen shot):

    Nevertheless, url shows as nil both in the tooltip and in the variables list. So the debugger is just lying to you: url is not nil. Don't worry, be happy. Your code is working fine.

    EDIT The bug has something to do with the Swift Foundation overlay. If you change the declaration of url to this:

    let url = NSURL(string: urlAsString)
    

    ...then everything works as expected.

    And see also https://stackoverflow.com/a/58156592/341994

    0 讨论(0)
提交回复
热议问题