swift - xcode 文件自动更改 - Swift
问题描述
每天我运行我的项目,它工作正常,但不幸的是,今天当我尝试运行项目时,它显示了很多锁定文件的错误,代码已自动更改,这是错误显示 TZStackView.swift 和 PageView.swift 的两个文件
TZStackView.swift 文件的代码是:
fileprivate func constraint(item view1: AnyObject, attribute attr1: NSLayoutConstraint.Attribute, relatedBy relation: NSLayoutConstraint.Relation = .equal, toItem view2: AnyObject?, attribute attr2: NSLayoutConstraint.Attribute? = nil, multiplier: CGFloat = 1, constant c: CGFloat = 0, priority: Float = 1000) -> NSLayoutConstraint {
let attribute2 = attr2 != nil ? attr2! : attr1
let constraint = NSLayoutConstraint(item: view1, attribute: attr1, relatedBy: relation, toItem: view2, attribute: attribute2, multiplier: multiplier, constant: c)
constraint.priority = priority // the error show here which is Replace 'constraint.priority = priority' with 'UILayoutPriority(rawValue: constraint.priority = priority) ?? <#default value#>'
return constraint
}
对于 PageView.swift 文件:
if #available(iOS 9.0, *) {
let topAnchors = [
topStackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: topContainerOffset),
topStackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -topContainerOffset),
topContainerAnchor,
topContainerHeightAnchor
].flatMap { $0 }
NSLayoutConstraint.activate(topAnchors)
} else {
let topAnchors = [
topStackView.anchors.leadingAnchor.constraintEqualToAnchor(anchors.leadingAnchor, constant: topContainerOffset),
topStackView.anchors.trailingAnchor.constraintEqualToAnchor(anchors.trailingAnchor, constant: -topContainerOffset),
topContainerAnchor,
topContainerHeightAnchor
].flatMap { $0 }
NSLayoutConstraint.activate(topAnchors)
}
// StackViews common setup
topStackView.axis = .vertical
topStackView.alignment = .center
topStackView.distribution = .fill
topStackView.spacing = -10 // TODO: Make inspectable
// Add subviews to the top StackView
topStackView.addArrangedSubview(imageView)
topStackView.addArrangedSubview(titleLabel)
// Intial setup for top StackView subviews
imageView.isOpaque = true
imageView.contentMode = .scaleAspectFit
titleLabel.font = UIFont.boldSystemFont(ofSize: 35)
titleLabel.textAlignment = .center
titleLabel.text = pageTitle
//titleLabel.backgroundColor = .redColor()
// This way the StackView knows how to size & align subviews.
imageView.setContentHuggingPriority(250, for: .vertical) // the error shows here which is Cannot convert value of type 'Int' to expected argument type 'UILayoutPriority'
titleLabel.setContentHuggingPriority(252, for: .vertical) // and here
}
解决方案
错误的原因是它需要一个类型的值,UILayoutPriority
而constraint.priority
不是setContentHuggingPriority
Int 或 Float。您可以使用以下构造函数UILayoutPriority
,您可以在其中传入一个浮点值并获取预期的类型。init(rawValue: Float)
在您的情况下,如下所示:
constraint.priority = UILayoutPriority(rawValue: priority)
有关更多详细信息,请参阅苹果文档: https ://developer.apple.com/documentation/uikit/nslayoutconstraint/1526946-priority
推荐阅读
- acumatica - 如何自定义 InventoryID 选择器?
- azure - 使用 Virtual Kubelet 从 AKS 突发到 ACI 时无法访问 Azure 容器注册表存储库
- c - 二元炸弹第 4 阶段确认
- git - 在 Git 中覆盖整个项目
- go - 如何在没有访问“go get”的情况下构建 Go 项目?
- javascript - 自定义控制字段不呈现
- javascript - 点击后根据 URL 更改链接和文本
- matlab - 在 MATLAB 中通过多次计算优化三个嵌套循环
- rust - rust 目前是否有类似于 JavaScript 的 setTimeout 和 setInverval 的库实现功能?
- qt - 我如何接收 QList
在 qt 4.8 中通过 dbus?