Saneef
Saneef

Reputation: 8890

What is the Objective-C "constructor"?

I have this class:

@interface RSSEntry : NSObject{
    NSString *blogTitle;
    NSString *articleTitle;
    NSString *articleUrl;
    NSDate *articleDate;
}

@property (copy) NSString *blogTitle;
@property (copy) NSString *articleTitle;
@property (copy) NSString *articleUrl;
@property (copy) NSDate *articleDate;

@end

Now, how will I make these member variables get initialized when creating an object, especially when ARC is enabled in the Xcode project? I'm new to Objective-C; I'm looking for something like constructor from C base languages.

Upvotes: 8

Views: 11014

Answers (2)

user405725
user405725

Reputation:

In Objective-C there are no constructors. However, it is a common practice to create init method and have users call alloc immediately followed by init. See Objective-C constructors for example.

Upvotes: 8

Vincent Bernier
Vincent Bernier

Reputation: 8664

you init an object like this

RSSEntry *e = [[RSSEntry alloc] init];

so you overwrite the init method.

Your init will look something like this

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) 
{
    lesImagesViews = nil;
}
return self;
}

This is the init for a UIViewController subClass other class have default init that are just "init" with no parameter.

Upvotes: 4

Related Questions