Nadal Alyafaie
Nadal Alyafaie

Reputation: 329

Programmatically Auto-Layout Functions

Im having an issue with these two functions. For func 1:

  func constrainWidth(width: CGFloat) -> [NSLayoutConstraint] {
    let constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:[item(width)]", metrics: ["width" : width], views: ["item" : self])
    self.superview?.addConstraints(constraints)
    return constraints as! [NSLayoutConstraint]
}

I get an error at "self.superview?.addConstraints(constraints)" they have a problem with "constraints" and it says: cannot convert value of type [Any Object] to expected argument type '[NSLayoutConstraint]'

Here is func 2:

 class func constraintsWithVisualFormat(visualFormat: String,  options: NSLayoutFormatOptions, views: [NSObject : AnyObject]) -> [AnyObject] {
    return NSLayoutConstraint.constraintsWithVisualFormat(visualFormat, options: options, metrics: nil, views: views)
}

I get an error at "return NSLayoutConstraint.constraintsWithVisualFormat(visualFormat, options: options, metrics: nil, views: views)" they have a problem with "views" and it says: *Cannot convert value of type '[NSObject : AnyObject]' to expected argument type '[String : AnyObject]'

These function don't affect one another, i just need help with them both. I didn't have this problem before, it just came up recently i believe because of the new swift.

Upvotes: 0

Views: 308

Answers (1)

TheEye
TheEye

Reputation: 9346

For func 1:

There is the options parameter missing, it doesn't even compile in my XCode. When I add the options parameter, it compiles & runs without problems.

For func 2:

It tells exactly what is wrong - the parameter you hand over for views is defined as [NSObject : AnyObject], but it has to be [String: AnyObject].

Upvotes: 0

Related Questions