gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:09 26 4
gpt4 key购买 nike

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

enter image description here

最佳答案

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

UIView Class Reference:
func addConstraint(constraint: NSLayoutConstraint)

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

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

关于ios - swift 2.1 无法将 anyobject 类型的值转换为预期的参数类型 nslayoutconstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com