如何在UITableViewController中正确使用Realm对象?

时间:2015-04-05 10:22:12

标签: ios swift realm

我正在尝试在我的UITableViewController中使用Realm,每当我尝试在行索引中找到对象时,如果我将对象转换为其类,我就会遇到问题(如果我使用了错误的术语,请原谅我,我仍然是Swift,Realm和iOS开发的新手!)...

我有一个Site类看起来像这样,数据库有几千个条目:

class Site: RLMObject {
    var id: String = ""
    var name: String = ""
}

在我的表视图控制器中,当我尝试根据结果集中的索引获取Site以加载到单元格中时,如果我尝试将其转换为Site对象,它总是零!如果我使用AnyObject设置它,那么我可以看到确实找到了该索引的正确站点。

我猜测.name上的AnyObject调用仅在AnyObject响应.name,但它有助于我看到索引是正确的该网站确实存在......

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let allSites = Site.allObjects()
    var any: AnyObject = allSites.objectAtIndex(UInt(indexPath.row))
    var site = allSites.objectAtIndex(UInt(indexPath.row)) as? Site
    println("With AnyObject: \(any.name)")
    println("With casting: \(site?.name)")

    return cell
}

上面的print语句的结果如下所示(例如在名为'Addeboda'的网站上):

With AnyObject: Addeboda
With casting: Optional("")

我做错了吗?我已经google了一下,我可以在类似的行中找到的所有教程和答案都表明results.objectAtIndex(index) as Class应该是正确的方法。

2 个答案:

答案 0 :(得分:3)

不需要演员

似乎不需要向网站投射。这很好用:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let allSites = Site.allObjects()
    let site: AnyObject! = allSites[UInt(indexPath.row)]

    cell.textLabel!.text = site.name

    println("Site is: \(site.id)")

    return cell
}

似乎是Swift或Realm的错误。我猜他们中的一个人在向{.1}}投降时会感到困惑。

使用正确的类型

初始化新实例

但是,如果您确实需要使用AnyObject!模型类,则可以从结果中初始化新的Site

RLMObject

首先尝试

听说你遇到Realm和Swift的问题是很遗憾的。我绝不是一个Swift专业版,但看起来你正在将override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let allSites = Site.allObjects() let site = Site(object: allSites[UInt(indexPath.row)]) cell.textLabel!.text = site.name println("Site is: \(site.id)") return cell } 转换为可选项,而使用可选的强制转换运算符site的结果也是可选的。因此得到site?.name

你可以试着看看下面的运气是否更好?

Optional("")

此外,您可以使用func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let allSites = Site.allObjects() if var site = allSites[UInt(indexPath.row)] as? Site { println("Site is: \(site.name)") } else { println("it not castable to Site. It is: \(toString(allSites[UInt(indexPath.row)].dynamicType))") } return cell } 来获取对象真实类类型的引用。

祝你好运

答案 1 :(得分:2)

以下是tableview sample project的一些代码:

class TableViewController: UITableViewController {

    var array = DemoObject.allObjects().sortedResultsUsingProperty("date", ascending: true)
    ...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell

        let object = array[UInt(indexPath.row)] as DemoObject
        cell.textLabel?.text = object.title
        cell.detailTextLabel?.text = object.date.description

        return cell
    }
}

您应该能够在存储indexPath.row对象

的行上进行强制转换