调用 Realm() 时出现 Realm RLM 异常

Realm RLM Exception when calling Realm()

我正在为 swift 3.1 使用 Realm,并通过 import RealmSwift import Realm 导入了它。当我在我的 swift 代码中执行此行时,我收到此错误 let realm = try? Realm()(或 ...= try! Realm()...let r = ...

>2017-08-16 19:39:53.666 PROJECT_NAME[19996:1183826] *** Terminating app due to uncaught exception 'RLMException', reason: 'Property 'members' is declared as 'NSArray', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject. See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.'
>*** First throw call stack:
>(
>   0   CoreFoundation                      0x000000010978fb0b >__exceptionPreprocess + 171
>   1   libobjc.A.dylib                     0x00000001091f4141 objc_exception_throw + 48
>   2   Realm                               0x000000010851a02e -[RLMProperty setTypeFromRawType:] + 1601
>   3   Realm                               0x000000010851a5dc -[RLMProperty initSwiftPropertyWithName:indexed:linkPropertyDescriptor:property:instance:] + 857
>   4   Realm                               0x000000010850e069 +[RLMObjectSchema propertiesForClass:isSwift:] + 695
>   5   Realm                               0x000000010850cff8 +[RLMObjectSchema schemaForObjectClass:] + 506
>   6   Realm                               0x000000010857d068 _ZL16RLMRegisterClassP10objc_class + 142
>   7   Realm                               0x000000010857d925 __25+[RLMSchema sharedSchema]_block_invoke + 12
>   8   CoreFoundation                      0x00000001097185ff __65-[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 111
>   9   CoreFoundation                      0x00000001097184fa -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 202
>   10  Realm                               0x000000010857d7ac +[RLMSchema sharedSchema] + 142
>   11  Realm                               0x0000000108573d08 +[RLMRealm realmWithConfiguration:error:] + 1148
>   12  RealmSwift                          0x0000000108ba4554 _TFC10RealmSwift5RealmcfzT_S0_ + 100
>   13  PROJECT_NAME                            0x000000010821415e _TFC8PROJECT_NAME19LoginViewController5loginfP_T_ + 1422
>   14  PROJECT_NAME                            0x0000000108215e43 _TToFC8PROJECT_NAME19LoginViewController5loginfP_T_ + 67
>   15  UIKit                               0x000000010a0a6d82 -[UIApplication sendAction:to:from:forEvent:] + 83
>   16  UIKit                               0x000000010a22b5ac -[UIControl sendAction:to:forEvent:] + 67
>   17  UIKit                               0x000000010a22b8c7 -[UIControl _sendActionsForEvents:withEvent:] + 450
>   18  UIKit                               0x000000010a22a802 -[UIControl touchesEnded:withEvent:] + 618
>   19  UIKit                               0x000000010a1147ea -[UIWindow _sendTouchesForEvent:] + 2707
>   20  UIKit                               0x000000010a115f00 -[UIWindow sendEvent:] + 4114
>   21  UIKit                               0x000000010a0c2a84 -[UIApplication sendEvent:] + 352
>   22  UIKit                               0x000000010a8a65d4 >__dispatchPreprocessedEventFromEventQueue + 2926
>   23  UIKit                               0x000000010a89e532 __handleEventQueue >+ 1122
>   24  CoreFoundation                      0x0000000109735c01 >__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
>   25  CoreFoundation                      0x000000010971b0cf >__CFRunLoopDoSources0 + 527
>   26  CoreFoundation                      0x000000010971a5ff __CFRunLoopRun + >911
>   27  CoreFoundation                      0x000000010971a016 >CFRunLoopRunSpecific + 406
>   28  GraphicsServices                    0x00000001113dca24 GSEventRunModal + >62
>   29  UIKit                               0x000000010a0a5134 UIApplicationMain + >159
>   30  PROJECT_NAME                            0x0000000108212a97 main + 55
>   31  libdyld.dylib                       0x000000010cf8465d start + 1
>)
>libc++abi.dylib: terminating with uncaught exception of type NSException
>(lldb) 

我不认为这与我的设备有任何关系,因为我的 friend/team 伙伴对我们的应用程序有同样的问题。我们都(我相信)使用 Xcode 8.3.3 和 swift 3.1。

当我们将领域放入测试应用程序并 运行 时,我们的代码确实有效。如果它有任何相关性,我们也使用 Almofire,尽管将其放入我们的测试应用程序并没有破坏任何东西。

编辑 1: 我们拥有的唯一使用领域的代​​码就是上面的一行,我们实际上没有通过领域存储任何东西

编辑 2: 以上编辑不正确,我们确实有查询领域对象的代码。

编辑 3: 已对其进行更改,因此所有提及领域的代码都不是简单地调用 Realm() 被注释掉,问题仍然存在。 IE; EDIT 1 仍然准确

您为什么不阅读错误消息?它清楚地表明您在您的 Realm 模型之一中使用了不受支持的类型。

如错误消息所述,您无法存储类型为 NSArray 的 属性(因为您使用的是 Swift,您可能一直在尝试存储 Array<Any>,相当于 NSArray)。如果你真的需要在你的模型 classes 中存储一组基元,作为一种解决方法,你可以创建一个自定义模型 class 只有一个 属性 并存储一个值列表那种类型的。

使用以下模型很容易重现错误 class(我刚刚向示例中的 Car class 添加了一个新的 属性):

class Car: Object {
    dynamic var brand = ""
    dynamic var name: String?
    dynamic var year = 0
    dynamic var array = [Int]()

    override var description: String { return "Car {\(brand), \(name), \(year)}" }
}

一旦您尝试实例化 Realm,即使您没有创建 Car.

的实例,也会在这一行出现上述错误

let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "TemporaryRealm"))