In objective-C i made 11 classes which subclass RLMObject to represent the model of my database. And as of now I have a problem building my App with this, because as stated in the title, they seem to not see each other. Additionally: they are in the same folder, the #import does not make any problems itself.
As a sample I want to provide two classes The following is the class for Books:
#import <Realm/Realm.h>
#import "Chapter.h"
@interface Book : RLMObject
@property NSInteger id;
@property NSString *name;
@property RLMArray<Chapter> *chapters;
@end
// This protocol enables typed collections. i.e.:
// RLMArray<Book>
RLM_ARRAY_TYPE(Book)
The following would be my class for chapters:
#import <Realm/Realm.h>
@class Book;
@interface Chapter : RLMObject
@property NSInteger id;
@property NSString *name;
@property Book *book;
@end
// This protocol enables typed collections. i.e.:
// RLMArray<Chapter>
RLM_ARRAY_TYPE(Chapter)
In Book.h I get:
Cannot find protocol declaration for 'Chapter'
Does anybody have an idea? It is definitely some kind of import circle. But how can I resolve it? If not necessary I would like to avoid putting all of the model-classes in a prefix header.
EDIT: a @class import helped in the chapter-file, but it doesn't in the Book-file
You can also make a protocol forward declaration for the objects, you want to use in your list property, like below:
#import <Realm/Realm.h>
@protocol Chapter;
@interface Book : RLMObject
@property NSInteger id;
@property NSString *name;
@property RLMArray<Chapter> *chapters;
@end
// This protocol enables typed collections. i.e.:
// RLMArray<Book>
RLM_ARRAY_TYPE(Book)
Note: You have to use @class for one-to-one relations, and @protocol for one-to-many relations.
Beside that I would recommend for your scenario to use a backlink from Chapter to Book instead of a property. What you have in your question are two independent properties, which are not automatically synchronized.
If you don't maintain it correctly, this could led to something like that:
Books:
- id: 0, name: "Moby Dick", chapters: [Chapter#0, Chapter#1]
- id: 1, name: "The Swift Programming Language", chapters: [Chapter#2, Chapter#3]
Chapters:
- id: 0, name: "Loomings.", book: Book#0
- id: 1, name: "The Carpet-Bag.", book: Book#0
- id: 2, name: "About Swift", book: Book#1
- id: 3, name: "A Swift Tour", book: Book#0
See how "A Swift Tour" is referencing back to "Moby Dick", while it's clearly the second chapter of the Swift book. Your model allows that, currently, but you can define it in a way to prevent that such scenarios can exist at all.
The solution with backlinks would look like that:
@interface Chapter : RLMObject
@property NSInteger id;
@property NSString *name;
@property (readonly) Book *book;
@end
@implementation Chapter
// Define "book" as the inverse relationship to Book.chapters
- (Book *)book {
return [self linkingObjectsOfClass:@"Book" forProperty:@"chapters"].firstObject;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With