Class issue

Discussion in 'Mac Programming' started by mystudioos, Jul 22, 2013.

  1. macrumors newbie

    Joined:
    Jul 22, 2013
    #1
    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
    Code:
    #import "Employee.h"
    #import "Film.h"
    
    @interface Actor : Employee {
        int oscar;
        Film *movie;
    }
    
    -(void)setOscar:(int)valueOscar;
    -(int)getOscar;
    -(void)print;
    
    @end

    Director.h
    Code:
    #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
    Code:
    @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
     
  2. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    Next time you get the error, post it in this thread. Then we can help you.
     
  3. thread starter macrumors newbie

    Joined:
    Jul 22, 2013
    #3
    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"
     
  4. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #4
    I'd recommend a couple of things:

    - In Actor.h and Director.h, instead of importing "Film.h", I'd use the line
    Code:
    @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.
     
  5. thread starter macrumors newbie

    Joined:
    Jul 22, 2013
    #5
    i just tried ty, but still get the same error.

    Actor.h
    Code:
    #import "Employee.h"
    @class Movie;
    
    @interface Actor : Employee {
        
        int oscar;
    [B][COLOR="Red"]    Movie *mymovie;[/COLOR][/B]//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
    Code:
    #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
    Code:
    #import <Foundation/Foundation.h>
    @class Actor;
    
    @interface Movie : NSObject {
        NSMutableArray *arr_actor;
        NSString *movieName;
        long double boxOffice;
        int year;
        [B][COLOR="Red"]Actor *manuelo;[/COLOR][/B]//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
    Code:
    #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
     
  6. thread starter macrumors newbie

    Joined:
    Jul 22, 2013
    #6
    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?
     
  7. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    #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.
     
  8. thread starter macrumors newbie

    Joined:
    Jul 22, 2013
    #8
    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
     
  9. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #9
    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.
     

Share This Page