Peter71
Peter71

Reputation: 2300

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?

Upvotes: 4

Views: 4203

Answers (2)

holex
holex

Reputation: 24041

it makes absolutely zero sense to do so, but currently (Swift3, Xcode Version 8.0 (8A218a)) it is working and gives you a totally blank URL object with no purpose at all, as you just asked for.

var myURL: URL = NSURLComponents().url!

Upvotes: 4

Christoph
Christoph

Reputation: 722

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.

Upvotes: 3

Related Questions