[swift-users] NSURLComponents - setting scheme bug?

Brent Royal-Gordon brent at architechies.com
Wed May 25 19:58:32 CDT 2016


>> var components = NSURLComponents(string: "apple.com")!
> 
> That’s incorrect, and I’m surprised the method didn’t fail, since “apple.com” is not a valid URL. Try initializing an empty object and then setting its host to “apple.com”.

"apple.com" is a perfectly valid relative URL, and that's how NSURLComponents interpreted it:

  1> import Foundation
  2> let comps = NSURLComponents(string: "apple.com")!
  3> comps.description
$R1: String = "<NSURLComponents 0x100707670> {scheme = (null), user = (null), password = (null), host = (null), port = (null), path = apple.com, query = (null), fragment = (null)}"

Note that `host` is nil, but `path` is "apple.com".

Your suggestion to initialize an empty `NSURLComponents` and set the `host` field is a good one, however.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-users mailing list