Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 4/Firebase-从不同阵列中的实时数据库读取和存储双嵌套项_Ios_Swift_Firebase_Firebase Realtime Database_Casting - Fatal编程技术网

Ios Swift 4/Firebase-从不同阵列中的实时数据库读取和存储双嵌套项

Ios Swift 4/Firebase-从不同阵列中的实时数据库读取和存储双嵌套项,ios,swift,firebase,firebase-realtime-database,casting,Ios,Swift,Firebase,Firebase Realtime Database,Casting,我真的很难从firebase实时数据库读取和存储数据。 这就是我的数据库的外观: text json { "Live": { "Today": { "121321": { "title": "rnd", "description": "blah blah blah", "stamp": 1111 }, "121441": { "title": "arnd", "descr

我真的很难从firebase实时数据库读取和存储数据。 这就是我的数据库的外观:

text json
{
 "Live": {
    "Today": {
      "121321": {
        "title": "rnd",
        "description": "blah blah blah",
        "stamp": 1111
      },
      "121441": {
        "title": "arnd",
        "description": "blh blah blah",
        "stamp": 134
      }
    },
    "Tomorrow": {
      "143821": {
        "title": "brnd",
        "description": "blh blah blah",
        "stamp": 1134
      }
    },
    "ThisWeek": {
      "18934": {
        "title": "drnd",
        "description": "blh blh blah blah blah",
        "stamp": 237812
    },
      "323123": {
        "title": "crnd",
        "description": "blh blh blah blah",
        "stamp": 138921
     }
   }
  }
}

我用绿色突出显示了我所称的事件。事件具有标题、描述和戳记。然后我有一个今天、明天和本周的外巢。今天、明天和本周都是各自独立的词典,可容纳任意数量的事件

我在单独的swift文件中创建了一个类:

class Event: NSObject {

    var EventTitle: String?
    var EventDescription: String?
    var EventStamp: Int?
    }
我想要三个数组。第一个数组将包含“今天”的所有事件,第二个数组将包含“明天”的所有事件,第三个数组将包含“本周”的所有事件。我最终需要按戳记对每个数组中的事件进行排序。但是,我一直在研究如何将firebase datasnapshots转换为事件,然后将所有事件放入一个数组中。 到目前为止我的代码

//declaring empty arrays to fill later
    var todayEvents = [Event]()
    var tomorrowEvents = [Event]()
    var thisweekEvents = [Event]()

//Getting snapshots
ref?.child(".../LiveEvents").observeSingleEvent(of: .value, with: { (snapshot) in

     let todaysnap = snapshot.childSnapshot(forPath: "Today")
     let tomorrowsnap = snapshot.childSnapshot(forPath: "Tomorrow")
     let thisweeksnap = snapshot.childSnapshot(forPath: "ThisWeek")

     //need to assign data to events and to arrays Today, Tomorrow, Thisweek

     })
当我尝试打印快照数据时,我会得到一个可选值,如果我尝试使用as字符串分配变量,则会出现如下错误 “无法在强制中将'DataSnapshot'类型的值转换为'Int'类型。” 我看过其他答案,但我相信我的情况有所不同,因为我有双重嵌套数据,我想分配给不同的数组。我用的是swift 4

多谢各位

---如果我这样做 打印(今天的快照)我收到了

}
执行print(todaysnap.value)时,除了类型是“可选”而不是“Snap(Today)”以下是完整的代码,用于读取Live节点中的所有数据,并将今天、明天和本周事件放入单独的数组中

var todayArray = [String]()
var tomorrowArray = [String]()
var thisWeekArray = [String]()

let liveRef = self.ref.child("Live")
liveRef.observeSingleEvent(of: .value, with: { snapshot in

    let todaySnap = snapshot.childSnapshot(forPath: "Today")
    for todayChild in todaySnap.children {
        let todayChildSnap = todayChild as! DataSnapshot
        let todayDict = todayChildSnap.value as! [String: Any]
        let title = todayDict["title"] as! String
        todayArray.append(title)
    }

    let tomorrowSnap = snapshot.childSnapshot(forPath: "Tomorrow")
    for tomorrowChild in tomorrowSnap.children {
        let tomorrowChildSnap = tomorrowChild as! DataSnapshot
        let tomorrowDict = tomorrowChildSnap.value as! [String: Any]
        let title = tomorrowDict["title"] as! String
        tomorrowArray.append(title)
    }

    let thisWeekSnap = snapshot.childSnapshot(forPath: "ThisWeek")
    for thisWeekChild in thisWeekSnap.children {
        let thisWeekChildSnap = thisWeekChild as! DataSnapshot
        let thisWeekDict = thisWeekChildSnap.value as! [String: Any]
        let title = thisWeekDict["title"] as! String
        thisWeekArray.append(title)
    }

    print("Today")
    for today in todayArray {
        print("  " + today)
    }

    print("Tomorrow")
    for tomorrow in tomorrowArray {
        print("  " + tomorrow)
    }

    print("This Week")
    thisWeekArray.map { print("  " + $0) } //gettin' all swifty here
})
输出是

Today
  rnd
  arnd
Tomorrow
  brnd
This Week
  drnd
  crnd
然而。。。我可能会改变结构:

Live
  event_0  //generated with childByAutoId
    title: "rnd"
    timestamp: "20180918"
  event_1
    title: "brnd"
    timestamp: "20180919"
因为您现在可以查询每个节点,提取今天的事件。明天的活动或本周的活动或任何你喜欢的日子或范围

我每天都要上传新的数据,所以今天,明天和今天 每周将每天刷新

有了这个结构,你所需要做的就是添加节点,你的查询就会知道在什么时间段内节点是什么

既然您的事件类不做任何处理,本质上只是一个保存数据的结构,那么结构如何:

struct EventStruct {
   var EventTitle: String?
   var EventDescription: String?
   var EventStamp: Int?
}
然后将事件添加到数组中:

let title = thisWeekDict["title"] as! String
let desc = thisWeekDict["desc"] as! String
let stamp = thsWeekDict["stamp"] as! String
let anEvent = Event(title: title, desc: desc, stamp: stamp)
thisWeekArray.append(anEvent)

或者将其作为一个类,只需传入快照,并让该类从快照中分配属性。无论哪种方式,请添加错误检查以检查是否为零。

您是否尝试使用
snapshot.value
?是的,我尝试过,但它返回一个“可选值”,我无法强制转换以打印快照值。。。它必须有一些类型。好的,我把我这样做的结果放在我的问题中。print(todaysnap)给了我一个Snap(todaysnap)值,print(todaysnap.value)给了我一些选项。请将Firebase结构包含为文本,无图像。如果我们想在答案中使用它,如果它是图像,我们必须重新键入它。此外,图像不可搜索。我怀疑这种结构是否能长期为你们服务,因为今天的事件是今天的,明天的事件是昨天的。和一个问题;你想一次读入所有这些,然后分成数组,或者读入今天的事件,放入数组,明天放入数组,等等。
let title = thisWeekDict["title"] as! String
let desc = thisWeekDict["desc"] as! String
let stamp = thsWeekDict["stamp"] as! String
let anEvent = Event(title: title, desc: desc, stamp: stamp)
thisWeekArray.append(anEvent)