自定义类的字典数组

Posted

技术标签:

【中文标题】自定义类的字典数组【英文标题】:Array of Dictionaries to Custom Class 【发布时间】:2017-09-29 05:31:39 【问题描述】:

我已成功将我的 Firebase 数据提取到字典中,但我很难将这些值放入我的自定义类中。

要从 Firebase 获取我的数据,我使用以下代码:

func testing(completion: @escaping ([String : Any]) -> ()) 

    ref.child("dailyJobs").observe(.childAdded, with:  (snapshot) in
        if let value = snapshot.value as? [String : Any] 
            completion(value as [String : Any])
            return
        
        completion([:])
    )

当我调用函数时

testing  (dictionary) in
        print(dictionary)
    

我从 Firebase 得到的字典是这样的(它是一个字典数组):

["name": custom chore list, "multiplier": 1, "assigned": none, "order": 0]
["name": bathrooms, "multiplier": 1, "assigned": none, "order": 1]
["name": laundry, "multiplier": 1, "assigned": none, "order": 2]
["name": living room, "multiplier": 1, "assigned": none, "order": 3]
["name": sweep & vacuum, "multiplier": 1, "assigned": sweep & vacuum, "order": 4]
["name": wipe table, "multiplier": 1, "assigned": none, "order": 5]
["name": counters, "multiplier": 1, "assigned": none, "order": 6]
["name": dishes, "multiplier": 1, "assigned": none, "order": 7]
["name": meal prep, "multiplier": 1, "assigned": none, "order": 8]
["name": feed pet / garbage, "multiplier": 1, "assigned": none, "order": 9]

我的自定义类是这样的:

class JobsAndHabits 

var name: String
var multiplier: Double
var assigned: String
var order: Int

init(jobName: String, jobMultiplier: Double, jobAssign: String, jobOrder: Int) 

    self.name = jobName
    self.multiplier = jobMultiplier
    self.assigned = jobAssign
    self.order = jobOrder

我解析dictionary的代码是这样的:

testing  (dictionary) in
        for item in dictionary 
            print(item)
            let multiplier = dictionary["multiplier"] as! Double
            let name = dictionary["name"] as! String
            let assigned = dictionary["assigned"] as! String
            let order = dictionary["order"] as! Int

            let dailyJob = JobsAndHabits(jobName: name, jobMultiplier: multiplier, jobAssign: assigned, jobOrder: order)
            self.dailyJobs.append(dailyJob)
        
        print(self.dailyJobs)

我得到了这个:

[ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits, ToDo_App.JobsAndHabits]

每个字典都有 4 个。不知何故,我的代码将数组复制了 4 倍,现在我的头疼了。我确信有一个简单的解决方案。

我也试过了:

将所有代码放入原始闭包中,如下所示:

    func loadExistingJobs(_ completion: @escaping () -> ()) 
    ref.child("dailyJobs").observe(.childAdded, with:  (snapshot) in
        if let dictionary = snapshot.value as? [String : Any] 
            let multiplier = dictionary["multiplier"] as! Double
            let name = dictionary["name"] as! String
            let assigned = dictionary["assigned"] as! String
            let order = dictionary["order"] as! Int

            let dailyJob = JobsAndHabits(jobName: name, jobMultiplier: multiplier, jobAssign: assigned, jobOrder: order)
            self.dailyJobs.append(dailyJob)
            self.dailyJobs.sort(by: $0.order < $1.order)

            self.jobsTableView.reloadData()
            completion()
        
    )

但是@escaping 闭包没有正常运行,我的后续代码也没有被执行。

TL;DR - 我想从 Firebase 中提取数据并将其附加到我的数组中。

【问题讨论】:

【参考方案1】:

请检查:

您的dictionary["name": custom chore list, "multiplier": 1, "assigned": none, "order": 0],您正在为此编写循环。它有 4 个键,所以它会循环 4 次。

如果你的字典像 [String: Any],那么你必须像下面这样调用:

testing  (dictionary) in
    let multiplier = Double(dictionary["multiplier"] as! Int)
    let name = dictionary["name"] as! String
    let assigned = dictionary["assigned"] as! String
    let order = dictionary["order"] as! Int

    let dailyJob = JobsAndHabits(jobName: name, jobMultiplier: multiplier, jobAssign: assigned, jobOrder: order)
    self.dailyJobs.append(dailyJob)

    print(self.dailyJobs)
)

如果你的字典像 [[String: Any]],那么你必须像下面这样调用:

func testing(completion: @escaping ([[String : Any]]) -> ()) 
    let dictionary = [[String: Any]]
    ref.child("dailyJobs").observe(.childAdded, with:  (snapshot) in
        for child in (snapshot?.children)! 
            let snap = child as! FDataSnapshot //each child is a snapshot
            if let value = snap.value as? [String : Any] 
                dictionary.append(value)   
            
        
        completion(dictionary)
    )


testing  (dictionary) in
    for item in dictionary 
        print(item)
        let multiplier = Double(item["multiplier"] as! Int)
        let name = item["name"] as! String
        let assigned = item["assigned"] as! String
        let order = item["order"] as! Int
        let dailyJob = JobsAndHabits(jobName: name, jobMultiplier: multiplier, jobAssign: assigned, jobOrder: order)
        self.dailyJobs.append(dailyJob)
        //            
        print(self.dailyJobs)
    
)

【讨论】:

是的,那我该如何避免呢?我难住了。如何将这 四个 键放入我的 one 类对象中? 请检查我的代码。删除了 for 循环。现在你会得到正确的结果。 崩溃了。 Unexpectedly found nil while unwrapping an Optional Value 第 1 行:let multiplier = dictionary... 没有。您正在尝试将 Any 转换为 Double。所以它失败了。你的 multiplier 是 Double 并且它在字典中的值是 Int。 好的。搞定了但是现在即使在我调用代码之后数据仍在填充。当我打印到控制台dailyJobs.count 时,我得到1, 2, 3, 4, 5, 6, 7, 8, 9, 10。这意味着代码仍在运行。它应该只是10。我的完成处理程序错了吗?【参考方案2】:

你可以试试这段代码,根据你的需要修改

class JobsAndHabits 

    var name: String
    var multiplier: Double
    var assigned: String
    var order: Int

    init(jobName: String, jobMultiplier: Double, jobAssign: String, jobOrder: Int) 

        self.name = jobName
        self.multiplier = jobMultiplier
        self.assigned = jobAssign
        self.order = jobOrder
    
    class func getAllJobsAndHabbits(forUserID userID: String, completion: @escaping (JobsAndHabits) -> Swift.Void, failure: @escaping () -> ()) 
        if Auth.auth().currentUser != nil 
            Database.database().reference().child("jobsAndHabits").child(userID).observe(.childAdded, with:  (snapshot) in
                if snapshot.exists() 
                    let receivedMessage = snapshot.value as! [String: Any]

                    let name = receivedMessage["content"] as? String ?? ""
                    let multiplier = receivedMessage["multiplier"] as? Double ?? 0.0
                    let assigned = receivedMessage["assigned"] as? String ?? ""
                    let order = receivedMessage["order"] as? Int ?? 0
                    completion(JobsAndHabits(jobName: name, jobMultiplier: multiplier, jobAssign: assigned, jobOrder: order))
                 else 
                    failure()
                
            )
         else 
            failure()
        
    

调用方式和处理方式

class Test 
   let dailyJobs:[JobsAndHabits] = []

   JobsAndHabits.getAllJobsAndHabbits(forUserID: "12346", completion: 
       self.dailyJobs.append(dailyJob)
       self.dailyJobs.sort(by: $0.order < $1.order)
    ) 
        //handle error
    

【讨论】:

嗯.. dailyJobs 仍在异步加载。我想在来自数据库的最终调用中运行代码,而不是增量。你看,我想附加另一个基于 dailyJobs 列表的数组。

以上是关于自定义类的字典数组的主要内容,如果未能解决你的问题,请参考以下文章

自定义类的数组作为属性

包含自定义类的数组总和(Swift)

Swift:在 Core Data 中存储自定义类的数组

如何在 swift 3 中将具有自定义类的数组转换为 JSON

java创建自定义类的数组

1.元类介绍/2.自定义元类控制类的行为/3.控制类的实例化行为/4.控制类的实例化行为的应用