如何使RLMObjects数组成为Realm类的属性?

时间:2014-12-04 01:02:13

标签: swift realm

我试图将单词与句子对象联系起来,并将句子组成单词对象。

这就是我想要做的事情:

class Word: RLMObject {
    dynamic var sentences = [Sentence]
    dynamic var spanish = ""
    dynamic var english = ""
}

class Sentence: RLMObject {
    dynamic var content = [Word]
    dynamic var machineTranslation = ""
}

word1 = Word.objectsWhere("spanish == 'hola'").firstObject()
word2 = Word.objectsWhere("spanish == 'amigo'").firstObject()

let sentence1 = [word1,word2]

var Realm = RLMRealm.defaultRealm()
Realm.beginWriteTransaction()
Realm.addObject(sentence1)
Realm.commitWriteTransaction()

我在调试控制台中收到了这个:

2014-12-03 12:42:36.139 MyApp [5743:586150] *** Terminating app due to uncaught exception 'RLMException', reason: ''(null)' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See http://realm.io/docs/cocoa/api/Classes/RLMObject.html for more information.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105c72f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001077f2bb7 objc_exception_throw + 45
    2   MyApp                               0x000000010578bd0f -[RLMProperty setTypeFromRawType] + 1155
    3   MyApp                               0x000000010578c108 -[RLMProperty initSwiftPropertyWithName:attributes:property:instance:] + 391
    4   MyApp                               0x0000000105785a6d +[RLMObjectSchema propertiesForClass:] + 412
    5   MyApp                               0x0000000105785341 +[RLMObjectSchema schemaForObjectClass:createAccessors:] + 318
    6   MyApp                               0x00000001057abdfc __23+[RLMSchema initialize]_block_invoke + 769
    7   libdispatch.dylib                   0x0000000107f977f4 _dispatch_client_callout + 8
    8   libdispatch.dylib                   0x0000000107f84343 dispatch_once_f + 565
    9   libobjc.A.dylib                     0x00000001077f34d6 _class_initialize + 648
    10  libobjc.A.dylib                     0x00000001077fc6e1 lookUpImpOrForward + 351
    11  libobjc.A.dylib                     0x00000001078090d3 objc_msgSend + 211
    12  MyApp                               0x0000000105782826 -[RLMObject init] + 38
    13  MyApp                               0x000000010577011b _TFC11MyApp      4WordcfMS0_FT_GSQS0__ + 315
    14  MyApp                               0x0000000105770292 _TFC11MyApp      4WordCfMS0_FT_GSQS0__ + 50
    15  MyApp                               0x000000010576a894 _TFC11MyApp      11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 340
    16  MyApp                               0x000000010576b660 _TToFC11MyApp        11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 560
    17  UIKit                               0x00000001064f9475 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 234
    18  UIKit                               0x00000001064f9fbc -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2463
    19  UIKit                               0x00000001064fcd2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    20  UIKit                               0x00000001064fbbf2 -[UIApplication workspaceDidEndTransaction:] + 179
    21  FrontBoardServices                  0x00000001092822a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    22  CoreFoundation                      0x0000000105ba853c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    23  CoreFoundation                      0x0000000105b9e285 __CFRunLoopDoBlocks + 341
    24  CoreFoundation                      0x0000000105b9da43 __CFRunLoopRun + 851
    25  CoreFoundation                      0x0000000105b9d486 CFRunLoopRunSpecific + 470
    26  UIKit                               0x00000001064fb669 -[UIApplication _run] + 413
    27  UIKit                               0x00000001064fe420 UIApplicationMain + 1282
    28  MyApp                               0x000000010576bb9e top_level_code + 78
    29  MyApp                               0x000000010576bbda main + 42
    30  libdyld.dylib                       0x0000000107fcc145 start + 1
    31  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我应该尝试在我的代码中使这种关系发挥作用?

1 个答案:

答案 0 :(得分:1)

正如Swift中的Realm's docs on relationships所述,只需将您的多对多属性声明为RLMArray。以下是模型的外观:

class Word: RLMObject {
    dynamic var sentences = RLMArray(objectClassName: Sentence.className())
    dynamic var spanish = ""
    dynamic var english = ""
}

class Sentence: RLMObject {
    dynamic var content = RLMArray(objectClassName: Word.className())
    dynamic var machineTranslation = ""
}