0

I am trying to relate words to sentence objects and have sentences be made up of word objects.

This is what I want to do:

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

I am getting this in my debug console:

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) 

What should I try to make this kind of relationship work in my code?

1 Answer 1

1

As stated by Realm's docs on relationships in Swift, simply declare your to-many properties as RLMArrays. Here's how your models should look:

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 = ""
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.