swift 2.1无法将anyobject类型的值转换为预期的参数类型nslayoutconstraint

时间:2015-12-23 14:03:35

标签: ios swift syntax-error

现在我正在开发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]) 

enter image description here

2 个答案:

答案 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)