Repository: realm/realm-swift
File: tools/RealmBrowser/RealmBrowser/Classes/RLMInstanceTableViewController.m (Objective-C)

Code before:
        
        RLMTypeNode *displayedType = [self displayedType];
        
        RLMClazzProperty *clazzProperty = displayedType.propertyColumns[columnIndex];
        NSString *propertyName = clazzProperty.name;
        RLMObject *selectedInstance = [displayedType instanceAtIndex:rowIndex];
        NSObject *propertyValue = selectedInstance[propertyName];
        
        switch (clazzProperty.type) {
            case RLMPropertyTypeInt:
            case RLMPropertyTypeBool:
            case RLMPropertyTypeFloat:
            case RLMPropertyTypeDouble:
                if ([propertyValue isKindOfClass:[NSNumber class]]) {
                    return propertyValue;
                }
                break;
                
                
            case RLMPropertyTypeString:
                if ([propertyValue isKindOfClass:[NSString class]]) {
                    return propertyValue;
                }
                break;
                
            case RLMPropertyTypeData:
                return @"<Data>";
                
            case RLMPropertyTypeAny:
                return @"<Any>";
                
            case RLMPropertyTypeDate:
                if ([propertyValue isKindOfClass:[NSDate class]]) {
                    return propertyValue;
                }
                break;
                
            case RLMPropertyTypeArray: {
                RLMArray *referredObject = (RLMArray *)propertyValue;
                return [NSString stringWithFormat:@"List of links to %@", referredObject.objectClassName];
            }
                
            case RLMPropertyTypeObject: {
                RLMObject *referredObject = (RLMObject *)propertyValue;
                if (referredObject == nil) {
                    return @"";
                }
                else {
                    return [NSString stringWithFormat:@"Link to %@", referredObject.objectSchema.className];
                }
            }

Review comment (bug):
I see what you mean. We are dealing with NSNumber bools here and not BOOL. However, casting it to BOOL does seem to work:

-(NSString *)test1Bool:(id)aBool {
    return (BOOL)aBool ? @"TRUE" : @"FALSE";
}

-(NSString *)test2Bool:(id)aBool {
    return (aBool != nil) ? @"TRUE" : @"FALSE";
}

-(NSString *)test3Bool:(id)aBool {
    return [(NSNumber *)aBool boolValue] ? @"TRUE" : @"FALSE";
}

-(NSString *)test4Bool:(id)aBool {
    if (!aBool) {
        return @"-----";
    }
    return [(NSNumber *)aBool boolValue] ? @"TRUE" : @"FALSE";
}

-(NSString *)test5Bool:(id)aBool {
    // Catches both aBool == nil and aBool.class != NSNumber
    if (![aBool isKindOfClass:[NSNumber class]]) {
        return @"-----";
    }
    return [(NSNumber *)aBool boolValue] ? @"TRUE" : @"FALSE";
}

with output:

2014-08-05 20:02:05.485 Realm Browser[10103:303] == Test1 ==
2014-08-05 20:02:05.486 Realm Browser[10103:303] nil: FALSE
2014-08-05 20:02:05.486 Realm Browser[10103:303] @NO: FALSE
2014-08-05 20:02:05.486 Realm Browser[10103:303] @YES: TRUE
2014-08-05 20:02:05.486 Realm Browser[10103:303] == Test2 ==
2014-08-05 20:02:05.486 Realm Browser[10103:303] nil: FALSE
2014-08-05 20:02:05.486 Realm Browser[10103:303] @NO: TRUE
2014-08-05 20:02:05.486 Realm Browser[10103:303] @YES: TRUE
2014-08-05 20:02:05.486 Realm Browser[10103:303] == Test3 ==
2014-08-05 20:02:05.486 Realm Browser[10103:303] nil: FALSE
2014-08-05 20:02:05.487 Realm Browser[10103:303] @NO: FALSE
2014-08-05 20:02:05.487 Realm Browser[10103:303] @YES: TRUE
2014-08-05 20:02:05.487 Realm Browser[10103:303] == Test4 ==
2014-08-05 20:02:05.487 Realm Browser[10103:303] nil: -----
2014-08-05 20:02:05.487 Realm Browser[10103:303] @NO: FALSE
2014-08-05 20:02:05.487 Realm Browser[10103:303] @YES: TRUE
2014-08-05 20:02:05.487 Realm Browser[10103:303] == Test5 ==
2014-08-05 20:02:05.487 Realm Browser[10103:303] nil: -----
2014-08-05 20:02:05.487 Realm Browser[10103:303] @NO: FALSE
2014-08-05 20:02:05.488 Realm Browser[10103:303] @YES: TRUE

There is another problem though, nil and @NO give the same output, nil should probably give "" instead. So i propose using 4 or five instead. Casting to BOOL is wrong, even if it works.


Code after:
        RLMTypeNode *displayedType = [self displayedType];
        RLMClazzProperty *clazzProperty = displayedType.propertyColumns[columnIndex];
        
        NSString *propertyName = clazzProperty.name;
        RLMObject *selectedInstance = [displayedType instanceAtIndex:rowIndex];
        
        NSObject *propertyValue = selectedInstance[propertyName];

        return [self.class printablePropertyValue:propertyValue ofType:clazzProperty.type];
    }
    
    return nil;
}

+(id)printablePropertyValue:(id)propertyValue ofType:(RLMPropertyType)propertyType
{
    return [self printablePropertyValue:propertyValue ofType:propertyType linkFormat:NO];
}

+(id)printablePropertyValue:(id)propertyValue ofType:(RLMPropertyType)propertyType linkFormat:(BOOL)linkFormat
{
    switch (propertyType) {
        case RLMPropertyTypeBool:
            if ([propertyValue isKindOfClass:[NSNumber class]]) {
                if (linkFormat) {
                    return (BOOL)propertyValue ? @"TRUE" : @"FALSE";
                }
                return propertyValue;
            }
            break;

        case RLMPropertyTypeInt:
        case RLMPropertyTypeFloat:
        case RLMPropertyTypeDouble:
            if ([propertyValue isKindOfClass:[NSNumber class]]) {
                return propertyValue;
            }
            break;
            
        case RLMPropertyTypeString:
            if ([propertyValue isKindOfClass:[NSString class]]) {
                return propertyValue;
            }
            break;
        
        case RLMPropertyTypeData:
            if (linkFormat) {
                return @"<Data>";
            }
            
            // Will eventually return an image if present