Tuesday, December 23, 2014

Using reserved word in Swift methods

If you attempt to call a method that uses a Swift reserved word in its method name, you will get an error. For example, the NSURLProtectionSpace class has an initializer that takes in an argument with the protocol label:

 var protectionSpace = NSURLProtectionSpace(
            host: url,
            port: 80,
            protocol: "http",
            realm: nil,

            authenticationMethod: nil)

As the protocol is a Swift reserved word, this will cause a problem for the compiler. To fix this, enclose the protocol word with a pair of back-quotes (``), like this:

 var protectionSpace = NSURLProtectionSpace(
            host: url,
            port: 80,
            realm: nil,

            authenticationMethod: nil)

No comments: