Override default init function in iOS

initially, I have a default init function

-(id) init { if (self=[super init]) { ...... } return self; } 

However, I like to override the init function to pass in user objects or other objects as

  -(id) initWithScore:(NSString*) score { if (self=[super init]) 

Now an error occurs saying that the [super init] function can only be called using the init () function.

So what should I do to fix this so that I can pass objects as well as use self = [super init]?

Error: cannot be assigned independently outside the method in the init family.

+4
source share
2 answers

I tried to convert the project to ARC, and after creating a new one and including files from the old one, one of the problems I received was

Cannot assign self outside method in init family

The name of the selector MUST begin with init - not only in my case, the initialization selector was:

 -(id)initwithPage:(unsigned)pageNum {...} 

Pay attention to the little 'w'.

I changed it to:

 -(id)initwithPage:(unsigned)pageNum {...} 

Pay attention to the capital "W"!

My problem is resolved.

I hope this helps someone.

+23
source

You need to return the type identifier object to the new method.

Suppose you declared the NSString * myscore property, you write something like this:

 -(id) initWithScore:(NSString*) score { self=[super init]; if (self) { self.myscore = score; } return self; } 
+1
source

All Articles