Wednesday, 15 September 2010

Is it possible to make the -init method private in Objective-C? -



Is it possible to make the -init method private in Objective-C? -

i need hide (make private) -init method of class in objective-c.

how can that?

objective-c, smalltalk, has no concept of "private" versus "public" methods. message can sent object @ time.

what can throw nsinternalinconsistencyexception if -init method invoked:

- (id)init { [self release]; @throw [nsexception exceptionwithname:nsinternalinconsistencyexception reason:@"-init not valid initializer class foo" userinfo:nil]; homecoming nil; }

the other alternative — far improve in practice — create -init sensible class if @ possible.

if you're trying because you're trying "ensure" singleton object used, don't bother. specifically, don't bother "override +allocwithzone:, -init, -retain, -release" method of creating singletons. it's virtually unnecessary , adding complication no real important advantage.

instead, write code such +sharedwhatever method how access singleton, , document way singleton instance in header. should need in vast bulk of cases.

objective-c

No comments:

Post a Comment