PDA

View Full Version : Class issue




mystudioos
Jul 22, 2013, 01:17 PM
hi, xcode is giving me some issues with a class. the code seem apparently good, my only doubt is on importing.
I have a classA which import a classB n classB import classA.

here's the code

Actor.h

#import "Employee.h"
#import "Film.h"

@interface Actor : Employee {
int oscar;
Film *movie;
}

-(void)setOscar:(int)valueOscar;
-(int)getOscar;
-(void)print;

@end


Director.h

#import "Employee.h"
#import "Film.h"

@interface Director : Employee {
Film *movie; //i get error here "unknow file name Film"
int oscar;
BOOL isPerc;
}

-(void)setOscar:(int)valueOscar;
-(int)getOScar;

@end


Film.h

@interface Film : NSObject{

NSString *nameMovie;
long double boxOffice;
int year;
Actor *actorName;
Director *dirName; //i get error here "unknow file name Director"

}

@end

sometimes i just rewrite the code, as it is, and the warning disappear. then randomly it appear again



robvas
Jul 22, 2013, 01:56 PM
Next time you get the error, post it in this thread. Then we can help you.

mystudioos
Jul 22, 2013, 02:00 PM
Next time you get the error, post it in this thread. Then we can help you.

the point is i can't compile it... i get the warning that won't let me run.

the error is the one i commented in the code
"unknow type name Director"
"unknow type name Film"

whooleytoo
Jul 22, 2013, 02:44 PM
I'd recommend a couple of things:

- In Actor.h and Director.h, instead of importing "Film.h", I'd use the line @class Film;

That could save you headaches with circular references later on.

- In Film.h, do the same for Actor and Director (i.e. use @class Actor; and @class Director).

You will need to import the header files in the .m file, just not in the .h file.

mystudioos
Jul 22, 2013, 03:02 PM
I'd recommend a couple of things:

- In Actor.h and Director.h, instead of importing "Film.h", I'd use the line @class Film;

That could save you headaches with circular references later on.

- In Film.h, do the same for Actor and Director (i.e. use @class Actor; and @class Director).

You will need to import the header files in the .m file, just not in the .h file.

i just tried ty, but still get the same error.

Actor.h

#import "Employee.h"
@class Movie;

@interface Actor : Employee {

int oscar;
Movie *mymovie;//unknow type name Movie

}

-(void)setOscar:(int)valueOscar;
-(int)getOscar;

-(void)addMovie:(id)nameFilm;

-(id)initWhitActorName:(NSString *)valueName andSurname:(NSString *)valueSurname andAddress:(NSString *)valueAddress andJob:(NSString *)valueJob andPhone:(long double)valuePhone andSalary:(long double)valueSalary andOscar:(int)valueOscar;

-(void)print;

@end



Actor.m

#import "Actor.h"
@class Movie;

@implementation Actor

-(void)setOscar:(int)valueOscar{ oscar = valueOscar; }
-(int)getOscar{ return oscar; }

-(void)addMovie:(id)nameFilm{


}

-(id)initWhitActorName:(NSString *)valueName andSurname:(NSString *)valueSurname andAddress:(NSString *)valueAddress andJob:(NSString *)valueJob andPhone:(long double)valuePhone andSalary:(long double)valueSalary andOscar:(int)valueOscar{

name = valueName;
surname = valueSurname;
address = valueAddress;
job = valueJob;
phone = valuePhone;
salary = valueSalary;
oscar = valueOscar;

return self;
}

-(void)print{
NSLog(@"Actor Name:%@ - Surname:%@ - Oscar:%i", [self getName], [self getSurname], [self getOscar]);
}

@end



Movie.h

#import <Foundation/Foundation.h>
@class Actor;

@interface Movie : NSObject {
NSMutableArray *arr_actor;
NSString *movieName;
long double boxOffice;
int year;
Actor *manuelo;//Unknow type name Actor


}


-(void)setNameMovie:(NSString *)valNameMovie;
-(void)setBoxOffice:(long double)valueBoxOffice;
-(void)setYear:(int)valueYear;

-(NSString *)getNameMovie;
-(long double)getBoxOffice;
-(int)getYear;

-(id)initWhitName:(NSString *)valueName andYear:(int)valueYear andBoxOffice:(long double)valueBoxOffice;

-(void)print;

@end


Movie.m

#import "Movie.h"
@class Actor;

@implementation Movie

-(void)setNameMovie:(NSString *)valNameMovie { movieName = valNameMovie; }
-(void)setBoxOffice:(long double)valueBoxOffice { boxOffice = valueBoxOffice; }
-(void)setYear:(int)valueYear { year = valueYear; }

-(NSString *)getNameMovie { return movieName; }
-(long double)getBoxOffice { return boxOffice; }
-(int)getYear { return year; }

-(id)initWhitName:(NSString *)valueName andYear:(int)valueYear andBoxOffice:(long double)valueBoxOffice{

self = [super init];

if(self){
arr_actor = [[NSMutableArray alloc]initWithCapacity:11];
movieName = valueName;
year = valueYear;
boxOffice = valueBoxOffice;
}
return self;

}

-(void)addActor:(Actor *)valueActor{ [arr_actor addObject:valueActor]; }

-(void)print{
NSLog(@" Name:%@ - BoxOffice:% .0LF - Year:%i Actor:%@",movieName, boxOffice, year,arr_actor);
}


@end

mystudioos
Jul 22, 2013, 03:58 PM
i'm trying few things, and if i remove the "ciclic" reference it build the project...

pretty much seems that the issue is that the class Actor import the class Movie,
and the class Movie import the class Actor.
so i need to avoid that

but anyone has an idea on how to fix the problem logically??

I need an object Actor which has the name of the actor, the surname, and a list of the movie he's acted in.

But i also need an object Movie which has the name of the movie, and a list of actors that acted in it.

am i just linking the class the wrong way?

ArtOfWarfare
Jul 22, 2013, 05:43 PM
#import is like a command to compile another file before the one that it's found in.

If A imports B and B imports A, you're telling Xcode to compile A before B but B before A - thus it fails.

Use @class instead. It tells Xcode that eventually while compiling it'll find parts of the compiling puzzle that seem to be missing right now if it just keeps going.

Use @class in one direction and #import in the other, if both need to know about each other. Often, only one really should know about the other.

What you're doing looks like it may be complicated enough to warrant using Core Data and just having it handle the coding for you.

mystudioos
Jul 23, 2013, 03:57 AM
#import is like a command to compile another file before the one that it's found in.

If A imports B and B imports A, you're telling Xcode to compile A before B but B before A - thus it fails.

Use @class instead. It tells Xcode that eventually while compiling it'll find parts of the compiling puzzle that seem to be missing right now if it just keeps going.

Use @class in one direction and #import in the other, if both need to know about each other. Often, only one really should know about the other.

What you're doing looks like it may be complicated enough to warrant using Core Data and just having it handle the coding for you.
yeah that's what i tought, ty. i tried with class but it's not working either...
what do u mean to use core data? i haven't really used it yet...
do u mean i should just link the two object with a id in a databse? that's honestly the thing that would better fit i think, but i wanted to find a solution that wouldnt use databases or plist

whooleytoo
Jul 23, 2013, 08:55 AM
In your implementation (Actor.m, Movie.m) files, you should use import rather than the @class directive. It's only in the interface (.h) files that you want to avoid the import statements, to avoid circular references. I'm not sure if that's the issue, but you should change that.

I'd second Core Data, you should look up some online tutorials.

If you're learning Obj-C, then it might be no harm to do it as you're doing it now, it's a good way to learn the Obj-C & Cocoa concepts. On the other hand, if you're looking to build a big project, Core Data could save you a lot of time & headaches.