Home > OS >  SwiftUI The compiler is unable to type-check this expression in reasonable time
SwiftUI The compiler is unable to type-check this expression in reasonable time

Time:09-29

I could compile and run these code successfully before I updated Xcode to version 13.0. But this error shows up now.

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

I tried to break down my views and reproduce it like this. I found the problem is on line 21. The code:

import SwiftUI

struct EmojiView: View {
    
    var EmojiArr: [Int] = [0x1f601, 0x1f602, 0x1f603, 0x1f604, 0x1f605, 0x1f606, 0x1f607, 0x1f608, 0x1f609]
    
    func GetVstackNum(num: Int) -> Int {
        if num % 3 == 0 {
            return num / 3
        } else  {
            return num / 3   1
        }
    }
    
    var body: some View {
        HStack(spacing: 10) {
            let length = EmojiArr.count
            if length > 0 {
                ForEach(0 ... (GetVstackNum(num: length) - 1), id: \.self) { i in
                    VStack(alignment: .leading, spacing: 22) {
                        ForEach((3 * i) ... (3 * i   2), id: \.self) { j in
                            if j < length {
                                Button(action: {
                                    // Some button action
                                }) {
                                    if (UnicodeScalar(EmojiArr[j])?.properties.isEmoji)! {
                                        Text(String(UnicodeScalar(EmojiArr[j])!)).font(.system(size:17))
                                    }
                                    else {
                                        Text("")
                                    }
                                }
                                .frame(width: 20, height: 10, alignment:.topLeading)
                                .buttonStyle(PlainButtonStyle())
                            }
                        }
                    }
                    .frame(height: 83, alignment:.topLeading)
                }
            }
        }
    }
}

How to fix this?

CodePudding user response:

You should do what the error message tells you.

I think the struggle the compiler has, is that the if j < length { ... } does not have an else case with a View. Try this as a test (but do what the error message tells you), works for me on macos 12, xcode 13-beta5 (not release):

var body: some View {
    HStack(spacing: 10) {
        let length = EmojiArr.count
        if length > 0 {
            ForEach(0 ... (GetVstackNum(num: length) - 1), id: \.self) { i in
                VStack(alignment: .leading, spacing: 22) {
                    ForEach((3 * i) ... (3 * i   2), id: \.self) { j in
                        if j < length {
                            Button(action: { }) {
                                if let emo = UnicodeScalar(EmojiArr[j]), emo.properties.isEmoji {
                                    Text(String(emo)).font(.system(size:17))
                                } else {
                                    Text("")
                                }
                            }
                            .frame(width: 20, height: 10, alignment:.topLeading)
                            .buttonStyle(PlainButtonStyle())
                        } else {
                            Text("") // <-- here
                        }
                    }
                }
                .frame(height: 83, alignment:.topLeading)
            }
        }
    }
}

And don't use force unwrap in your code.

CodePudding user response:

Your Code worked fine when I tested it using xcode 12.3 on macOS 11.2.3 . You can try using return HStack and see if the problem still persists. The above answer has some good tips as well

  • Related