(setValue :)不能在pictureURL中存储

编程入门 行业动态 更新时间:2024-10-28 15:23:23
本文介绍了(setValue :)不能在pictureURL中存储_SwiftValue类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经把我的观察者改成了这样的样子:

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

我过去只是做

观察.....在{ 让picture = snapshot.value![pictureURL]

但现在我猜你必须明确地告诉Firebase你正在处理字典?

我的问题是当我试图设置一个值。

我将pictureURL从上面添加到一个Person类中,后来我做了: pre $ 让picURL = self.currentUserInfo.pictureURL //从 之前的图片let info = [pictureURL:picURL,.... morestuff] as [String:Any] // Swift conversion added the String:Any bit。假设这是正确的? ref.setValue(info)

我得到这个错误: / p>

由于未捕获的异常InvalidFirebaseData,终止应用程序,原因:'(setValue :)无法在pictureURL中存储_SwiftValue类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。

为什么会发生这种情况,我该如何解决? 编辑:

所以我发现这只发生在试图从类中提取信息的时候。

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

$ b

上面的代码工作。

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)

该代码崩溃并且说我不能指定一个swiftvalue类型。看起来Firebase希望我们对于Swift 3的数据类型非常具体。

我们需要直接告诉Swift我们将有什么样的字典。在大多数情况下,它可能是

字典< String,Any>

Any是Swift 3中的一个新东西。 AnyObject在这里不起作用,因为Swift重新定义了AnyObject,而Strings / Ints似乎不再是它们。

最后,好像我们必须完全没有在你的字典中的可选项。也许一个可选的是_SwiftValue?一旦我摆脱了选择权,并保证每个价值将会在那里开始为我工作。

Having some issues understanding firebase with Swift 3.

I've remade my observer to look 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 used to just do

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

But now I guess you have to explicitly tell Firebase that you're dealing with a Dictionary?

My problem then comes when I'm trying to set a value.

I'm adding the pictureURL from above to a Person class, and later I'm doing:

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 this only happens when trying to pull info from a 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)

That code crashes and says I can't assign a swiftvalue type.

解决方案

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

We need to straight up tell Swift what kind of dictionary we're going to have. In most cases it'll probably be

Dictionary<String, Any>

"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 we 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.

更多推荐

(setValue :)不能在pictureURL中存储

本文发布于:2023-11-26 04:49:19,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能在   setValue   pictureURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!