Peter71 Peter71 - 1 year ago 161
Swift Question

Swift 3: NSURL to URL missing init?

I just used XCode 8 and let it convert my existing project. Now I face the error that there is no init function for new URL without parameters.

class fileObj : NSObject, NSCopying, Comparable {
var URL = NSURL() // initial
...


The new code looks like:

class fileObj : NSObject, NSCopying, Comparable {
var myUrl = Foundation.URL() // initial
...


How should I init the new URL var?

Answer Source

Your observation is indeed correct, there is no empty initialiser because this would be an invalid URL, therefore they decided to disallow that.

What I recommend you doing is not initialising the variable at first and making it an optional (URL?). Later in the code, you'll be able to initialise it.