Reputation: 530
I am using Realm for a small, simple project. I'm using the latest version of the framework (compiled from Github yesterday) and the current AppStore version of Xcode with Swift 2.1.
I am handing a Realm database object over from one view controller to another via a segue in prepareForSegue()
, in order to edit it (and fill out the current values).
Somehow, there seems to be a problem with the object in the target view controller. When I simply print()
the object in the target view controller (either in viewDidLoad()
or any of the methods I plan to use it in), the object is properly shown in the console like so:
Object {
name = asdf
value = 8
}
and so on.
If I try to access the values (even in the previous or next line) and assign them to another variable like myValue = objectToEdit.value
I get 0.
Debugging the code by stepping into it with breakpoints shows the object in the debugger with all values empty ("") or 0, depending on the type. Still, somehow Strings are grabbed out of it for some reason, I can access and print them with myString = objectToEdit.name
, while the debugger shows name = (String) ""
, claiming it's empty.
The same behavior already occurs when I get the database object out of the database in the first view. The immediate thought would be, that the values are indeed 0 and only the strings were saved, but the print()
method says otherwise.
Can anybody explain this odd behavior and maybe show me where I overlooked something? I now suspect it's some internal problem of the framework, but I'm not sure. I'll of course update with more code or answer questions, if needed.
Upvotes: 3
Views: 2172
Reputation: 530
I have found the problem when I noticed that the values, oddly enough, were the "default" values I put in the definition of the model.
For some reason, I did not use the dynamic
declaration on some of the properties, like so:
class myObject: Object {
dynamic var name = ""
var value = 0
}
This caused the new Object that was created in the view controller to have these standard values and I always got the 0, but on the other hand could get the correct Strings.
Adding dynamic
to the variable declaration fixed my problems.
Thanks for your answers.
Upvotes: 8
Reputation: 7806
You will need the LLDB plugin to see correct values while debugging your code. Install it as advised in the first code comments 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
Upvotes: 0