Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Object - Objective-C

Problem

I am trying to create an object in Objective-C. I know how to do it but I have a few questions regarding the methods in the implementation file.

The Object:

@interface Program : NSObject {
    NSString *sid;
    NSString *le;
    NSString *sd;
    NSString *pid;
    NSString *n;
    NSString *d;
    NSString *url;
}

@property (nonatomic, retain) NSString *sid;
@property (nonatomic, retain) NSString *le;
@property (nonatomic, retain) NSString *sd;
@property (nonatomic, retain) NSString *pid;
@property (nonatomic, retain) NSString *n;
@property (nonatomic, retain) NSString *d;
@property (nonatomic, retain) NSString *url;

@end

Question

I should only implement dealloc and init.. Am I right on this?

Furthermore, I have no special initializations, so should I keep the default init method as follows?

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}
like image 986
darksky Avatar asked Sep 13 '26 12:09

darksky


2 Answers

You need to synthesize the properties and if you don't need any custom initialization then you can keep the init method as it is. In fact there is no need to implement init here. But in dealloc you need to release the strings.

@implementation Program

@synthesize sid;
// ... synthesize others

- (void)dealloc {
    [sid release];
    // ... release others

    [super dealloc];
}

@end
like image 90
taskinoor Avatar answered Sep 15 '26 01:09

taskinoor


I may be wrong, and I'm sure someone will correct me ;), but if you're not doing any special initializations, you don't need the init method.

like image 20
John T Avatar answered Sep 15 '26 03:09

John T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!