【问题标题】:swift 2.1 cannot convert value of type anyobject to expected argument type nslayoutconstraintswift 2.1 无法将 anyobject 类型的值转换为预期的参数类型 nslayoutconstraint
【发布时间】:2016-03-29 23:43:25
【问题描述】:

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

【问题讨论】:

    标签: ios swift syntax-error


    【解决方案1】:

    UIView 方法.addConstraints 接受类型NSLayoutConstraint,而不是类型AnyObject

    UIView Class Reference:
    func addConstraint(constraint: NSLayoutConstraint)
    

    由于您在调用.addConstraints(...) 时将NSLayoutConstraint 属性转换为AnyObject,因此您尝试使用无效的参数类型调用后者。

    尝试在您对.addConstraints(...) 的调用中删除as [AnyObject]

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-22
        • 2017-01-06
        • 2016-10-04
        相关资源
        最近更新 更多