问题
I have a RealmObject model that my tableViewDataSource depends on. The cellForRowAtIndexPath1 method is able to access the properties fine, but the didSelectRowAtIndexPath gets all empty properties from the Realm object. I'm guessing this has to do with passing persisted Realm objects around but I'm not sure where to fix it.
//viewController...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let realm = try! Realm()
let books = realm.objects(Book)
viewModel = BookListViewModel(books: Array(books), onSelection: onSelection, onDeleteFailure: onDeleteFailure)
}
//end viewController
extension BookListViewModel: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(BookTableViewCell.nibName, forIndexPath: indexPath) as! BookTableViewCell
cell.bind(bookCellViewModels[indexPath.row])
// NOTE:
// bookCellViewModels[indexPath.row].book all properties are valid strings
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bookCellViewModels.count
}
}
extension BookListViewModel: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let book = bookCellViewModels[indexPath.row].book
// NOTE:
// all book properties are empty string or nil
onSelection(book)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
回答1:
Your debugger is likely showing you all empty property values for the Realm object, because you're missing the LLDB plugin.
Install Realm Xcode plugin
via Alcatraz
The LLDB plugin comes bundled with the Realm Xcode plugin, which is available on Alcatraz.
Install Alcatraz, if not already installed.
curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | shOpen the Package Manager in Xcode (⇧⌘9 or Windows > Package Manager)
Search for RealmPlugin
- Quit and relaunch Xcode to make sure the plugin is loaded
Compile yourself
You can also install the plugin manually by opening plugin/RealmPlugin.xcodeproj contained in the release zip and clicking build.
Install LLDB plugin only
Alternatively, you can install it as advised in the first code comments of the linked scripts or just execute the script below:
mkdir -p ~/Library/Application\ Support/Realm
wget -O ~/Library/Application\ Support/Realm/rlm_lldb.py https://raw.githubusercontent.com/realm/realm-cocoa/master/plugin/rlm_lldb.py
touch ~/.lldbinit
grep -q "rlm_lldb.py" ~/.lldbinit || echo "command script import "~/Library/Application Support/Realm/rlm_lldb.py" --allow-reload" >> .lldbinit
回答2:
Nevermind, it turned out to be a bug related to my view logic. I just was confused because the Xcode debugger shows all empty property values for the realm object, probably because they're all dynamic properties.
来源:https://stackoverflow.com/questions/33806204/realm-object-is-missing-all-properties-except-primarykey