|
Hi there, I need some help with this code. I have this Model, with assuntos being Optional:
And this View:
But the compiler gives me this error message: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. The funny thing is that if I do not make assuntos Optional, the code compiles normally. I guess the best option would be to refactor the code above, but I have no idea how to implement this refactoring. By the way, this is the VM:
And this is the APIService:
Thanks for the support. |
|
hi Rafael, on refactoring, the easiest things to do are to pull out the deepest-level constructs that are the content of the LazyVStack and the two ToolbarItems as separate functions or views. for example, you could write
where you define the function
hope that gets you off to a good start, DMG |
|
Hi DMG, I implemented that refactoring of yours, but still receiving the same error message. I guess the code part that should be refactored is the if let assuntos block... Any idea on this one? Thanks a lot! |
|
hi, keep refactoring ... there are still two low-level constructs to go ... but you could also try commenting out some code and checking compilation to see what's really driving the compiler crazy. for example, how about trying
if this compiles, then you know that you focus on refactoring the content of the ForEach; and you'll at least be giving SwiftUI a better chance to eventually expose whatever's causing the problem. hope that helps, DMG |
|
In addition to just factoring out some of the structural parts of your
There are a couple different ways to approach this. The easiest way would be to just give
and then use that computed property in the Another way to do it would be to rework how you decode the JSON. Why have an Can you post a sample of the JSON you are decoding for your |
|
Hi roosterboy, There are different ways I can reach this API (which returns a XML file, that is why I use the XMLCoder library, examples below). I can ask the API to return Documents with Assuntos (which means Documents with Subjects) or I can ask it to return Documents without Subjects, that is why my Model has optional Assuntos. Example 1, with subjects:
Example 2, without subjects:
I have a SettingsView that I use to make a POST request to the API, with a boolean field sending Yes/No for Subjects. Thanks for your help. |
|
Hi https://www.hackingwithswift.com/users/roosterboy , Any ideas on how I should handle this problem? Thank you again! |
|
Nothing that I and others haven't already mentioned. I'm not familiar with the XMLCoder library, so I can't offer any suggestions how to rework the way you decode the XML returned by the API. |
|
Hi @roosterboy and @delawaremathguy, I managed to figured the solution out. The problem was in the Documento model, the Struct Assuntos should be inside the Struct Documentos, then I could mark Assuntos as optional normally. Then in DocumentsView, I've marked assuntos as optional as well.
Now I can send different POST requests to the API, asking for Documents with or without subjects...the List inside DocumentsView updates accordingly. Best, |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.