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.