I want to connect two classes of mine and link each other with a property of each class. I broke down the main aspects to this example:
Ocean.h file:
#import <Foundation/Foundation.h>
#import "Fish.h"
@interface Ocean : NSObject
@property (nonatomic) NSArray *listOfFishesInOcean;
@end
Fish.h file:
#import <Foundation/Foundation.h>
#import "Ocean.h"
@interface Fish : NSObject
@property (nonatomic) Ocean *homeOcean; // Compiler: "Unknown type name Ocean"
@property (nonatomic) int age;
@end
What I wanna do in the end, is to manipulate the age property of an Fish object and be able to save it (listOfFishesInOcean, NSUserDefaults), as well as call a function in the Ocean object, when saving is completed. This way I'd always have the latest list of Fish-objects in my list of the Ocean-object.
My two problems here are:
- How to avoid the import-loop, which causes the complier error, I think?
- How should I implement the save & action workflow in the end?
I thought about solving this issue with notifications and observer but this way I'd still need to filter the notifications in any way, due to the fact that I've multiple Oceans with more Fishes. Another idea to solve it, would be to give each Ocean object and Fish object an ID, which I would use as a key in NSUserDefaults again.
If anyone has some thoughts about it or other ideas, you are very welcome!