Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kingjames

macrumors newbie
Original poster
Aug 4, 2011
3
0
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 ;)
 
Shooting in the dark here...

Try using
Code:
#import "Movie.h"

instead of
Code:
@class Movie

Let me know whether it works :)
 
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.
 
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.

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.)
 
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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.