(setValue :) Unable to save object of type _SwiftValue in pictureURL. Can store objects like NSNumber, NSString, NSDictionary, and NSArray

Having some trouble understanding firebase with Swift 3.

I redid my observer like this:

currentUserFirebaseReference.observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in let UID = snapshot.key let dict = snapshot.value as! Dictionary<String,AnyObject> let pictureURL = dict["pictureURL"] as! String 

I just did

 observation..... in { let picture = snapshot.value!["pictureURL"] 

But now, I think, you need to explicitly tell Firebase that you are dealing with a Dictionary?

My problem occurs when I try to set a value.

I add pictureURL on top to the Person class, and later I do:

  let picURL = self.currentUserInfo.pictureURL // The picture from before let info = ["pictureURL" : picURL, ....morestuff] as [String : Any] // Swift conversion added the String:Any bit. Assume this is correct? ref.setValue(info) 

and I get this error:

 Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(setValue:) Cannot store object of type _SwiftValue at pictureURL. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray. 

Why is this happening and how can I fix it?

edit:

So, I found out that this only happens when trying to extract information from the class.

  let data : Dictionary<String, Any> = ["Cats" : 3, "Dogs" : 9] DataService.ds.REF_BASE.child("testing").setValue(data) 

The above code works.

  class Dog { var fur : String? } let dog = Dog() dog.fur = "Brown" let data : Dictionary<String, Any> = ["Cats" : 3, "Dogs" : 9, "DogFur" : dog.fur] DataService.ds.REF_BASE.child("testing").setValue(data) 

This code crashes and says that I cannot assign a swiftvalue type.

+5
source share
2 answers

It looks like Firebase wants us to be very specific with your data types with Swift 3.

We need to directly tell Swift which dictionary we will have. In most cases, this is likely to be

 Dictionary<String, Any> 

β€œAnyone” is a new thing in Swift 3, I think. AnyObject does not work here because Swift overrides what AnyObjects is and the / Ints lines no longer look like them.

Finally, it seems that we have absolutely no options in your dictionary. Maybe an optional _SwiftValue? As soon as I got rid of the options and earned money, each value will be where it started to work for me.

+3
source
 class Dog { var fur : String? } 

to

 class Dog { var fur : String! } 

no options

0
source

All Articles