-1

I like to layout constraints in code.. but for some reason my latest swift project is throwing errors my way.. my constraint code is this.

    let views = ["optionsView":optionsView, "cameraView":cameraView, "imageView":imageView]
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[optionsView(300)]-[cameraView(300)]-[imageView(300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[optionsView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[cameraView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[imageView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))

For all four constraints, Xcode throws an error, telling me that the last function parameter - views - is invalid. The error message is thus:

'String' is not identical to 'NSObject'

any ideas?? plz and thank you

DanMoore
  • 621
  • 1
  • 8
  • 14

1 Answers1

1

The problem is NSLayoutFormatOptions(0). Just say nil.

Oh, also, all your views are Optionals. You need to unwrap all of them!

let views = ["optionsView":optionsView!, "cameraView":cameraView!, "imageView":imageView!]
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    right on! but.. no cigar, this isn't the solution I'm looking for. Do you think this is an Xcode 6 problem?? I use this function all the time and I'm using basic UIViews.. – DanMoore Dec 23 '14 at 03:37
  • 1
    Okay, got it. It's the Optionals. – matt Dec 23 '14 at 03:37
  • optionals? you mean I can't use optional variables? – DanMoore Dec 23 '14 at 03:37
  • 1
    Do you know Swift? Do you understand what an Optional is? – matt Dec 23 '14 at 03:38
  • yes, Im giving the dictionary weak references that are optionals - that I set in viewDidLoad. I cannot use optionals? – DanMoore Dec 23 '14 at 03:39
  • 1
    Did you even look at the code I've given you? I've shown you what to do. – matt Dec 23 '14 at 03:40
  • This is a good use case for you to write a bug report to Apple. The Swift compiler error message here was totally off the mark! – matt Dec 23 '14 at 03:44
  • However, I should add, it would also be good for you to learn Swift before using it any further; an Optional containing a UIView is not a UIView, and that fact that you don't know that, and that you don't understand the notion of unwrapping, is a very serious conceptual hole. – matt Dec 23 '14 at 18:27