so I am trying to get values from firebase into a table. therefore first I am adding the data items to a list of type[Dictonary]
the data is like this:
Posts:
P1 :
post data....
P2:
post data....
code I’m using
let Posts: [Dictionary<String,Any>]! = nil
var dict = Postsnapshot?.value as? Dictionary<String, Dictionary<String, Any>>
Posts.append(contentsOf: dict.values)
I am doing something like this but it throws me this error "No exact matches in call to instance method ‘append’ ". I have tried every initializer even list.append(Dict) , still the same error. what should I do ?
3
Answers
You are calling your array
Posts
with a capital letter and you probably have a type declared somewhere with the sam exact name and the compiler is confused. Of course you also need to make theposts
avar
to be able to add something to is sincelet
makes it immutable.A much cleaner and correct way to write this code would be this:
Also please rename the
Postsnapshot
property topostsnapshot
unless you are actually calling a static property on aPostsnapshot
type.Here are some resources for learning how to style your Swift code:
But to learn more on how to use Swift most efficiently I would strongly suggest to take a look a the official Swift Language Guide and preferably reading the whole thing. It’s incredibly informative!
Using loop append one by one value in array
eg.
var Posts: [Dictionary<String,Any>]! = nil
var dict = Postsnapshot?.value as? Dictionary<String, Dictionary<String, Any>>
for val in dict.values {
}
I am posting this answer in case anyone like me faces this issue when the problem isn’t really with the
append
method itself but with what was passed to theappend
method.In my case, I tried to append a newly created instance of a struct to a an array of the said struct. One of the parameters I was supposed to supply to the struct’s initializer was a string. I incidentally passed in an enum instead of the enum’s
rawValue
. Since I was creating the object and initializing the struct in the same line, Xcode showed me the error mentioned in the question for some reason.The parameter was currency.
I had to seperate the struct’s initialization from it’s addition to the array by assigning to a variable first then appending that variable to the array before I got the right error message. After I resolved the issue I made it a one-liner again.