简体   繁体   中英

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.

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM