Objective C - Avoid Import Loop

user3191334

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:

  1. How to avoid the import-loop, which causes the complier error, I think?
  2. 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!

KSR

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Objective C for loop delay

From Dev

Objective C @import for modules does it replace #import?

From Dev

When to avoid using accessor methods in Objective C

From Dev

When to avoid using accessor methods in Objective C

From Dev

Loop with delay or completion in objective c

From Dev

How to implement for loop in objective c

From Dev

objective c - for loop with two if statements

From Dev

Objective-c Loop in NSThread

From Dev

Value of loop variable after "for in" loop in Objective C

From Dev

Import Objective-C files breaks CocoaPod

From Dev

Import objective-c initializer as optional

From Dev

Using @import in objective C in conjunction with __cplusplus

From Dev

Import UIKit for debugging Objective-C by default

From Dev

Import Objective-C Framework (CocoaPod) into Swift?

From Dev

Import files in objective-c using wildcard

From Dev

Is it possible to import Java models on Objective c?

From Dev

Objective-c - Replace #import with #define

From Dev

Import swift class into objective c storyboard

From Dev

Import ViewController.swift in Objective C file

From Dev

How to avoid using "and" in Objective-C method name

From Dev

Objective-C: Is there any trick to avoid casting of NSArray objects?

From Dev

How for in loop works internally - Objective C - Foundation

From Dev

For-loop optimization in Objective-C

From Dev

Objective-C property infinite loop

From Dev

Objective-C start loop from the beginning

From Dev

Loop between two NSDates in Objective C

From Dev

For-loop optimization in Objective-C

From Dev

Objective-C start loop from the beginning

From Dev

How to create an event loop in Objective C?

Related Related

HotTag

Archive