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!
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
I think one of the best approaches could be using the identifiers so you improve the performance.
#interface Ocean : NSObject
#property (nonatomic) int identifier;
#property (nonatomic) NSArray *listOfFishesInOcean;
#end
#interface Fish : NSObject
#property (nonatomic) int age;
#property (nonatomic) int parent_identifier;
#end
if you want to keep your approach:
#import <Foundation/Foundation.h>
#class Ocean;
#interface Fish : NSObject
#property (nonatomic) Ocean *homeOcean; // Compiler: "Unknown type name Ocean"
#property (nonatomic) int age;
#end
Fish.m
#import "Ocean.h"
Related
I have a custom delegate that I have created for a subclass of UITextField. In the delegate class, I've declared an enum like this:
MyCustomDelegate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MyCustomTextField.h"
#interface MyCustomDelegate : NSObject <MyDelegate, UITextFieldDelegate>
#property (nonatomic, strong)MyCustomTextField *customTextField;
#end
MyCustomTextField.h:
#import <UIKit/UIKit.h>
typedef enum {
EnumTypeA,
EnumTypeB,
EnumTypeC,
EnumTypeD,
EnumTypeE
} MyEnumType;
#class MyCustomDelegate;
#protocol MyDelegate <NSObject>
#required
- (void)methodA;
- (void)methodB;
#end
#interface MyCustomTextField : UITextField
#property (nonatomic, weak)id <MyDelegate>myDelegate;
#property (nonatomic) MyEnumType enumType;
#end
Now, I am trying to use this enum in conjunction with my custom UITextField elsewhere in my project, like this:
MyViewController.h
#import "MyCustomTextField.h"
#import "MyCustomDelegate.h"
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController
#property (weak, nonatomic) IBOutlet MyCustomTextField *mySampleTextField;
#end
MyViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.mySampleTextField setMyEnumType:EnumTypeA];
}
However, I am getting the error, "No visible #interface for 'MyCustomTextField' declares the selector 'setMyEnumType'".
Can anyone see what it is I'm doing wrong?
Objective-C will automatically generate setter for you based on the name of the property (enumType) instead of the name of type (MyEnumType). So your setters should be the following instead:
[self.mySampleTextField setEnumType:EnumTypeA];
Imagine I have two header files: SomeFileA.h and SomeFileB.h
SomeFileA.h includes SomeFileB.h, and SomeFileB.h includes SomeFileA.h.
This creates a loop and confuse the compiler. How can we overcome this?
You should "forward declare" your classes. This tells the compiler that the class exists, but without the need to actually import it.
SomeFileA.h
#class SomeFileB // <-- This "forward declares SomeFileB"
#interface SomeFileA
#property (nonatomic, strong) SomeFileB *someFileB;
...
#end
SomeFileA.m
#import "SomeFileB.h"
#implementation SomeFileA
...
#end
And the same thing, but the other way around in SomeFileB
SomeFileB.h
#class SomeFileA // <-- This "forward declares SomeFileA"
#interface SomeFileB
#property (nonatomic, strong) SomeFileA *someFileA;
...
#end
SomeFileB.m
#import "SomeFileA.h"
#implementation SomeFileB
...
#end
If you don't use a class in the header, you don't need to forward declare it.
#interface SomeFileA
//I took out the property for SomeFileB.. no need for the #class anymore.
...
#end
I have an object named friend (I know, first letter should be uppercase). I use this class in many other classes and view controller.
All of a sudden, without changing ANY CODE, every custom object I have, stopped recognize each other. I always get the error Unknown type name "friend".
I already tried to clear the project and restart the mac.
WTF is wrong with this Xcode? All of a sudden my whole project stops working.
Here is my class currentUser.h
#import <Foundation/Foundation.h>
#import "friend.h"
#interface currentUser : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
#property (nonatomic,strong)NSString *token;
#property (nonatomic,strong)NSString *email;
#property (nonatomic,strong)NSString *name;
#property (nonatomic,strong)UIImage *userImg;
#property (nonatomic,strong)NSString *username;
#property (nonatomic,strong)NSString *facebookID;
#property (nonatomic,strong)NSString *userPSW;
#property (nonatomic,strong)NSString *userID;
#property (nonatomic,strong)NSMutableArray *friendsList;
#property (nonatomic,strong)NSMutableArray *groups;
#property (nonatomic,strong)NSData *audioMessage;
#property (nonatomic,strong)NSMutableArray *mimosToDownload;
#property (nonatomic,strong)NSMutableArray *mimosDownloaded;
#property (nonatomic,strong)friend *friendToSend; //Here is where a I get the error of unknown type name
#end
this is the friend.h
#import <Foundation/Foundation.h>
#import "currentUser.h"
#import "Imager.h"
#interface friend : NSObject<NSCoding>
#property (nonatomic,strong)NSString *username;
#property (nonatomic,strong)NSString *userID;
#property (nonatomic,strong)UIImage *userImg;
#property(nonatomic,strong)NSMutableData *buffer;
#property(nonatomic)BOOL flagDownloaded;
-(UIImage*)downloadImageBlocked;
-(id)init;
-(UIImage*)getFriendImg;
-(UIImage*)userImg;
-(NSString*)getUserID;
-(NSString*)getUserName;
- (void)encodeWithCoder:(NSCoder *)encoder;
-(bool)isNil;
#end
fix the stuff that you know is wrong first...
change
#interface currentUser : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
to
#interface User : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
you don't need to know if the user is current or not for the user to have properties of a user, think of them as actually representing objects.
change:
#interface friend : NSObject<NSCoding>
to
#interface Friend : NSObject<NSCoding>
you know it is wrong, fix it now before you have to fix it in 1000 places instead of 100 places.
Convention is very important in Objective-C.
also don't import everything into your header if you don't need to...
#class Friend; //forward class declaration
#interface currentUser : NSObject<NSCoding>
...
#property (nonatomic,strong)Friend *friendToSend; //Here is where a I get the error of unknown type name
and as for your ivar's in currentUser you don't need those in the interface (they can go in a block after the #implimentation line), unless you need them for backwards compatibility...
This question already has answers here:
Errors when making circular reference imports
(2 answers)
Closed 8 years ago.
I have two custom NSObjects which reference each other, as below:
.h file class 1
#import <Foundation/Foundation.h>
#import "MyChildClass.h"
#interface MyParentClass : NSObject
#property (nonatomic, strong) NSString *Name;
#property (nonatomic) MyChildClass *myChild;
#end
.m file class 1:
#import "MyParentClass.h"
#implementation MyParentClass
#end
and...
.h file class 2:
#import <Foundation/Foundation.h>
#import "MyParentClass.h"
#interface MyChildClass : NSObject
#property (nonatomic) MyParentClass *myParent;
#end
.m file class 2:
#import "MyChildClass.h"
#implementation MyChildClass
#end
When I try and implement those as below, the project will not build, with the error "Arc Restrictions: Implicit conversion of an Objective-C pointer to 'int *' is disallowed with ARC"
View Controller implementation code:
MyParentClass *newParent = [[MyParentClass alloc] init];
MyChildClass *newChild = [[MyChildClass alloc] init];
newParent.Name = #"Johnny";
newParent.myChild = newChild;
newChild.myParent = newParent;
However, when I nest a second example into single .h/.m files, the project will build:
Nested .h file:
#import <Foundation/Foundation.h>
#class MyNestedClass;
#interface MyChildNestedClass : NSObject
#property (nonatomic, strong) MyNestedClass *myNested;
#property (nonatomic, strong) NSString *ChildName;
#end
#interface MyNestedClass : NSObject
#property (nonatomic, strong) MyChildNestedClass *myChild;
#property (nonatomic, strong) NSString *ParentName;
#end
Nested .m file:
#import "MyNestedClass.h"
#implementation MyChildNestedClass
#end
#implementation MyNestedClass
#end
I guess that by nesting the objects into a single file, I am somehow tricking ARC and that really I shouldn't be creating these circular references.
Could anyone suggest the correct way of nesting types like this?
What I am trying to achieve, is the following pseudo-scenario: Parent has a property of Child. Child has a property of the parent it is the child of.
Am I finding this difficult because I'm not supposed to do it (circular referencing), or am I missing something glaringly obvious?
Many thanks!
Do not #import "MyChildClass.h" in the parent class' header file, change it to
#class MyChildClass
and add the #import in the source file (.m)
I am trying to declare a property
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
#property (nonatomic, strong) MPMoviePlayerController*player;
}
#end
I receive a red warning sign when i click on it, it reads;
a parameter list without types is only allowed in function definition
properties are outside of braces
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
// here is for ivars only
id _something;
}
// properties and methods
#property (nonatomic, strong) MPMoviePlayerController*player;
#end