KomodoDave
KomodoDave

Reputation: 7319

Objective-C: Where is this delegate declared, and other queries

I'd like to find out where a delegate specified exclusively as a property in a particular WWDC Live Demo Video is declared (Note: You'll need an Apple Developer login to access the video).

The relevant code is listed below, omitting an iOS 5 property qualifier for NDA reasons. I believe this qualifier has no relevance for my query.

#import <UIKit/UIKit.h>
@class NSManagedObject;
@protocol CoffeeViewControllerDelegate;

@interface CoffeeViewController : UITableViewController
@property (_____,nonatomic) id <CoffeeViewControllerDelegate> delegate;
@end

@protocol CoffeeViewControllerDelegate <NSObject>
 // ...
@end

My questions are:

  1. Where is the delegate declared as a class member?
  2. Does the inheritance of the NSObject protocol by the CoffeeViewControllerDelegate protocol mean that runtime checking that the delegate has all NSObject methods will occur?
  3. Why is it necessary to forward declare NSManagedObject? Is this a common requirement when utilizing Core Data?

Many thanks for your time.

Upvotes: 1

Views: 567

Answers (4)

Caleb
Caleb

Reputation: 124997

Where is the delegate declared as a class member?

It's right there:

@property (_____,nonatomic) id <CoffeeViewControllerDelegate> delegate;

The ivar used to implement the property is synthesized (not shown in the code you provided), but that's standard practice these days.

Does the inheritance of the NSObject protocol by the CoffeeViewControllerDelegate protocol mean that runtime checking that the delegate has all NSObject methods will occur?

No, I don't think that needs to happen at run time. The compiler can check that.

Why is it necessary to forward declare NSManagedObject? Is this a common requirement when utilizing Core Data?

It's common to use a forward declaration anytime you want to refer to a class but don't need to use its interface. The alternative is including the entire header file where it's defined, or the umbrella header for the whole framework. That'd just slow things down here -- all the compiler cares about at this point is that NSManagedObject refers to some class.

Upvotes: 1

Giuseppe
Giuseppe

Reputation: 6644

1) When you specified a property without a class member, objective c automatically create a private member for you.

2) It just tell you that delegate respects this protocol. You have to do this check manually using [delegate conformsToProtocol: @protocol(CoffeeViewControllerDelegate)] before the assignation. Protocol checks are usually done by compiler at compile time

3) Because you can have a cyclic import problem. If you just need to use your entity as a type use a forward declaration, if you use some method of an instance of this class you have to use an import.

Upvotes: 1

FluffulousChimp
FluffulousChimp

Reputation: 9185

You do not now need to explicitly declare a _delegate ivar; your implementation simply needs @synthesize delegate.

You should still check at runtime that the delegate conforms to the expected protocol.

Based on the code fragment provided, I don't see a need for a forward declaration of NSManagedObject.

Upvotes: -1

Stephen Darlington
Stephen Darlington

Reputation: 52565

  1. I think this is an LLVM (Apple's new GCC replacement Objective-C compiler) feature, that means that the variable is create when you @synthesize the property (I'm not 100% sure about this)
  2. No. It would be checked at compile time
  3. You could also #import <CoreData/CoreData.h> but your code will compile more quickly just doing the forward declaration. Basically, it doesn't need to know anything about the implementation other than the size of it (it's an object so it's a pointer)

Upvotes: 3

Related Questions