1

I have a few separate arrays that I am trying to combine. When I have more than 4 arrays that I try to combine, it does not build and only gives the error The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. I have tried it multiple different ways and am sure that it is only when I attempt to combine more than 4 arrays. Here is how I combine them:

For example let's say these are my arrays:

var a = [1, 2, 3, 4]
var b = [4, 6, 8, 10]
var c = [2, 4, 6, 8]
var d = [3, 6, 9, 12]
var e = [1, 3, 5, 7]

Then I combine them and sort like this:

This works:

(a+b+c+d).sorted { $0.name < $1.name }

But this does not and causes the build to fail:

(a+b+c+d+e).sorted { $0.name < $1.name }

Is there a different way that I should go about combining these arrays to support the combination of more than 4?

4
  • Where are you doing this stuff? In the body of a SwiftUI view, or...? Commented May 29, 2021 at 21:52
  • Best to create Chain5 and submit for inclusion in Algorithms. github.com/apple/swift-algorithms/blob/main/Guides/Chain.md Commented May 29, 2021 at 22:09
  • A number doesn't have a name. Can't reproduce the issue. Commented May 29, 2021 at 22:21
  • @West1 I am doing this in the body of a SwiftUI View, yes. Commented May 29, 2021 at 22:30

1 Answer 1

1

the following works well for me on macos 11.4, xcode 12.5, target ios 14.5 and macCatalyst 11.3. Show us exactly how you are using the code.

import SwiftUI

@main
struct TestErrorApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
struct ContentView: View {
    var a = [1, 2, 3, 4]
    var b = [4, 6, 8, 10]
    var c = [2, 4, 6, 8]
    var d = [3, 6, 9, 12]
    var e = [1, 3, 5, 7]
    var f = [4, 6, 8, 10]
    var g = [1, 3, 5, 7]
    var h = [2, 4, 6, 8]
    
    var body: some View {
        Text("array combination").onAppear {
            let arr = (a+b+c+d+e+f+g+h).sorted { $0 < $1 }
            print("----> arr: \(arr)")
        }
}
} 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.