0

I may not have worded the question right, but I am not sure if what I am asking makes 100% so here goes:-)

In Xcode you can set a @class (name of class) above the @interface in the header file. Is this the same as changing the the UIViewController in the name of the class? See code below:

So is this the same -

@class CoreDataClass;

@interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{

}

//This file declares the UITableView
@property (nonatomic, retain) IBOutlet UITableView *mainTableView;

@property (nonatomic, retain) CoreDataClass *cdc;

As this:

@interface FlipsideViewController : CoreDataClass <UITableViewDataSource, UITableViewDelegate>
{

}

//This file declares the UITableView
@property (nonatomic, retain) IBOutlet UITableView *mainTableView;

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end

??

If this is not the same, how is it different and what are advantages to the different implementation?

The Difference is only really asked if they are similar:-)

jwknz
  • 6,598
  • 16
  • 72
  • 115

4 Answers4

3

@class is not used to create a class, but to forward declare another one. See this question for a good explanation.

Community
  • 1
  • 1
tgt
  • 1,308
  • 1
  • 10
  • 16
1

They're not even related. The difference is that the superclass ("parent" class) of your view controller will be different (and this can lead to nice unrecognized selector errors...). Forward-declaring a class using the @class keyword is just a convenient way of referring to a class when one doesn't want to import a whole framework header hierarch just in order to refer to one class. I. e., if you don't need to know anyting about a class except that it exists, you can use this keyword. Be careful, however, if you maks heavy use of the class - in those cases, the class forward-declaration is not considered a good solution.

1

They are not the same at all. The first case is a 'forward declaration' - you are telling the compiler that the class CoreDataClass exists, so that you can refer to it in your header file without actually importing the files that define it.

The second case, you are declaring that FlipsideViewController is a subclass of CoreDataClass, and inherits all its methods and instance variables.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
0

In first case when you use @class it's inform XCode that you will be using CoreDataClass somewhere and you will #import header for example in .m file, in second case you're inherit from CoreDataClass (you will get access to all public and protected properties)

Michal Zaborowski
  • 5,039
  • 36
  • 35