RLMException: 无法使用不兼容的类型保留 属性 'id',但不存在

RLMException: Can't persist property 'id' with incompatible type, but nonexistant

我正在尝试新建此对象的一个​​实例:

class MyAwesomeRealmObject: BaseRealmObject {

    var field:     String = ""
    var thing:     String = ""
    var whatever:  String = ""
    var something: String = ""
    var key:       String = ""

    var cards = [OtherRealmObject]()//wasnt this either

    override static func primaryKey() -> String? {
        return "key"
    }

//    override static func ignoredProperties() -> [AnyObject] {
//        return ["id"] //this didnt work either
//    }

}

这是父级 class

class BaseRealmObject: Object {
    //nothing really here, just extending realm's Object
}

我得到了这个运行时异常。

2015-08-19 10:03:53.388 app[84571:2188559] *** Terminating app due to uncaught exception 'RLMException', reason: 'Can't persist property 'id' with incompatible type. Add to ignoredPropertyNames: method to ignore.'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000110913c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001105acbb7 objc_exception_throw + 45
2   Realm                               0x000000010ff0d786 -[RLMProperty initSwiftPropertyWithName:indexed:property:instance:] + 879
3   Realm                               0x000000010ff064e6 +[RLMObjectSchema propertiesForClass:isSwift:] + 519
4   Realm                               0x000000010ff056b6 +[RLMObjectSchema schemaForObjectClass:] + 419
5   Realm                               0x000000010ff3de12 +[RLMSchema initialize] + 805
6   libobjc.A.dylib                     0x00000001105ad4d6 _class_initialize + 648
7   libobjc.A.dylib                     0x00000001105b66e1 lookUpImpOrForward + 351
8   libobjc.A.dylib                     0x00000001105c30d3 objc_msgSend + 211
9   Realm                               0x000000010ff02c44 -[RLMObjectBase init] + 112
10  RealmSwift                          0x000000010ce29573 _TFC10RealmSwift6ObjectcfMS0_FT_S0_ + 67
11  realmio                             0x000000010c0c3741 _TFC7realmio11RealmObjectcfMS0_FT_S0_ + 17
12  realmio                             0x000000010c0b4aa5 _TFC7realmio8UserDatacfMS0_FT_S0_ + 309
13  realmio                             0x000000010c0b495c _TFC7realmio8UserDataCfMS0_FT_S0_ + 44
14  realmio                             0x000000010c0be05d _TFC7realmio18UserDataSerializer13provideObjectfS0_FT_PSs9AnyObject_ + 29
15  realmio                             0x000000010c0a49cf _TFC7realmio14DataSerializer3mapfS0_FTCSo12NSDictionary7successFT6objectPSs9AnyObject__T_18dataStructureErrorFT_T__T_ + 175
16  realmio                             0x000000010c0a508b _TTWC7realmio14DataSerializerS_15IDataSerializerS_FS1_3mapUS1___fQPS1_FTCSo12NSDictionary7successFT6objectPSs9AnyObject__T_18dataStructureErrorFT_T__T_ + 91
17  realmio                             0x000000010c138010 _TFFC7realmio15BaseDataService7executeFS0_FTFPSs9AnyObject_T_23handlesFailedValidationFGSaSS_T__T_U_FCSo12NSDictionaryT_ + 560
18  realmio                             0x000000010c26798b _TFFC7realmio3API8dispatchFS0_FT_T_U_FTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T_ + 1355
19  realmio                             0x000000010c0f156e _TFFC7realmio7Request12responseJSONFDS0_FT7optionsVSC20NSJSONReadingOptions17completionHandlerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T__DS0_U_FTS2_GSqS3__GSqPS4___GSqS5___T_ + 126
20  realmio                             0x000000010c0e8493 _TFFFC7realmio7Request8responseFDS0_FT5queueGSqCSo8NSObject_10serializerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqCSo6NSData__TGSqPSs9AnyObject__GSqCSo7NSError__17completionHandlerFTS2_GSqS3__GSqPS5___GSqS6___T__DS0_U_FT_T_U_FT_T_ + 403
21  realmio                             0x000000010c0a7c77 _TTRXFo__dT__XFdCb__dT__ + 39
22  libdispatch.dylib                   0x00000001112c3186 _dispatch_call_block_and_release + 12
23  libdispatch.dylib                   0x00000001112e2614 _dispatch_client_callout + 8
24  libdispatch.dylib                   0x00000001112caa1c _dispatch_main_queue_callback_4CF + 1664
25  CoreFoundation                      0x000000011087b1f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
26  CoreFoundation                      0x000000011083cdcb __CFRunLoopRun + 2043
27  CoreFoundation                      0x000000011083c366 CFRunLoopRunSpecific + 470
28  GraphicsServices                    0x0000000112a71a3e GSEventRunModal + 161
29  UIKit                               0x000000010da098c0 UIApplicationMain + 1282
30  realmio                             0x000000010c1da607 main + 135
31  libdyld.dylib                       0x0000000111316145 start + 1
)

我什至没有字段名称 'id'。我已将其添加到 ignoredProperties(),不知道还能做什么。

编辑:它发生在实体上什至没有任何字段

我尝试添加 dynamic 修饰符,但没有用。只是再排除一些问题,我把所有字段都炸掉了。

class MyAwesomeRealmObject: RLMObject {

}

而且我仍然收到有关 'id' 的运行时异常。

编辑 2:有关异常的更多信息

我想知道这是否会阐明它

2015-08-19 12:27:51.658 realmio[5728:52676] Exception name=RLMException, reason=Can't persist property 'id' with incompatible type. Add to ignoredPropertyNames: method to ignore., userInfo={
    RLMRealmCoreVersion = unknown;
    RLMRealmVersion = "0.94.1";
} addresses=(0x112f16c4d 0x112bafbb7 0x111e36786 0x111e2f4e6 0x111e2e6b6 0x111e66e12 0x112bb04d6 0x112bb96e1 0x112bc60d3 0x111e2bc44 0x111e2ab6d 0x10e6b6b21 0x10e6b6b5c 0x10e6bfe6d 0x10e6a76ef 0x10e6a7dab 0x10e73a340 0x10e86ba1b 0x10e6f3a1e 0x10e6ea943 0x10e6aa997 0x113fab186 0x113fca614 0x113fb2a1c 0x112e7e1f9 0x112e3fdcb 0x112e3f366 0x114390a3e 0x10ffad8c0 0x10e7ddcc7 0x113ffe145)

RLMRealmCoreVersion = 未知。嗯嗯

你更新模型结构了吗? 如果这样做,请尝试在 application:didFinishLaunchingWithOptions

中调用迁移函数
[RLMRealm setSchemaVersion:1
        forRealmAtPath:[RLMRealm defaultRealmPath]
    withMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
    // Nothing to do!
    // Realm will automatically detect new properties and removed properties
    // And will update the schema on disk automatically
   }
}];

您在模型属性之前缺少 dynamic 关键字:

class MyAwesomeRealmObject: BaseRealmObject {

    dynamic var field:     String = ""
    dynamic var thing:     String = ""
    dynamic var whatever:  String = ""
    dynamic var something: String = ""
    dynamic var key:       String = ""

    let cards = List<OtherRealmObject>()

    override static func primaryKey() -> String? {
        return "key"
    }
}

事实证明,Realm 以惰性方式解析我模型中的所有实体。 'id' 错误是因为另一个实体的 ID 类型为 UInt,不受支持。

我将不同实体上的那个字段更改为类型Int,这是Realm支持的。

现在我收到有关其他字段的错误,但至少我现在知道去哪里找了。