skip to Main Content

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


  1. 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!

    Login or Signup to reply.
  2. 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 {

    Posts.append(val)
    

    }

    Login or Signup to reply.
  3. 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 the append 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.

    private var items: [Item] = []
    
    func addItem(name: String, currency: Currency){ 
      items.append(
        Item(
          name: name, 
          currency: currency
        )
      )
    }
    

    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.

    let item = Item(
        name: name, 
        currency: currency
      )
    items.append(item)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search