Search code examples
iosobjective-ccocoarealmrealm-mobile-platform

How to use RLMBool property correctly in realm-cocoa?


There declared a Bool property in subclasses of RLMObject look like so:

@interface PhotoRealm : RLMObject

@property NSNumber<RLMBool> *isVoted;

- (id)initWithMantleModel:(PhotoModel *)photoModel;

@end

In .m file ,I have implemented -defaultPropertyValues to init like this:

+ (NSDictionary *)defaultPropertyValues {
    return @{@"isVoted" : @NO};
}

In somewhere, I need to do something by jungle the bool value, but the result is not expected. Then, I write the following debug code:

   if (photoRealm.isVoted) {
        NSLog(@"isVoted");
    } else {
        NSLog(@"unVoted");
    }

    NSLog(@"%ld", (NSInteger)photoRealm.isVoted);

log as following:

[15:32:43] -[DTCollectionViewCell setupContentWithPhotoModel:] [306 line] isVoted [15:32:43] -[DTCollectionViewCell setupContentWithPhotoModel:] [311 line] 4646266992

in the meantime, the snapshot of realm file as so:

the snapshot of realm file

I'm stuck in here.


Solution

  • I think I has resolved it.

    Actually, isVoted is NSNumber, so the code should like this :

    if ([photo.isVoted integerValue]) {
            NSLog(@"isVoted");
        } else {
            NSLog(@"unVoted");
        }
    
        NSLog(@"%ld", [photo.isVoted integerValue]);