Class properties not available?

Discussion in 'iOS Programming' started by Kingjames, Sep 3, 2011.

  1. Kingjames macrumors newbie

    Joined:
    Aug 4, 2011
    #1
    Hi,
    I just started programming with Xcode 4 working with a book called "Entwickeln mit dem iPhone SDK" (on german, published in english, don't know the original name).

    But I have a little problem with a code provided bye the book, that was written for Xcode 3. Hopefully you can help me with that :)

    Here's the situation: I have a view based app an a class called "Movie" (created the Movie.h and Movie.m files). In the default MovieViewController I can use this class perfectly. Now I added a MovieEditor UIViewController subclass.

    Despite the fact, that I added @class Movie in the .h file and created a variable like I did with the MovieViewController (with all the @property and @synthesize stuff) I can not access for example self.movie.title what is easily possible in the files created with creating the project. I have no declaration like "Movie *movie = [[Movie alloc] init] in the following code, but that is not working too

    Movie.h
    Code:
    @interface Movie : NSObject
    {
        NSString *title;
        NSNumber *boxOfficeGross;
        NSString *summary;
    }
    
    -(id)initWithTitle:(NSString *)NewTitle boxOfficeGross:(NSNumber *)newBoxOfficeGross summary:(NSString *)newsummary;
    
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSNumber *boxOfficeGross;
    @property (nonatomic, copy) NSString *summary;
    
    @end
    Movie.m
    Code:
    #import "Movie.h"
    
    @implementation Movie
    
    @synthesize title, summary, boxOfficeGross;
    
    -(id)initWithTitle:(NSString *)NewTitle boxOfficeGross:(NSNumber *)newBoxOfficeGross summary:(NSString *)newsummary
    {
        self = [super init];
        if (self != Nil) {
            self.title = NewTitle;
            self.boxOfficeGross = newBoxOfficeGross;
            self.summary = newsummary;
        }
        return self;
    }
    
    -(void)dealloc
    {
        self.title = Nil;
        self.boxOfficeGross = nil;
        self.summary = nil;
        [super dealloc];
    }
    
    - (id)init
    {
        self = [super init];
        if (self) {
            // Initialization code here.
        }
        
        return self;
    }
    
    @end

    MovieEditorViewController.h (file I created)
    Code:
    #import <UIKit/UIKit.h>
    
    @class Movie;
    
    @interface MovieEditorViewController : UIViewController <UITextFieldDelegate>
    {
        UITextField *titleField;
        UITextField *boxOfficeGrossField;
        UITextField *sumamryField;
        Movie *movie;
    }
    
    @property (nonatomic, retain) IBOutlet UITextField *titleField;
    @property (nonatomic, retain) IBOutlet UITextField *boxOfficeGrossField;
    @property (nonatomic, retain) IBOutlet UITextField *summaryField;
    @property (nonatomic, retain) Movie *movie;
    
    @end
    Important part of the MovieEditorViewController.m file
    Code:
    #import "MovieEditorViewController.h"
    
    @implementation MovieEditorViewController
    
    @synthesize titleField, boxOfficeGrossField, summaryField, movie;
    
    -(void)viewWillAppear:(BOOL)animated  //here start the problems
    {
        self.titleField.text = self.movie.title;  //property title cannot be found in forward class movie
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        self.boxOfficeGrossField.text = [formatter stringFromNumber:self.movie.boxOfficeGross];
        [formatter release];
        self.summaryField.text = self.movie.summary;
    }
    
    Best regards :)
    KingJames

    PS: writing this from Germany, reply could take it's time ;)
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Shooting in the dark here...

    Try using
    Code:
    #import "Movie.h"
    instead of
    Code:
    @class Movie
    Let me know whether it works :)
     
  3. Kingjames thread starter macrumors newbie

    Joined:
    Aug 4, 2011
    #3
    Oh wow that works :D I didn't read the book very well, it said explicit "import Movie.h".

    But in the progress of learning, could you explain to me the difference between @class and import "Movie.h". For me as a former C# programer it looks the same.

    But anyway, I thank you very much.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    IDK, they looked the same to me, too. I just tend to use #import except when for, whatever reason, I can't, in which case I resort to @class. (IE, because I have circular import statements.)
     
  5. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #5
    Use @class; when you just want to declare a variable of that class. e.g. Classname *myVar;

    Use import when you will be actually accessing methods and properties inside the class.
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    The technique I use in this situation is to use @class in MovieEditorViewController.h and add the #import to MovieEditorViewController.m.

    Why? Because this technique (theoretically) results in faster compiler times for other .m files that #import MovieEditorViewController.h. These other .m files don't need the full definition of Movie and so don't need to spend time compiling Movie.h.

    But maybe it's irrelevant today with lots of RAM and fast CPUs. But the technique also copes with circular class references, so I find it to be good general technique anyway.
     
  7. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #7
    The way I learned to use @class is to use it when there is a circular reference. For example Class A imports Class B and Class B imports Class A. If you use #import here you would get a loop going back and forth between Class A and Class B. @class somehow stops that.
     
  8. Kingjames thread starter macrumors newbie

    Joined:
    Aug 4, 2011

Share This Page