斯威夫特用户界面:![Array] 编译器无法在合理的时间内对该表达式进行类型检查 [英] SwiftUI:! [Array] the compiler is unable to type-check this expression in reasonable time

查看:29
本文介绍了斯威夫特用户界面:![Array] 编译器无法在合理的时间内对该表达式进行类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决这个问题吗?问题出在文本代码中带有字符串数组的第二个按钮中,如果文本代码中存在字符串数组,则标记典型错误:编译器无法在合理的时间内对该表达式进行类型检查",但是如果我将字符串数组更改为普通字符串,没有错误.如果 sti 数组中包含 2 个或更多元素并带有第二个元素,则只会出现第二个按钮.

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()
        }
    }
}

推荐答案

修复the-compiler-is-unable-to-type-check-this-expression-in-reasonable-time 错误:

  • 为变量使用显式类型:
let telephone = "tel://"   // Not Optimal

let telephone: String = "tel://"    // This is Better because of the explicit `: String`

  • 使用较小的声明,而不是包含所有逻辑的大声明:
  • 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")
            .
            .
            .
    }
    

    • 避免在一个变量中进行大量计算:
    • // 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)
      

      • 确保您没有太大的东西.例如包含 5000 行元素的数组可能会让编译器抱怨.

        • 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.

          您不需要全力以赴地声明显式类型,因为这是 Swift 语言的一个很好的特性,您不想将其丢弃,但事实上,这总是帮助编译器.

          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.

          这篇关于斯威夫特用户界面:![Array] 编译器无法在合理的时间内对该表达式进行类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆