0

I am trying to append data from Firebase to a variable called var itemNumbers: [[String: AnyObject]]! that belongs to an NSObject. This is how the fetch looks like:

        let orderItems = snapDict["itemNumbers"] as! [String:String]
        self.NSOBJECT.itemNumbers.append(orderItems as [String : AnyObject]) 
        print(orderItems)

When I do this the app breaks exactly where I try to append orderItems to the array. This is my error:

fatal error: unexpectedly found nil while unwrapping an Optional value

JSON:

{ "order": {
      "itemNumbers" : {
         "1": "Item Description",
         "2": "Item Description"
                      }
           }   
}

Do I need to convert orderItems into something else before I try to append it?

Thanks for the help!

27
  • where is the itemNumbers key ? Commented Nov 8, 2016 at 6:22
  • @cosmos can you trying with my answer Commented Nov 8, 2016 at 6:25
  • try this . self.NSOBJECT.itemNumbers.append(orderItems as [String : String]) Commented Nov 8, 2016 at 6:27
  • Maybe changing the var itemNumbers: [[String: AnyObject]]! to var itemNumbers: [[String: String]]! ??? Commented Nov 8, 2016 at 6:28
  • @cosmos your json is still invalid Commented Nov 8, 2016 at 6:30

2 Answers 2

1

Correct your json as whole lot of fields are incorrect.

Make your variable itemNumbers from

var itemNumbers: [[String:AnyObject]]!

to

 var itemNumbers: [[String:AnyObject]] = [] //if nil instantiate

as itemsNumbers is optional.

And append as

 let order = jsonDictionary["order"] as! [String:AnyObject]
 let tempItemNumbers = order["itemNumbers"] as! [String:AnyObject]
 self.NSOBJECT.itemNumbers.append(tempItemNumbers)
 print( self.NSOBJECT.itemNumbers)
Sign up to request clarification or add additional context in comments.

4 Comments

could not cast value of nsdictionary to nsarray. Also, I am accessing cart through the database reference so I do not need to use it to access tempItemNumbers right?
now the problem is how should I send it to firebase? let cartVal = ["item":"1"]["item2":"2"] like that???
I am not sure how to create this when I set the value on the database reference. Never done this , any clues? This will help me greatly.
@cosmos Your can make your hierarchy as { "order": { "itemNumbers": { { "1": "Item Description" }, { "2": "Item Description" } } } } ..For reference how to strucuture see here or here
1

mention comment

well if you want to append order then use [[String: AnyObject]] on the other hand append itemNumbers for [String:String]

      guard  let  itemNumbers= snapDict["itemNumbers"] as? [String:String]  else{
         return
     }

      self.NSOBJECT.itemNumbers.append(itemNumbers)

or

      guard  let  orderItems = snapDict["order"] as? [[String: AnyObject]]  else{
         return
     }

      self.NSOBJECT.itemNumbers.append(orderItems)

Note : avoid the problematic implicit conversion [[String: AnyObject]]!

3 Comments

I added ` self.NSOBJECT.itemNumbers.append(orderItems! as [String : AnyObject])` and this was the output: [["item": 1]]
Is this how I should send the data to Firbase? let ordertems = ["item":"1"]["item2":"2"] in order to receive it like your example? I assign this to the key orderItems.
(ill keep working on this tomorrow) thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.