Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setObjectForKey: key cannot be nil #17

Closed
joeblau opened this issue Dec 8, 2014 · 7 comments
Closed

setObjectForKey: key cannot be nil #17

joeblau opened this issue Dec 8, 2014 · 7 comments
Assignees

Comments

@joeblau
Copy link

joeblau commented Dec 8, 2014

Running the code below causes setObjectForKey: key cannot be nil in Swift. I'm not sure if I'm actually using this correctly or not, but I can't seem to get any of the category functionality working.

    var grp = GroupRLMObject()
    grp.name = "David Foster Wallace"

    // Get the default Realm
    let realm = RLMRealm.defaultRealm()
    // You only need to do this once (per thread)

    // Add to the Realm inside a transaction
    realm.beginWriteTransaction()
    realm.addObject(grp)
    realm.commitWriteTransaction()

    let results: NSDictionary = grp.JSONDictionary() as NSDictionary
@matthewcheok
Copy link
Owner

Do you mind posting your class definition for GroupRLMObject?

@joeblau
Copy link
Author

joeblau commented Dec 9, 2014

class GroupRLMObject: RLMObject {
    dynamic var id = ""
    dynamic var name = ""
    dynamic var color = ""
    dynamic var on: Bool = false
    dynamic var createdAt = NSDate()
    dynamic var modifiedAt = NSDate()
}

@matthewcheok
Copy link
Owner

To use the JSONDictionary() method, you need to implement class func JSONOutboundMappingDictionary() -> NSDictionary, which maps from properties in your object to the JSON keys you want.

@joeblau
Copy link
Author

joeblau commented Dec 10, 2014

So is this quote from the documentation still true?

Leaving out either one of the above will result in a mapping that assumes camelCase for your properties which map to snake_case for the JSON equivalents.

I thought that if i didn't define anything, I would get some boilerplate JSON using default mapping.

@matthewcheok
Copy link
Owner

There seems to be a problem with defining the model in Swift.
This works for me (the default mapping works):

@interface GroupRLMObject : RLMObject

@property NSString *id;
@property NSString *name;
@property NSString *color;
@property BOOL on;
@property NSDate *createdAt;
@property NSDate *modifiedAt;

@end

@implementation GroupRLMObject

+ (NSDictionary *)defaultPropertyValues {
    return @{
               @"id": @"",
               @"name": @"",
               @"color": @"",
               @"on": @(NO),
               @"createdAt": [NSDate date],
               @"modifiedAt": [NSDate date]
    };
}

@end

@matthewcheok matthewcheok reopened this Dec 10, 2014
@matthewcheok matthewcheok self-assigned this Dec 10, 2014
@matthewcheok
Copy link
Owner

This issue should now be fixed in the latest release 0.2.3

@joeblau
Copy link
Author

joeblau commented Dec 10, 2014

👍 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants