使用Realm获取单击的单元格引用对象

时间:2015-08-27 16:48:20

标签: ios swift realm

我在我的swift应用程序中使用Realm进行本地存储。 在我的TableViewController的顶部,我有这个:

var rides : Results<Rides>!

然后,在viewDidLoad中我有这个:

rides = Realm().objects(Rides)

当我这样做时,数据完全加载到我的单元格中:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let customCell = tableView.dequeueReusableCellWithIdentifier("ridesCell", forIndexPath: indexPath) as! RidesViewCell

        var ride: Rides = rides[indexPath.row]
        customCell.setRide(ride)  // This just sets labels in the cell...
        return customCell

    }

当用户点击一个单元格时,我想获取我的乘坐ID。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         dump(rides[indexPath.row])
        var ride: Rides = rides[indexPath.row]
        self.selectedRideId = ride.id
        self.performSegueWithIdentifier("ridesListToMap", sender: self)
    }

但我的selectedRideId总是空白的......我不知道为什么? 我有什么想法,我做错了什么?

以上是转储的结果:

▿ Gleam.Rides #0
  ▿ super: RealmSwift.Object
    - super: Rides {
    id = B3C78163-30D4-445B-A2D7-B1D447AF3037;
    distance = 0.09266282501476202;
    avgSpeed = 24.65913043478261;
    duration = 21.78935199975967;
    startDate = 2015-08-27 16:09:28 +0000;
    endDate = 2015-08-27 16:09:50 +0000;
    userId = A32C8DDB-9A31-46AC-8264-0E5A12FDD157;
}
  - id: 
  - distance: 0.0
  - avgSpeed: 0.0
  - duration: 0.0
  - startDate: Aug 27, 2015, 6:40 PM
  - endDate: Aug 27, 2015, 6:40 PM
  - userId: 

所以它得到了正确的结果,但后来没有给我ID(不知道为什么它被列出2次......

1 个答案:

答案 0 :(得分:2)

看起来输出来自调试器 - 因为Realm调用了对象的属性访问器,实例变量不一定会被设置。你最好检查对象的description属性。

相关问题