samedi 24 octobre 2020

ForEach on sorted dictionary: Xcode hangs on building with if block

I have a simple dictionary such as follows:

let testDict = [
    "a": "apple",
    "b": "banana",
    "c": "cherry"
]

To render it in a view, using an answer from SwiftUI iterating through dictionary with ForEach, I use:

ForEach(testDict.sorted(by: >), id: \.key) { key, value in
   Text(value)
}

and it works well enough. However, if I attempt to include an if block:

ForEach(testDict.sorted(by: >), id: \.key) { key, value in
   if key != "b" {
       Text(value)
   }
}

XCode seems to hang (it just takes a very long time) and eventually gives up with these messages:

Command CompileSwift failed with a nonzero exit code

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

at the very end of the ContentView struct. This is not very helpful, and I can’t see why it takes XCode so long to give up.

How do I include an if block in a ForEach ?

I real life, the dictionary comes from a CoreData model, but the above sample gives the same problems.

I am developing on XCode 12.1, targeting MacOS 10.15

Aucun commentaire:

Enregistrer un commentaire