Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C, protocols and subclasses

Let's say that I have the following protocols defined:

// basic protocol for an User Interface object:

@protocol UIObjectProtocol <NSObject>
@property (assign) BOOL touchable;
@end

// basic protocol for an User Interface object that is held by a holder object:

@protocol UIHeldObjectProtocol <UIObjectProtocol>
@property (readonly) id holder;
@end

And the following class hierarchy:

// base class for an User Interface object, which synthesizes the touchable property

@interface UIObject : NSObject <UIObjectProtocol> {
   BOOL _touchable;
}
@end

@implementation UIObject
@synthesize touchable=_touchable;
@end

At this point, everything is OK. Then I create a UIObject subclass named UIPlayingCard. Inherently, UIPlayingCard conforms to the UIObjectProtocol since it's superclass does it too.

Now suppose I want UIPlayingCard to conform to UIHeldObjectProtocol, so I do the following:

@interface UIPlayingCard : UIObject <UIHeldObjectProtocol> {
}
@end

@implementation UIPlayingCard
-(id)holder { return Nil; }
@end

Note that the UIPlayingCard conforms to UIHeldObjectProtocol, which transitively conforms to UIObjectProtocol. However I'm getting compiler warnings in the UIPlayingCard such that:

warning: property 'touchable' requires method '-touchable' to be defined - use @synthesize, @dynamic or provide a method implementation

which means that the UIPlayingCard superclass conformance to the UIObjectProtocol wasn't inherited (maybe because the @synthesize directive was declared in the UIObject implementation scope).

Am I obligated to re-declare the @synthesize directive in the UIPlayingCard implementation ?

@implementation UIPlayingCard
@synthesize touchable=_touchable; // _touchable now must be a protected attribute
-(id)holder { return Nil; }
@end

Or there's another way to get rid of the compiler warning? Would it be the result of bad design?

Thanks in advance,

like image 469
Eduardo Coelho Avatar asked Mar 02 '11 14:03

Eduardo Coelho


People also ask

What is Objective-C protocol?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.

What is the difference between Swift protocol and Objective-C protocol?

In Swift, calling a method will be decided at compile time and is similar to object-oriented programming, whereas in Objective C, calling a method will be decided at runtime, and also Objective C has special features like adding or replacing methods like on a class which already exists.

When should I use subclass?

Subclassing : If we want to modify state as well as behaviour of any class or override any methods to alter the behaviour of the parent class then we go for subclassing.

What is protocol and delegate in Objective-C?

An Objective-C delegate is an object that has been assigned to the delegate property another object. To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol.


1 Answers

You comment that _touchable would then have to be protected, but you did not include an @private compiler directive, so _touchable is already protected.

Clang does not seem to give this warning in this case, so the most straightforward solution:

  • Change your compiler from GCC to Clang.

This will have plenty of side-benefits, as well, including much more intelligible error and warning messages.

It is a bug that GCC complains about this. The implementation is in fact inherited, as you can verify with a simple main function exercising UIPlayingCard's touchable property. So, your second solution:

  • Ignore the warning as bogus. If none of your tests are failing, what do you care, anyway?
like image 133
Jeremy W. Sherman Avatar answered Sep 20 '22 14:09

Jeremy W. Sherman