1

Can you help me solve this problem? The problem is in the second button with the string array inside the Text Code, if there is an string array inside the text code mark the typical error: "the compiler is unable to type-check this expression in reasonable time", but if I change the string array to a normal string, there is no error. The second button just appears if the sti array has 2 or more elements inside it with the second element.

Group {
    let telephone = "tel://"
    
    Button(action:{
        let formattedString = telephone + CardPn[0]
        guard let url = URL(string: formattedString) else { return }
        UIApplication.shared.open(url)
    }){
        Text(CardPn[0])
            .fontWeight(.bold)
            .underline()
    }
    
    if CardPn.count >= 2{
        Button(action:{
            let formattedString = telephone + CardPn[1]
            guard let url = URL(string: formattedString) else { return }
            UIApplication.shared.open(url)
        }){
            Text(CardPn[1])
                .fontWeight(.bold)
                .underline()
        }
    }
}
2
  • 2
    Indentation ... Commented Apr 9, 2021 at 17:23
  • try adding explicit type. let formattedString: String = .... Commented Apr 9, 2021 at 17:24

1 Answer 1

2

General advice to fix the-compiler-is-unable-to-type-check-this-expression-in-reasonable-time errors:

  • Use explicit types for your variables:
let telephone = "tel://"   // Not Optimal

let telephone: String = "tel://"    // This is Better because of the explicit `: String`
  • Use smaller declarations instead of a one big thing that contains all your logic:
var body: some View {
    
    // Not optimal button
    Button(action: {
    // do something
        .
        .
        .
    }, label: {
        Text("something")
            .
            .
            .
    })
    

    // Better example of a button
    Button(action: actionOfTheButton, label: buttonLabel)

}

// Declare the action somewhere else
func actionOfTheButton() {
    // do something
        .
        .
        .
}

// Label of the button is declared here
var buttonLabel: some View {
    Text("something")
        .
        .
        .
}
  • Avoid big calculations in one variable:
// We want to calculate the finalArea

// some variables to showcase
let someWidth: CGFloat = 200
let someHeight: CGFloat = 450
let ratio: CGFloat = 0.75
let maxSize: CGFloat = 80000
let minSize: CGFloat = 100

// not optimal because there are lots calculations going on in one line
// to calculate the finalArea
let finalArea = max(min((someWidth * someHeight) * ratio, minSize), maxSize)

// this approach is better, both for compiler and for code-readability,
// finalArea is calculated in 3 steps instead of 1:
let initialArea = someWidth * someHeight
let areaWithRatio = initialArea * ratio
let finalArea = max(min(areaWithRatio, minSize), maxSize)
  • Make sure you don't have something too big. e.g. an array containing 5000 lines of Elements will likely make the compiler complain.

  • The last possibility i remember, is that you have a syntax error or other errors on your side.
    In this case you should just try to eye-ball the problem and fix it.

You don't need to go all-in in declaring explicit types, as thats one of the nice features of the Swift language and you don't want to be throwing that away, but as a matter of fact, that always helps the compiler.

About the second tip, it is generally advised to cut your app to smaller components instead of big components. That also helps your code to be better readable and more manageable. The bigger your project, the more you thank yourself for these smaller components.

Sign up to request clarification or add additional context in comments.

1 Comment

The function in the button worked for me, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.