-1

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 (Xcode doesn't tells about them). And some time before it worked fine. Can please anyone help what's causing this? 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 a url to GitHub if this problem is not caused by the code. Thank you.

The problem is definitely caused by UIView extension:

  • I tried to clean Build folder, restarting Xcode and rebooting... Nothing Helped.
  • If replace generic with UIView, problem remains.
  • If a single case is left, problem remains
Alexey_BH
  • 101
  • 7
  • Maybe [this question](https://stackoverflow.com/questions/46690619/build-fails-with-command-failed-with-a-nonzero-exit-code) could be helpful – Joakim Danielson May 26 '23 at 14:31
  • Thank you, I tried cleaning Derived data by Terminal command: "cd ~/Library/Developer/Xcode/DerivedData && open .", but the problem remains. Also, the most voted reply in that thread is: "Everybody wrote down his own case, damit apple, what a frustrating environment is that Xcode thing" by @yerlilbilgin – Alexey_BH May 26 '23 at 14:41
  • Also I tried other ways mentioned there, like adding SWIFT_ENABLE_BATCH_MODE=NO to project build settings, but they didn't remove the issue (( – Alexey_BH May 26 '23 at 14:47
  • I've deleted most cases and used a bit simplified code... no effect – Alexey_BH May 26 '23 at 15:02

1 Answers1

1

Fix your switch statement to have the . syntax.

Here is what your code should look like, I also fixed your toWidth typo.

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)
                }
            }
        )
    }
}
valosip
  • 3,167
  • 1
  • 14
  • 26
  • Thank you so much! Now everything works fine! I dunno why the compiler didn't tell me about that error... – Alexey_BH May 27 '23 at 13:39