Home > Net >  No exact matches in call to instance method 'append' : Swift
No exact matches in call to instance method 'append' : Swift

Time:06-24

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 ?

CodePudding user response:

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 the posts a var to be able to add something to is since let makes it immutable.

A much cleaner and correct way to write this code would be this:

var posts: [Dictionary<String,Any>] = []
let dict = postsnapshot?.value as? Dictionary<String, Dictionary<String, Any>>
posts.append(contentsOf: Array(dict?.values ?? []))

Also please rename the Postsnapshot property to postsnapshot unless you are actually calling a static property on a Postsnapshot 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!

CodePudding user response:

The problem was "let" during the declaration. as always creating issues

  • Related