Ios swift 2.1无法将anyobject类型的值转换为预期的参数类型nslayoutconstraint
现在我正在使用swift 2.1,但我已经在swift 2.0上创建了我的项目,我的代码正在使用swift 2.0,但当我尝试使用最新的swift进行代码转换时,我无法理解如何转换,请给我解决方案Ios swift 2.1无法将anyobject类型的值转换为预期的参数类型nslayoutconstraint,ios,swift,syntax-error,Ios,Swift,Syntax Error,现在我正在使用swift 2.1,但我已经在swift 2.0上创建了我的项目,我的代码正在使用swift 2.0,但当我尝试使用最新的swift进行代码转换时,我无法理解如何转换,请给我解决方案 var header_constraint_H_Format = "" var header_constraint_V_Format = "" var bubble_constraint_H_Format = "" var
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])
使用
UIView
方法.addConstraints
获取类型NSLayoutConstraint
,而不是类型AnyObject
UIView Class Reference:
func addConstraint(constraint: NSLayoutConstraint)
由于在对.addConstraints(…)
的调用中将NSLayoutConstraint
属性强制转换为AnyObject
,因此尝试使用无效的参数类型调用后者
在调用.addConstraints(…)
func addConstraints(u.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)