Hello,
I'm new to Swift and I started making my pet project. I was building a UIKit user interface programmatically, and everything worked fine.
Since some moment (I do not understand what really happened) Xcode started telling me that "Command SwiftCompile failed with a nonzero exit code" while it doesn't tell me that syntax is not correct.
I found that this extension causes the problem:
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)
}
})
}
}
I'm using it to reduce layout code:
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)
)
}
}
It's strange because there are no syntax errors. And some time before it worked fine.
Can please anyone help what's causing this? I tried to clean Build folder, restarting Xcode and rebooting... Nothing Helped.
I'm using MacBook Pro 2023 having M2 Max CPU with 32 Gb of RAM and Xcode Version 14.3 (14E222b) running on MacOs Ventura 13.3.1 (a) (22E772610a)
I can send an url to GitHub if this problem is not caused by the code.
Thank you.