Singleton pattern in objc, how to keep init private?

后端 未结 4 1217
说谎
说谎 2020-12-19 23:47

How can i make sure user do not call init, instead client should call sharedSingleton to get a shared instance.

@synthesize delegate;

- (id)init
{
    self          


        
4条回答
  •  余生分开走
    2020-12-20 00:05

    You can't make methods private in Objective-C. You could raise a NSException if the wrong initializer is invoked.

    - (id)init
    {
         [NSException exceptionWithName:@"InvalidOperation" reason:@"Cannot invoke init." userInfo:nil];
    }
    

提交回复
热议问题