SwiftCompile命令失败,退出代码为非零(代码语法正确)

l7wslrjt  于 2023-05-27  发布在  Swift
关注(0)|答案(1)|浏览(227)

我是Swift的新手,我开始制作我的宠物项目。我正在以编程方式构建UIKit用户界面,一切都很好。从某个时刻开始(我不明白到底发生了什么),Xcode开始告诉我“Command SwiftCompile failed with a nonzero exit code”,但它并没有告诉我语法不正确。
我发现这个扩展导致了这个问题:

import Foundation
import UIKit

enum Constraints<T> {
    case setTop(to: T, constant: CGFloat = 0)
    case setBottom(to: T, constant: CGFloat = 0)
    case setLeading(to: T, constant: CGFloat = 0)
    case setTrailing(to: T, constant: CGFloat = 0)
    case toBottom(of: T, constant: CGFloat = 0)
    case toTop(of: T, constant: CGFloat = 0)
    case toLeading(of: T, constant: CGFloat = 0)
    case toTrailing(of: T, constant: CGFloat = 0)
    case toHeight(of: T, multiplier: CGFloat = 1)
    case toWigth(of: T, multiplier: CGFloat = 1)
}

extension UIView {
    func applyConstraints(_ constraints: Constraints<UIView>...) {
        self.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(constraints.map {
            switch $0 {
            case setTop(to: let to, constant: let constant):
                return self.topAnchor.constraint(equalTo: to.topAnchor, constant: constant)
             case setBottom(to: let to, constant: let constant):
                return self.bottomAnchor.constraint(equalTo: to.bottomAnchor, constant: constant)
            case setLeading(to: let to, constant: let constant):
                return self.leadingAnchor.constraint(equalTo: to.leadingAnchor, constant: constant)
            case setTrailing(to: let to, constant: let constant):
                return self.trailingAnchor.constraint(equalTo: to.trailingAnchor, constant: constant)
            case toBottom(of: let of, constant: let constant):
                return self.topAnchor.constraint(equalTo: of.bottomAnchor, constant: constant)
            case toTop(of: let of, constant: let constant):
                return self.bottomAnchor.constraint(equalTo: of.topAnchor, constant: constant)
            case toLeading(of: let of, constant: let constant):
                return self.trailingAnchor.constraint(equalTo: of.leadingAnchor, constant: constant)
            case toTrailing(of: let of, constant: let constant):
                return self.leadingAnchor.constraint(equalTo: of.trailingAnchor, constant: constant)
            case toHeight(of: let of, multiplier: let multiplier):
                return self.heightAnchor.constraint(equalTo: of.heightAnchor, multiplier: multiplier)
            case toWigth(of: let of, multiplier: let multiplier):
                return self.widthAnchor.constraint(equalTo: of.widthAnchor, multiplier: multiplier)
            }
        })
    }
}

我使用它来减少布局代码:

class MainVC: UIViewController {
    
    
    let searchAreaContainer = UIView()
    let tabBarContainer = UIView()
    
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        
        view.addSubview(searchAreaContainer)
        searchAreaContainer.applyConstraints(
            .setTop(to: view),
            .setLeading(to: view),
            .setTrailing(to: view),
            .toHeight(of: view, multiplier: 0.06)
        )

        view.addSubview(tabBarContainer)
        tabBarContainer.applyConstraints(
            .setBottom(to: view),
            .setLeading(to: view),
            .setTrailing(to: view),
            .toBottom(of: searchAreaContainer)
        )

    }
    
}

这很奇怪,因为没有语法错误(Xcode没有告诉他们)。一段时间前,它工作正常。有人能帮我解释一下是什么导致了这一切吗?我使用的MacBook Pro 2023具有M2 Max CPU,32 Gb RAM和Xcode版本14.3(14E222b),在MacOs Ventura 13.3.1(a)(22E772610a)上运行。
如果这个问题不是由代码引起的,我可以发送一个URL到GitHub。谢谢你。
问题肯定是由UIView扩展引起的:

  • 我试图清理Build文件夹,重新启动Xcode并重新启动...没有什么帮助。
  • 如果将通用替换为UIView,问题仍然存在。
  • 如果留下一个案例,问题仍然存在
snvhrwxg

snvhrwxg1#

修复switch语句,使其具有.语法。
这是你的代码应该看起来像,我还修复了你的toWidth错字。

enum Constraints<T> {
    case setTop(to: T, constant: CGFloat = 0)
    case setBottom(to: T, constant: CGFloat = 0)
    case setLeading(to: T, constant: CGFloat = 0)
    case setTrailing(to: T, constant: CGFloat = 0)
    case toBottom(of: T, constant: CGFloat = 0)
    case toTop(of: T, constant: CGFloat = 0)
    case toLeading(of: T, constant: CGFloat = 0)
    case toTrailing(of: T, constant: CGFloat = 0)
    case toHeight(of: T, multiplier: CGFloat = 1)
    case toWidth(of: T, multiplier: CGFloat = 1)
}

extension UIView {
    func applyConstraints(_ constraints: Constraints<UIView>...) {
        self.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(
            constraints.map {
                switch $0 {
                case .setTop(to: let to, constant: let constant):
                    return self.topAnchor.constraint(equalTo: to.topAnchor, constant: constant)
                case .setBottom(to: let to, constant: let constant):
                    return self.bottomAnchor.constraint(equalTo: to.bottomAnchor, constant: constant)
                case .setLeading(to: let to, constant: let constant):
                    return self.leadingAnchor.constraint(equalTo: to.leadingAnchor, constant: constant)
                case .setTrailing(to: let to, constant: let constant):
                    return self.trailingAnchor.constraint(equalTo: to.trailingAnchor, constant: constant)
                case .toBottom(of: let of, constant: let constant):
                    return self.topAnchor.constraint(equalTo: of.bottomAnchor, constant: constant)
                case .toTop(of: let of, constant: let constant):
                    return self.bottomAnchor.constraint(equalTo: of.topAnchor, constant: constant)
                case .toLeading(of: let of, constant: let constant):
                    return self.trailingAnchor.constraint(equalTo: of.leadingAnchor, constant: constant)
                case .toTrailing(of: let of, constant: let constant):
                    return self.leadingAnchor.constraint(equalTo: of.trailingAnchor, constant: constant)
                case .toHeight(of: let of, multiplier: let multiplier):
                    return self.heightAnchor.constraint(equalTo: of.heightAnchor, multiplier: multiplier)
                case .toWidth(of: let of, multiplier: let multiplier):
                    return self.widthAnchor.constraint(equalTo: of.widthAnchor, multiplier: multiplier)
                }
            }
        )
    }
}

相关问题