首页 > 解决方案 > 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 

}

标签: swiftxcodeswift5

解决方案


错误的原因是它需要一个类型的值,UILayoutPriorityconstraint.priority不是setContentHuggingPriorityInt 或 Float。您可以使用以下构造函数UILayoutPriority,您可以在其中传入一个浮点值并获取预期的类型。init(rawValue: Float)在您的情况下,如下所示:

constraint.priority = UILayoutPriority(rawValue: priority)

有关更多详细信息,请参阅苹果文档: https ://developer.apple.com/documentation/uikit/nslayoutconstraint/1526946-priority


推荐阅读