2015-08-27 67 views
1

我在我的swift应用程序中使用Realm作为本地存储。 在我TableViewController顶部我有这样的:使用Realm获取单击的单元格引用对象

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倍......

回答

2

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

+0

谢谢,但我是一个noob,你可以发展一点通过检查对象的“description”属性来表达更多的意思吗? – denislexic

+0

'println(object.description)'应该这样做! – segiddins

相关问题