How to use Realm objects in UITableViewController correctly?

I try to use Realm in my UITableViewController, and I run into problems whenever I try to find an object in the row index, if I drop the object into my class (ask me if I use the wrong terminology, I'm still pretty new to Swift, Realm and iOS dev!) ...

I have a class Sitethat looks like this: the database contains several thousand records:

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

In my table view controller, when I try to get Sitebased on its index in the result set to load into a cell, if I try to apply it to an object Site, it is always zero! If I let him install using AnyObject, then I can see that the correct site in this index is indeed found.

I assume that calling .nameon AnyObjectonly works because it AnyObjectanswers .name, but it helps me see that the index is correct and that the site exists ...

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
}

The result of the above print statements looks like this (for example, on a site named Addeboda):

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

Am I doing something wrong? I was looking a bit for the language, and all the training materials and answers that I can find in similar lines show what results.objectAtIndex(index) as Classshould be right.

+4
2

, . :

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. , , AnyObject! -.

, Site, RLMObject :

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
}

, Realm Swift. Swift pro, , 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
}

, yourObject.dynamicType .

+3

:

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

+1

All Articles