现在我正在开发swift 2.1但是我已经在swift 2.0上创建了我的项目,我的代码正在使用swift 2.0但是当我尝试在最新的swift中进行代码转换时我无法理解如何转换请给我解决方案< / p>
var header_constraint_H_Format = ""
var header_constraint_V_Format = ""
var bubble_constraint_H_Format = ""
var bubble_constraint_V_Format = ""
var content_constraint_H_Format = ""
var content_constraint_V_Format = ""
if message?.role == Role.Sender {
header_constraint_H_Format = "[header(50)]-5-|"
header_constraint_V_Format = "V:|-5-[header(50)]"
bubble_constraint_H_Format = "|-(>=5)-[bubble]-10-[header]"
bubble_constraint_V_Format = "V:|-5-[bubble(>=50)]-5-|"
content_constraint_H_Format = "|-(>=5)-[content]-25-|"
content_constraint_V_Format = "V:|[content]-5-|"
} else {
header_constraint_H_Format = "|-5-[header(50)]"
header_constraint_V_Format = "V:|-5-[header(50)]"
bubble_constraint_H_Format = "[header]-10-[bubble]-(>=5)-|"
bubble_constraint_V_Format = "V:|-5-[bubble(>=50)]-5-|"
content_constraint_H_Format = "|-25-[content]-(>=5)-|"
content_constraint_V_Format = "V:|[content]-5-|"
}
let header_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let header_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let bubble_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let bubble_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(bubble_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let content_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let content_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat(content_constraint_V_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
self.contentView.addConstraints(header_constraint_H as [AnyObject])
self.contentView.addConstraints(header_constraint_V as [AnyObject])
self.contentView.addConstraints(bubble_constraint_H as [AnyObject])
self.contentView.addConstraints(bubble_constraint_V as [AnyObject])
self.bubbleImgView.addConstraints(content_constraint_H as [AnyObject])
self.bubbleImgView.addConstraints(content_constraint_V as [AnyObject])
答案 0 :(得分:0)
UIView
方法.addConstraints
采用NSLayoutConstraint
类型,而非类型AnyObject
UIView Class Reference:
func addConstraint(constraint: NSLayoutConstraint)
由于您在调用NSLayoutConstraint
时将AnyObject
属性转换为.addConstraints(...)
,因此尝试使用无效的参数类型调用后者。
尝试删除对as [AnyObject]
。
.addConstraints(...)
答案 1 :(得分:0)
func addConstraints(_ constraints: [NSLayoutConstraint])
是NSLayoutConstraint
数组的期望参数。所以添加[AnyObject]没有意义。
因此,不要设置类型NSArray,而是[NSLayoutConstraint]或者只是删除它。
let header_constraint_H = NSLayoutConstraint.constraintsWithVisualFormat(header_constraint_H_Format, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
self.contentView.addConstraints(header_constraint_H)