How do I stop 'The compiler is unable to type-check this expression in reasonable time'

I am trying to write my first app, after adding another item to tab view, I get the error
Code Block
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions


How do I stop this error?

my code is:




Answered by OOPer in 658660022

it got rid of the original error

Big advance!

now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45

Having multiple things with the same name may not be good for programming, especially when the code being complex enough.
I guess you may need to distinguish the property rockets and closure argument rockets:
Code Block
rocketApiCall().getUsers{ (rockets) in self.rockets = rockets} //<- Please do not miss `self.`


my code can be found here: code

You should better include the core part of your code as Code block.
I can't because there is a character limit

I can't because there is a character limit

Thanks for moving your code into Text Attachment.

But it is hard to say something sure as many undefined symbols are preventing us to estimate the actual part causing the issue.

Better try breaking up the expression into distinct sub-expressions, as suggested.

You can break up the definition of body, in the same way as aboutPage.

Generally, this error The compiler is unable to type-check this expression... often occurs when some type-related errors are in the code. You would be able to find where that is happening. with splitting your loooong body into sub-views.
I moved the group from body > tab view > group > ZStack > NavigationView > VStack > List > ForEach > Button > HStack to its own View, but now I get more errors... The view looks like:
Code Block
var bodyGroup: some View {
        Group {
            Text(launch.name)
                .font(.system(size: 23))
                .frame(maxWidth: .infinity, alignment: .leading)
                .fixedSize(horizontal: false, vertical: true)
            Text(Date(timeIntervalSince1970: launch.date_unix).getFormattedDate(format: "dd/MM/yyyy HH:mm:ss"))
                .font(.system(size: 11.5))
                .foregroundColor(Color.gray)
                .frame(maxWidth: .infinity, alignment: .leading)
                .fixedSize(horizontal: false, vertical: true)
            Spacer()
        }
    }

The errors are
  • The same original error

  • Cannot find 'launch' in scope (line 5)

  • Cannot find 'launch' in scope (line 13)

I never used to get these errors before I moved it to the view...

now I get more errors

Generally, when you get The compiler is unable to type-check this expression, there may be multiple type-related errors, that Swift compiler could not detect. So, while breaking up, you may find more and more errors.

Your definition of body is too large. You should better check errors every time you add lines into body.

  • The same original error

You need to split up codes until the same error is not generated

  • Cannot find 'launch' in scope (line 5)

  • Cannot find 'launch' in scope (line 13)

You use launch in your definition of bodyGroup, that should be declared inside the scope of bodyGroup.
But it does not exist.

You may need to make it a function instead of computed property:
Code Block
    func bodyGroup(_ launch: Launch) -> some View {
//...
}


And use it as:
Code Block
                                            bodyGroup(launch)

You may need to repeat this process.
I have moved quite a chunk of code to a new Swift file called rocketView, it got rid of the original error, but now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45... my code is attached. (Line 45 is the line between }.onAppear { and }.listStyle...


Accepted Answer

it got rid of the original error

Big advance!

now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45

Having multiple things with the same name may not be good for programming, especially when the code being complex enough.
I guess you may need to distinguish the property rockets and closure argument rockets:
Code Block
rocketApiCall().getUsers{ (rockets) in self.rockets = rockets} //<- Please do not miss `self.`


If not too long, it is easier to Paste And Match Style for the code. You get line numbers directly.
Code Block
import SwiftUI
import SDWebImage
import HalfModal
import Foundation
struct rocketView: View {
@State var rockets: [Rocket] = []
@State private var showingHalfModal2: Bool = false
var body: some View {
ZStack {
NavigationView {
VStack {
Spacer()
.frame(height: 1)
.navigationBarTitle("Rockets")
List {
ForEach(rockets, id: \.id) { rocket in
Button(action: {
withAnimation {
self.showingHalfModal2 = true
}
}) {
rHStack(rocket)
}
.buttonStyle(PlainButtonStyle())
}
}
}.onAppear {
rocketApiCall().getUsers{ (rockets) in rockets = rockets}
}.listStyle(SidebarListStyle())
.frame(alignment: .center)
}
}
if showingHalfModal2 {
HalfModalView(content: AnyView(HStack {
VStack {
Text("test123")
.padding()
}
}), header: AnyView(HStack {
VStack(alignment: .leading) {
Text("test")
Text("test")
.font(.system(size: 10))
.foregroundColor(Color.gray)
}}), isPresented: $showingHalfModal2)
}
}
func rHStack(_ rocket: Rocket) -> some View {
HStack {
Group {
Text(rocket.name)
.font(.system(size: 23))
.frame(maxWidth: .infinity, alignment: .leading)
.fixedSize(horizontal: false, vertical: true)
Text("Active: " + String(rocket.active))
.font(.system(size: 15))
.foregroundColor(Color.gray)
Spacer()
}
}
}
}
struct rocketView_Previews: PreviewProvider {
static var previews: some View {
rocketView()
}
}

I have moved quite a chunk of code to a new Swift file called rocketView, it got rid of the original error, but now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45... my code is attached. (Line 45 is the line between }.onAppear { and }.listStyle...

So the error is now line 34.
But you don't show the code for
rocketApiCall() and its getUsers
So it's hard to say.
Note: it is more readable to give a different name anyway, such as:
rocketApiCall().getUsers { (theRockets) in rockets = theRockets}
Try also removing the parenthesis around theRockets
OOPer's method worked! Apart from the fact that I display it all in a tabview, it now doesn't display correctly... Screenshots can be found in this iCloud folder, with the names incorrect display for the wrong display of my app, and correct display for how it should look...

Edit: fixed it, it was me being silly. Thanks OOPer for the help!

Edit #2: Each time a modal shows up, it adds a new tab to my TabView, it never used to do this... How do I stop that?

OOPer's method worked!

Happy to hear that.

it now doesn't display correctly...

You should better start a new thread.
Generally, you should not include too many topics in a single thread. (If one topic is large enough, two are too many.)
And a brand-new thread would get more attention from readers who want to solve issues.

Please include enough code into your new thread. (If there were no missing parts in your code -- such as Launch, Rockect, rocketApiCall, apiCall, URLimageView, etc... I could pinpoint where to fix instead of telling a general method.)
If you can include some testing data, it would be better.
I've started a new thread here
How do I stop 'The compiler is unable to type-check this expression in reasonable time'
 
 
Q