我使用的领域为迅捷3.1,并已通过import RealmSwift
和import Realm
导入它。当我在我的快速代码中执行这一行时,我会得到这个错误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)
我不认为这与我的设备有任何关系,因为我的朋友/队友与我们的应用程序有同样的问题。我们(我相信)都在使用Xcode 8.3.3和Swive3.1。
当我们将领域放入测试应用程序并运行它时,我们的代码就可以工作了。如果它有任何相关性的话,我们也在使用Almofire,尽管把它放在我们的测试应用程序中并没有破坏任何东西。
编辑1:我们拥有的唯一使用领域的代码就是上面的一行代码,实际上我们没有通过王国存储任何东西
编辑2:上面的编辑是不正确的,我们有代码查询领域对象。
编辑3:修改了它,所以所有提到领域的代码,不仅仅是调用Realm()
,都会被注释掉,问题仍然存在。即编辑1仍然是准确的
发布于 2017-08-16 04:52:44
你为什么不读错误信息呢?它清楚地指出,您正在您的领域模型中使用不受支持的类型。
正如错误消息所述,您不能拥有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"))
https://stackoverflow.com/questions/45713639
复制相似问题