I have a class that has this in the initializer:
#implementation BaseFooClass
-(id) init
{
if (self = [super init])
{
// initialize instance variables that always need to start with this value
}
return self;
}
-(id) initWithSomeInt:(int) someInt
{
if (self = [self init]) // <-- I need to make sure that I am calling BaseFooClass's init here, not SubFooClass's, does that make sense?
{
self.someInt = someInt;
}
return self;
}
#end
That is all fine and dandy. My problem is that when I implement the subclass:
#implementation SubFooClass
-(id) init
{
return [self initWithSomeInt:0];
}
-(id) initWithSomeInt:(int) someInt
{
if (self = [super init]) // <--- Infinite loop (stack overflow :) )
{
// initialize other variables
}
}
#end
I basically need to specifically call the BaseFooClass's init rather than the SubFooClass's init.
I cannot change the way the objects are initialized, as I am converting a project from C# to use in my iPad application.
Thank you all in advance
EDIT:
Due to someone asking, here is my header:
#interface BaseFooClass : NSObject
// implicit from NSObject
// -(id) init;
-(id) initWithSomeInt:(int) someInt;
// more methods
#end
#interface SubFooClass : BaseFooClass
// implicit from NSObject
// -(id) init;
// implicit from BaseFooClass
//-(id) initWithSomeInt:(int) someInt;
#end
Objective-C doesn't work this way because of the way the runtime converts methods into function calls. Self is always an instance of the allocated class, even when invoking the super-class's methods. You need to create your designated initializer for your BaseClassFoo and always go there. So you should be doing something like this:
#implementation BaseFooClass
-(id) init
{
return [self initWithSomeInt:0]; // redirect super class's designated initializer
}
-(id) initWithSomeInt:(int) someInt
{
if ((self = [super init])) // Designated initializer always calls into super class's designated initializer (in this case, NSObject's designated initializer is init
{
self.someInt = someInt;
}
return self;
}
#end
#implementation SubFooClass
// Here we don't override init because our super class's designated initializer
// is initWithSomeInt:
// -(id) init
// {
// return [self initWithSomeInt:0];
// }
// we override this because it's our superclass's designated initializer, plus it
// is ours as well
-(id) initWithSomeInt:(int) someInt
{
if ((self = [super initWithSomeInt:someInt]))
{
// initialize other sub-class specific variables
}
}
#end
You have to call [super initWithSomeInt:someInt]; in the init method of your SubFooClass.
;)
ADDED:
I think is weird you try to call init within iniWithSomeInt . The usual thing would be to call [super initWithSomeInt:someInt] in initIthSomeInt method of SubFooClass and change what you need inside the if clause.
Related
Trying to override init method (to create instance with already initialized tag) in class and getting exception. Code sample:
#interface DiagnosticsReport : NSObject {
}
#property NSString *tag;
- (void) initWithTag:(NSString*) tag;
#end
#implementation DiagnosticsReport
- (id) initWithTag:(NSString*) tag {
if (self = [self init]) {
_tag = tag;
}
return self;
}
- (id) init {
if (self = [super init]) {
// default init here
}
return self;
}
Your declaration of method returns void, while definition returns id. Change both to return instancetype and you're good to go.
I have created a custom class extending UIView. This class has some methods such as Drawerect...
Up to now, I was just putting it in my storyboard and telling that it belongs to the class. I would now allocate and place those objects dynamically. Is there a method so I could call :
[[MyObj alloc] initWithFrame:....]
Id be glad to find any help !
You can create your own constructor in the header file of your class.
The return value is of type id , in its declaration in the main file you need to call a super initialization (for example self = [super initWithFrame:CGRect]) and then return the self. You can customize the parameters of your constructor in the header file to fit your needs.
Example for UIView:
.h:
#import <UIKit/UIKit.h>
#interface CustomView : UIView
-(id)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor;
.m:
#import "CustomView.h"
#implementation CustomView
-(id)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor{
self = [super initWithFrame:frame];
if (self) {
//after allocation you could set variables:
self.backgroundColor = backgroundColor;
}
return self;
}
#end
When instances of UIView are unarchived from an Interface Builder document, their initWithFrame: method isn't called. Instead, the unarchiver calls initWithCoder:. Ideally you should override both methods, and have them call a common method that provides a shared implementation of the initialization code. That way the views will be initialized correctly whether they're instantiated programmatically, or as a result of being unarchived. Here's an example:
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame])) return nil;
[self configureSubviews];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (!(self = [super initWithCoder:aDecoder])) return nil;
[self configureSubviews];
return self;
}
- (void)configureSubviews
{
// Custom configuration code...
}
I have an abstract interface in Objective-C where every sub-class needs to set up a property and then do the exact same thing with that property at the end of init. I'm trying to avoid duplicated code with something like this:
Interface File
#interface Shape : NSObject
#property (nonatomic) PropertyType *prop;
- (id)init;
- (void)initProperty;
#end
Implementation File
#implementation Shape
- (id)init
{
if(self = [super init]) {
[self initProperty];
[prop doSomething];
}
return self;
}
- (void)initProperty
{
}
#end
My problem is that every sub-class will need a different set of parameters passed to initProperty in order to implement the method correctly:
#implementation Rectangle
- (void)initPropertyWithRect:(CGRect)rect
{
prop = [RectangleStuff rectangleWithRect:rect];
}
#end
#implementation Circle
- (void)initPropertyWithRadius:(CGFloat)radius
{
prop = [CircleStuff circleWithRadius:radius];
}
#end
Is there a clean way to do what I'm trying to do in Objective-C? So far, my options seem to be:
Create a "property bag", and just pass around an NSDictionary.
Duplicate the [property doSomething]; code in every subclass.
Somehow pass in a factory object to init, and have the factory object create prop. This approach seems the cleanest, but I'd need the factory object to keep the rect and/or radius as internal state somehow, and that doesn't seem clean to me.
Any thoughts?
I would probably choose #2 (to keep it simple). If the property is only set once
(in the subclass init method), you could override the property setter method in the
superclass, and do the additional stuff there.
Untested code:
- (void)setProp:(PropertyType *)prop
{
_prop = prop; // (Assuming ARC)
[_prop doSomething];
}
First, I feel obligated to mention that your init function should not do anything besides initialize the object. That said, every rule has a time and a place to be broken, so I'll offer what suggestions I can.
Your init function is no different than any other function. You can do things before and after you call super. While generally discouraged, this would be a good place to do it. Your init in your subclass would now look like this:
- (id)init
{
self.myProperty = value;
self = [super init];
if (self) {
// more init stuff
}
return self;
}
I ended up using a variant of what was suggested in the other two answers:
Shape.h
#interface Shape : NSObject
#property (nonatomic) PropertyType *prop;
- (id)initWithProperty:(PropertyType *prop);
#end
Shape.m
#implementation Shape
- (id)initWithProperty:(PropertyType *)prop
{
if(self = [super init]) {
_prop = prop;
[_prop doSomething];
}
return self;
}
#end
Rectangle.m/Circle.m
#implementation Rectangle
- (void)initWithRect:(CGRect)rect
{
return [self initWithProperty:[RectangleStuff rectangleWithRect:rect]];
}
#end
#implementation Circle
- (void)initWithRadius:(CGFloat)radius
{
return [self initWithProperty:[CircleStuff circleWithRadius:radius]];
}
#end
My super class defines a private method called "commonInit" which is only called at construction.
The super class is derived by 2 additional classes, each of which also implement a method called "commonInit"
While constructing the objects of the derived class I see in the debugger that the subclass method is called from the scope of the superclass.
This seems to be very dangerous behavior - even in a trivial case when by coincedence you "overwrite" your superclass private method
How can I overcome this behavior without renaming the method in the super class?
Example:
#interface ASuperView : UIView
#end
#implementation ASuperView
-(id)init
{
self = [super init];
if(self)
{
[self commonInit]; // BOOM - The derived view method is called in this scope
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}
-(void)commonInit
{
//setup the view
}
#end
#interface ADerivedView : ASuperView
#end
#implementation ADerivedView
-(id)init
{
self = [super init];
if(self)
{
[self commonInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}
-(void)commonInit
{
//setup the view for the derived view
}
#end
In this image PXTextMessageBox derived from PXTextBox
Both declare privately the method common init
There is no such thing as 'private' methods in obj-c. At best you can hide the existence of a method from consumers of your header, but by design anyone that has a reference to your object can call any method it implements - even if they don't have that method defined in the header. Your best bet will be to define a new method, say _private_commonInit, and not share that in your class header.
I believe this is actually by design. Polymorphism at its best even! .. self actually refers to the object that originally sent the message (which is not always the class instance where self appears) ... one way to solve this would be to chain the commonInit in the same way Init is chained ... a call to [super commonInit] will invoke the correct method from the subclass ...
I define
#property (nonatomic, assign) int currentUserNum;
#property (nonatomic, assign) BOOL isAlive;
in #interface MyClass
and defined -init method in #implementation MyClass
#synthesize currentUserNum, isAlive;
-(id) init {
if (self = [super init]) {
self.currentUserNum = 0;
self.isAlive = YES;
}
return self;
}
self.currentUserNum = 0; is crashed , but self.isAlive = YES; can work ! They are both assign property.
I want to know why ? Thanks!
Your init method is missing a lot of important code.
- (id)init {
if ((self = [super init])) {
_currentUserNum = 0; // it's not wise to reference properties in the init method
}
return self;
}
Every init method should follow this basic pattern. You assign self the value of calling an appropriate super init or other self init. If that's not nil, you then perform appropriate initialization code, and finally you return self.