0

从 Realm 0.95.3 升级到 Realm 0.96.3

RLMObjectStore.mm:106 内出现应用程序错误

抛出错误,说明属性已成为可选

    (lldb) po objectSchema
DTFLogMessage {
    id {
        type = string;
        objectClassName = (null);
        indexed = YES;
        isPrimary = YES;
        optional = YES;
    }
    creationDate {
        type = date;
        objectClassName = (null);
        indexed = NO;
        isPrimary = NO;
        optional = YES;
    }
    message {
        type = string;
        objectClassName = (null);
        indexed = NO;
        isPrimary = NO;
        optional = YES;
    }
    fileinfo {
        type = string;
        objectClassName = (null);
        indexed = NO;
        isPrimary = NO;
        optional = YES;
    }
    type {
        type = int;
        objectClassName = (null);
        indexed = NO;
        isPrimary = NO;
        optional = NO;
    }
}

如何使这些再次成为非可选我在文档中看不到有关如何执行此操作的任何内容。模型配置如下:

#import <Realm/RLMObject.h>

@interface DTFLogMessage : RLMObject

@property NSString *id;
@property NSDate *creationDate;
@property NSString *message;
@property NSString *fileinfo;
@property NSInteger type;

@end

RLM_ARRAY_TYPE(DTFLogMessage)

.m 文件如下。

#import "DTFLogMessage.h"

@implementation DTFLogMessage

+ (NSString*)primaryKey
{
    return @"id";
}

@end
4

1 回答 1

1

Realm 关于可选属性的 Objective-C 文档解释了如何做到这一点:

默认情况下,NSString *NSData *NSDate *properties 允许您将它们设置为 nil。如果你想要求一个值存在,你可以覆盖+requiredProperties你的 RLMObject 子类的方法。例如,使用以下模型定义,尝试将人名设置为 nil 将引发异常,但允许将其生日设置为 nil:

@interface Person : RLMObject
@property NSString *name;
@property NSDate *birthday;
@end

@implementation Person
+ (NSArray *)requiredProperties {
    return @[@"name"];
}
@end
于 2015-12-10T18:10:04.523 回答