objective c - Is this combination of inheritance and protocols correct? -
In this scenario, I want a class whose purpose is to direct some subclases, keeping in mind the need for implementation of the original class Base behavior I thought:
Protocol
@protocol TheProtocol @required - (NSString *) Some properties; - (zero) Some method: (NSString *) parameter;
Base view controller
@interface BaseViewController: UIViewController @end @implementation BaseViewController - (zero) viewDidLoad { Super Viewedload]; NSSart ([Self Conflict Protocol: @ Protocol (The Protocol)], @ "This view controller has to be compatible with TheProtocol."); } - (Zero) ViewVillagePower: (BOOL) Animated {[Super Viewer: Animated]; BaseViewController & LT; TheProtocol & gt; * CastedController = (BaseViewController & lt; theProtocol & gt; *) itself; NSString * Some Properties = [Some Developers Property]; NSLog (@ "% @", some properties); [Caste Communicator Some Method: @ "Hello Subclasses!"]; }
TheProtocol & gt; @end @ Implementation Subclass 1View Controller # Prima Mark - The Protocol - (nssting *) Some Properties {Return @ "Subclass 1"; } - (zero) Some method: (NSString *) parameter {NSLog (@ "% @", parameter); } @end Is this pattern correct? I have many concerns ... the other option is to create a safe category rather than a protocol.
Your pattern is correct.
Here's a similar solution to your idea, but personally I will do this in my project for the same purpose - so I do not have to put an example of this in. BaseClass < / Code>, because in my opinion the base class corresponds to essentially
DemandedProtocol
, but throws a runtime exception in every required method does not override in the subset but they To apply I tried it
Note: This is not worse or better than you, but differently, someone can use it for educational purposes in the future.
The requested protocol.
@protocolprogram requested & lt; NSObject & gt; @required - (NSString *) someString; - (zero) set something: (id) something; @optional - (ntiiittaar) some integer; @end
BaseClass.h
#import "Demand Protocol" #define ThrowOverrideIsMissingException @throw [NSException with Exceptionname: NSInternalInconsistencyException Reason: [NSString stringWithFormat: @ "% S is to be overridden in subsets.", __func__] userInfo: zero] @ interface base class: NSObject & lt; DemandedProtocol & gt; {} @end
base class.m
#import "BaseClass.h" @ Implementation Base Class # Pragya Mark - & lt; Demanded prototol & gt; - (NSString *) Some strings {ThrowOverrideIsMissingException; } - Set something (zero): (id) some {ThrowOverrideIsMissingException; } @end
Comments
Post a Comment