将自定义类转换为NSDictionary(Casting custom class to NSDictionary)

编程入门 行业动态 更新时间:2024-10-17 23:27:17
自定义类转换为NSDictionary(Casting custom class to NSDictionary)

我到处搜索但无济于事:

你能为NSDictionary投一个自定义类吗?

使用案例:

我有一个包含一些属性的Todo类,我想用taskRef.setValue(task as? NSDictionary)将它保存到Firebase但是我甚至不知道这是否可行。

这是我的Todo课程:

class Todo : NSObject{ var name: String var desc: String var date: Double var location: [Double]? var snapshot: FIRDataSnapshot? init(name: String, desc: String? = nil, date: Double, location: [Double]? = nil){ self.name = name self.desc = desc! self.date = date self.snapshot = nil self.location = location } convenience init(name: String, desc: String? = nil, date: NSDate, location: CLLocationCoordinate2D? = nil){ var serLoc: [Double]? = nil if let location = location { serLoc = [location.latitude, location.longitude] } self.init(name: name, desc: desc, date: date.timeIntervalSince1970 as Double, location: serLoc) } convenience init(snapshot: FIRDataSnapshot){ let dict = snapshot.value as! [String: AnyObject] let location = dict["location"] as? [Double] self.init(name: dict["name"] as! String, desc: dict["desc"] as? String, date: dict["date"] as! Double, location: location) self.snapshot = snapshot } convenience override init(){ self.init(name: "", desc: "", date: NSDate()) } }

I've searched everywhere but to no avail:

Can you cast a custom class to NSDictionary?

Use case:

I have a Todo class containing some properties and I want to save it to Firebase with taskRef.setValue(task as? NSDictionary) however I don't even know if this is possible.

Here's my Todo class:

class Todo : NSObject{ var name: String var desc: String var date: Double var location: [Double]? var snapshot: FIRDataSnapshot? init(name: String, desc: String? = nil, date: Double, location: [Double]? = nil){ self.name = name self.desc = desc! self.date = date self.snapshot = nil self.location = location } convenience init(name: String, desc: String? = nil, date: NSDate, location: CLLocationCoordinate2D? = nil){ var serLoc: [Double]? = nil if let location = location { serLoc = [location.latitude, location.longitude] } self.init(name: name, desc: desc, date: date.timeIntervalSince1970 as Double, location: serLoc) } convenience init(snapshot: FIRDataSnapshot){ let dict = snapshot.value as! [String: AnyObject] let location = dict["location"] as? [Double] self.init(name: dict["name"] as! String, desc: dict["desc"] as? String, date: dict["date"] as! Double, location: location) self.snapshot = snapshot } convenience override init(){ self.init(name: "", desc: "", date: NSDate()) } }

最满意答案

不,您不能将自定义类“ NSDictionary ”为NSDictionary 。 (或者也是一个Swift字典。)Casting告诉编译器“别担心,这个对象真的是一个字典。相信我。” 铸造不是转换。 如果您尝试将不是字典的自定义对象CAST到字典,它将不是字典。

您需要将对象转换为字典,而不是转换它。 正如Larme在他的评论中所建议的那样,在你的类中添加一个toDictionary方法,该方法返回一个字典,该字典将对象的属性编码为字典中的键/值对。

然后创建一个自定义init方法,该方法将字典作为参数。

我似乎记得读过有Swift方法可以让你查询一个对象的属性并得到它们的名字。 你可以编写你的toDictionary代码来使用它。 如果我没记错的话,他们会使用Mirror和.children 。 如果您不想编写自定义代码来转换对象的属性,请查看Mirror。

No, you cannot "cast" a custom class to NSDictionary. (Or a Swift dictionary either.) Casting is telling the compiler "Don't worry, this object is really a dictionary. Trust me." Casting is not converting. If you try to CAST a custom object that is not a dictionary to a dictionary, it won't BE a dictionary.

You need to convert your object to a dictionary, not cast it. As Larme suggests in his comment, add a toDictionary method to your class that returns a dictionary that encodes the properties of your object into key/value pairs in the dictionary.

Then create a custom init method that takes a dictionary as a parameter.

I seem to remember reading that there are Swift methods that let you interrogate the properties of an object and get their names. You could write your toDictionary code to use that. If I remember correctly they use Mirror and .children. If you don't want to write custom code to convert your object's properties, take a look at Mirror.

更多推荐

本文发布于:2023-04-28 05:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   转换为   NSDictionary   class   custom

发布评论

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

>www.elefans.com

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