2

I've changed nothing in my project and all of a sudden I get these errors when building to my device:

'componentsWithURL(_:resolvingAgainstBaseURL:)' is unavailable: use object construction 'NSURLComponents(URL:resolvingAgainstBaseURL:)'

'componentsWithURL(_:resolvingAgainstBaseURL:)' has been explicitly marked unavailable here (Foundation.NSURLComponents)

on this line:

let urlComponents = NSURLComponents.componentsWithURL(url, resolvingAgainstBaseURL: false)

The quick help says the method (and the class, NSSURLComponents) has been available since "iOS (8.0 and later)". The docs say "Available in iOS 7.0 and later". Strange.

If I follow the directions in the error and use the constructor instead, I get compiler errors when accessing urlComponents.scheme and urlComponents.URL. Not that I had high hopes -- my code is untouched and has compiled fine with no problems for weeks.

  • I cleaned the product and the build folder
  • I reverted to a known working commit
  • I closed the project, restarted Xcode, restarted the OS, reinstalled Xcode
  • Tried running both debug and release
  • Tried changing deployment target to 8.0
  • Tried running in simulator and on device

The only thing that looks different in Xcode is that below my project name it says "ios sdk 8.1". I can only remember having seen 8.0 there before, but I've never changed it and there's no option to change it back. Don't know why it would make any difference though.

Xcode Version 6.1 (6A1052d)

Andreas
  • 2,665
  • 2
  • 29
  • 38

1 Answers1

7

As indicated in the error message, the constructor is NSURLComponents(URL:resolvingAgainstBaseURL:), in your case

let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false);

Note also that this is a "failable initializer" (now) and returns an optional. So you have to unwrap the result explicitly or test with optional binding:

if let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) {
    // Use urlComponents ..
} else {
    // failed
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382