1

The following code once worked fine and I was able to save multiple values to a Firebase record. However this no longer works since upgrading to Swift 3 (Xcode 8). I now get the following error:

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

The above error always mentions the second value, regardless of what type it is (even if it is one of the supported types like NSString). Here's what I have:

postsRef.childByAutoId().setValue(["postedBy": self.currentUser?.uid, "mood": mood, "status": status, "date": convertedDate])

This still seems to comply with the docs on the Firebase website. What am I doing wrong?

Thanks!

winston
  • 3,000
  • 11
  • 44
  • 75

2 Answers2

0

Try :-

 let toBePosted = ["postedBy": String(self.currentUser!.uid),
                    "mood": String(mood), "status": String(status),    
                    "date": String(convertedDate)]

 postsRef.childByAutoId().setValue(toBePosted){ (error, ref) -> Void in

   }

As for converted date you will have to convert it to NSDate format when you retrieve it.

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Still getting the error. Do you think it's because I haven't updated the pod in a few months? I have `import Firebase` but I've seen in the docs that we now need to use separate pods like `import Firebase/Database` or something – winston Sep 09 '16 at 02:42
  • I'm using Using Firebase (3.3.0) – winston Sep 09 '16 at 02:45
  • Sorry I haven't had a chance. I will try to update the pods as soon as I get home. Do you know if there are any syntax changes upgrading from 3.3.0? I'm worried it might break my app even more – winston Sep 09 '16 at 15:26
  • Nope, I ran pods update and it still gives an error. I think something is wrong though. The latest update I still just have `import Firebase` at the top of my view controllers. If I try to use `import FirebaseDatabase` I get the segment fault 11 error. It's like it's still trying to use the old version. I might uninstall/reinstall Firebase – winston Sep 09 '16 at 21:28
  • OK, I reinstalled and then changed `import Firebase` to `import FirebaseDatabase`. I now get an error on the solution you provided above. `Contextual type 'Any' cannot be used with dictionary literal` – winston Sep 09 '16 at 21:46
  • It looks like that got rid of the error, but I'm getting a Segmentation fault 11 error when I try to run. I had to switch back to `import Firebase`. I think using `import FirebaseDatabase` caused the initial error. The firebase docs show only use `import Firebase`. – winston Sep 09 '16 at 22:06
  • Swift 3 is giving a lot of issues.... i guess Firebase wont update their Docs till this fall till swift 3 is finalised.. – Dravidian Sep 09 '16 at 22:10
  • I'm not sure, the build is failing after I updated Firebase pods. I get an error `Command failed due to signal: Segmentation fault:11`. Looks like it errors on "Merge MyApp.swiftmodule" – winston Sep 09 '16 at 22:11
  • http://stackoverflow.com/a/31797572/6297658 Also try cleaning your project, deleting the derived data and reseting the siumulator and then running again... – Dravidian Sep 09 '16 at 22:14
  • Nope, doesn't work. I'm at a complete loss. I spent hours on this tonight. I have no idea where to even troubleshoot – winston Sep 10 '16 at 04:54
0

I just had this issue and after a few hours of toying around I think I've got a solution.

It looks like Firebase is wanting you to be very specific with your data types with Swift 3.

You need to straight up tell Swift what kind of dictionary you're going to have. In this case it's probably

let toBePosted : Dictionary<String, Any> = ["postedBy": String(self.currentUser!.uid),
                "mood": String(mood), "status": String(status),    
                "date": String(convertedDate)]

"Any" is a new thing in Swift 3 I think. AnyObject doesn't work here because Swift redefined what AnyObjects are, and Strings/Ints dont seem to be them anymore.

Finally, it seems like you have to have absolutely no optionals in your dictionary. Maybe an optional is a _SwiftValue? Once I got rid of optionals and garunteed each value was going to be there it started working for me.

This worked for me, let me know if you're still having an issue.

user6820041
  • 1,213
  • 2
  • 13
  • 29