Object only initialisable through factory method (Objective-c) -
I am trying to create an object through a factory method. I used the init
to throw an exception (see). However it meant that I would have to make a secret INIT in order to use it in a factory method.
// factory method - (URLReqs *) buildURLReq: (NSString *) location {URLReqs * tmp = [[URLReqs alloc] secretInit]; Tmp.str = Place Return TMP; } - (id) secret entry {returns [super-init]; }
This method is confusing and whenever we can avoid declaring secretInit in the header file, no one can still access it. Is there a good solution?
An idea is to try to call init directly on the Super Object of URLReqs instead of creating a function.
You do not want to do this like this, but it is possible:
#include & lt; Objc / message.h> @ Implementation MicroLOS + (ID) factory rulespace: (NSString *) Some strings; {Struct objc_super Example Super; Id myInstance = [self assign]; InstanceSuper.receiver = myInstance; Example Super Super-class = [self super class]; // instanceSuper.class = [self Super class]; // Use this line if this does not compile my above mentioned hyphen = objc_msgSendSuper (& exampleSuper, @selector (init)); // Continue establishing the income of my instances. . [My instant set variable: some string]; Make me return; } - (id) init {self = [super init]; If (! Self!) Return zero; [Self release]; [Self doesNotRecogniseSelector: _cmd]; Return zero; } @end
This will give you init
messages to myInstance
's & ldquo; Super & rdquo; Will continue to send. Object, but any init
to MyClass
instances
The sending will not be allowed, however be careful, though it is using standard runtime function objc / *. H
, Apple is not expected to keep this runtime API compatible.
Comments
Post a Comment