Resolving “Cannot infer type of closure parameter … without a type annotation”
I’m working on a project right now making use of a lot of different things I don’t have much experience with - namely SwiftUI and using Firestore from Firebase. It’s a lot to learn as you go along, and sometimes when learning something new you just hit a snag that makes you want to quit software development forever. Today I finally resolved one of those snags.
While trying to organize some data into my models for a UI update I needed to update a @Published
variable inside the callback to FirebaseFirestore’s getDocument
. Everything was going swimmingly until suddenly I see that red line of death. An error at the call to getDocument
and the beginning of the closure that reads “Trailing closure passed to parameter of type 'FirestoreSource' that does not accept a closure”.
It made no sense to me - surely this should work. So I did what any great developer does and started commenting out code to find what exactly was the culprit here.
Ok! So! It doesn't like it when I append a new item to the list. Progress!
So now I'm thinking there is something illegal I am doing and that I can't just append to a published variable like this inside of the closure for getDocument
. But that's wild, how do people update their data? So I do the next thing any great developer does and head straight to google to find if anyone has run into this error. Good news! I find this stackoverflow post that describes the exact issue I am having. Bad news? It has 0 updates and it’s from 3 months ago. Every other stack overflow question I see is having unrelated trailing closure issues on SwiftUI's Form or similar. Some of the questions even have an example of them appending data to an array like I was and they are not hitting this issue - in fact they have a completely unrelated question that was usually a culprit of the asker not understanding asynchronous functions.
Great.
So I start getting desperate. Thinking of different ways to get this data and update it - keeping a temporary variable of lists that wasn't published, tracking when I got to the last id in a for loop then using a passed in callback to finally update the published variable and....
Nope.
At this point I am tearing my hair out. No one on the internet has this problem except me and one other person that I can find. So I start just rewriting things. I change the data model I try a different route to get the data I sacrifice a goat and still... nothing. Finally I just comment everything out and try to rewrite it as simply as possible and suddenly everything was working. After comparing the before + after you know what the issue was?
I wasn't converting the data's values I was getting from Any to Strings.
This is the least helpful error message I have seen in a long time. Don't be like me, kids. Convert your data values.