What are the advantages and disadvantages of using a protocol against inheritance?

In objective-c?

I had a class, and I decided that I needed another class, similar to the first class. Should I use the protocol and ensure that both classes support this protocol, or should I create a parent class and decide that 2 classes inherit from this one class?

Note:

Classes: Business, Catalog, AddressAnnotation (currently works only for business) and AddressAnnotationView (currently works only for business). I want the same to work in the Catalog. There is also BGGoogleMapCacheForAllAnnotations that control when annotations are grouped together (they should now be treated as AddressAnnotation for Directory and Business. There is also a BGGoogleMap View Controller that I want to turn into a parent class.

+5
source share
2 answers

, , . , , , , . , .

, , Vehicle , , , , . Car, , , Vehicle, ; , , , , , . , . , , , ( Class Extension, ). , , .

- . , -, , , , , , . , , , , , , . , , .

//SGSprite.h
@protocol SGSpriteDelegate
    - (BOOL) animation:(int)animationIndex willCompleteFrameNumber:(int)frame forSprite:(id)sender;
@end

@interface SGSprite : NSObject
@property (nonatomic, assign) id<SGSpriteDelegate> delegate;
@end

//SGViewController.h
@interface SGViewController : UIViewController <SGSpriteDelegate>
    //...dreadfully boring stuff
@end

SGSprite 2D-. , , SGSprite , . , , , , - , , - . , id, , , , / , .

; , , . . , , , , , . , , - , .

+7

, . -, , " ", ? , 90% ? , , -?

, "", . , , .

, , , - , , . Core Data, , Core Data . .

, (NSObject, ..) , , (NSCoding, ..). , .

+3
source