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:
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!
Import ".h" files only in ".m" file, like:
Ocean.h file:
#import <Foundation/Foundation.h>
@class Fish;
@interface Ocean : NSObject
@property (nonatomic) NSArray *listOfFishesInOcean;
@end
Ocean.m file:
#import "Ocean.h"
#import "Fish.h"
@implementation Ocean
@end
Fish.h file:
#import <Foundation/Foundation.h>
@class Ocean;
@interface Fish : NSObject
@property (nonatomic) Ocean *homeOcean;
@property (nonatomic) int age;
@end
Fish.m file:
#import "Fish.h"
#import "Ocean.h"
@implementation Fish
@end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments