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

标签 swift xcode swift3 realm xcode8

我正在使用 Realm for swift 3.1 并通过 import RealmSwift 导入它 导入 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 /队友对我们的应用程序有同样的问题。我们都(我相信)使用 Xcode 8.3.3 和 swift 3.1。

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

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

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

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

最佳答案

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

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

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

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"))

关于swift - 调用 Realm() 时出现 Realm RLM 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713639/

相关文章:

ios - 在上下文菜单 SwiftUI 中显示不同的 View

ios - 无法在新开发机器上使用 nil 模型创建 NSPersistentStoreCoordinator

xcode - 如何在 Swift 3.0 中使用@asmname()

ios - 从图层重新加载 tableView 时崩溃

ios - Swift 检索 Firebase 数据

ios - 仅下载前 n 个子节点(Firebase、Swift)

ios - 检测 tableViewCell 不同区域中的点击

swift - 从 github 克隆后,Xcode 项目工作区丢失文件

ios - 带有字符串枚举的开关未被识别

ios - 放入 UIImage 失败(Swift)