6 Replies
      Latest reply on Mar 25, 2020 11:53 PM by hippoX
      tommyfromgotha Level 1 Level 1 (0 points)

        I'm working on swift code that should create an instance of URLComponents. At one point the instance was successfully created. Now URLComponents always returns nil. I have tried two forms of URLComponents to create the instance.

         

        var urlComps: URLComponents = URLComponents()

        var urlComps: URLComponents = URLComponents(string: path)

        where path contains a valid url path.  In both cases urlComps was nil.

         

        I created a new project and tried the code there and URLComponents returned nil instead of a new instance of the class.

         

        I put the URLComponents related lines of code in a playground and it worked fine.  urlComps pointed to an instance of URLComponents.

         

        I'm using Xcode 11 and the swift 5 compiler built for iOS 13. I'm developing on a Macbook Pro running macOS 10.14.6, Mojave. Here is the code as it is currently written.

         

          public func get(path: String, queryParams: [String: String]) -> [String: String] {

                var response: [String: String]

                var urlComps: URLComponents = URLComponents()

                urlComps.path = "/user/auser"

                let URL = urlComps.url

                response = api(path: URL!, data: [:])

                if(!response.isEmpty) {

                    return response

                } else {

                    return [:]

                }

            }

        • Re: URLComponents returns nil instance
          OOPer Level 8 Level 8 (5,715 points)

          I cannot make `URLComponents.init()` return nil.

          How have you checked that the result was nil? Isn't that just the debugger's malfunctioning?

            • Re: URLComponents returns nil instance
              tommyfromgotha Level 1 Level 1 (0 points)

              I have checked that the result is nil and if I let the simulater continue it will fail.

                • Re: URLComponents returns nil instance
                  OOPer Level 8 Level 8 (5,715 points)

                  I have checked that the result is nil

                  Please explain how. You inserted `print`? Or you have checked any of the variables with debugger?

                   

                  if I let the simulater continue it will fail.

                  That does not always mean `URLComponents` is nil.

                   

                  If you really want to solve your issue, please show enough info to solve it.

                  The code you have shown finishes without any problems on my simulator of Xcode 11. (With dummy `api(path:data:)`.

                  You may not be showing the exact code which causes the issue, or there may be something wrong somewhere currently not shown.

                    • Re: URLComponents returns nil instance
                      tommyfromgotha Level 1 Level 1 (0 points)

                      OK, now it is working when I initialize urlComps and then initialize urlComps.path with the following two lines.

                      var urlComps: URLComponents = URLComponents()

                      urlComps.path = "/user/auser"

                      I printed it in the console using control click (Print Description of "urlComps") or mousing over urlComps in the source code and clicking the i icon shows the value for path.  This is also the method I used when urlComps was showing nil.

                      Printing description of urlComps:

                      ▿ /user/auser

                        - path : "/user/auser"

                       

                      It also works as

                      var urlComps = URLComponents()

                       

                      It must have been my error but before posting the initial question, I tried these and many ways of creating the instance.  They were all producing nil but are now working.  Even as I was writing my response some failed that now work. I know code does not magically start working but unfortunately I didn't catch whatever I did to fix this.

                       

                      Thank you for your response.  I'll be sure to provide much greater detail with questions in the future.

                        • Re: URLComponents returns nil instance
                          OOPer Level 8 Level 8 (5,715 points)

                          It must have been my error

                          Possibly, but not necessarily. Xcode has some bugs when showing values of Swift types, all the method you have tried may show some false  content. That's why I once suggested Isn't that just the debugger's malfunctioning? and insisted Please explain how.

                           

                          I guess you had some bugs in other parts of your code, and the false info by the Xcode had made you confused, but accidentally you fixed it.

                          Just my guess. Next time (if any) I believe we can find the real reason.

                  • Re: URLComponents returns nil instance
                    hippoX Level 1 Level 1 (0 points)

                    Same as my problem.