Why can't I initialize a class through a setter?
- by Rob emenaker
If I have a custom class called Tires:
#import <Foundation/Foundation.h>
@interface Tires : NSObject {
@private
NSString *brand;
int size;
}
@property (nonatomic,copy) NSString *brand;
@property int size;
- (id)init;
- (void)dealloc;
@end
=============================================
#import "Tires.h"
@implementation Tires
@synthesize brand, size;
- (id)init {
if (self = [super init]) {
[self setBrand:[[NSString alloc] initWithString:@""]];
[self setSize:0];
}
return self;
}
- (void)dealloc {
[super dealloc];
[brand release];
}
@end
And I synthesize a setter and getter in my View Controller:
#import <UIKit/UIKit.h>
#import "Tires.h"
@interface testViewController : UIViewController {
Tires *frontLeft, *frontRight, *backleft, *backRight;
}
@property (nonatomic,copy) Tires *frontLeft, *frontRight, *backleft, *backRight;
@end
====================================
#import "testViewController.h"
@implementation testViewController
@synthesize frontLeft, frontRight, backleft, backRight;
- (void)viewDidLoad {
[super viewDidLoad];
[self setFrontLeft:[[Tires alloc] init]];
}
- (void)dealloc {
[super dealloc];
}
@end
It dies after [self setFrontLeft:[[Tires alloc] init]] comes back. It compiles just fine and when I run the debugger it actually gets all the way through the init method on Tires, but once it comes back it just dies and the view never appears. However if I change the viewDidLoad method to:
- (void)viewDidLoad {
[super viewDidLoad];
frontLeft = [[Tires alloc] init];
}
It works just fine. I could just ditch the setter and access the frontLeft variable directly, but I was under the impression I should use setters and getters as much as possible and logically it seems like the setFrontLeft method should work.
This brings up an additional question that my coworkers keep asking in these regards (we are all new to Objective-C); why use a setter and getter at all if you are in the same class as those setters and getters.