Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Objective-C "constructor"?

Tags:

People also ask

What is the purpose of a constructor in C?

A Constructor in C is used in the memory management of C++programming. It allows built-in data types like int, float and user-defined data types such as class. Constructor in Object-oriented programming initializes the variable of a user-defined data type. Constructor helps in the creation of an object.

Does Objective-C have constructors and destructors?

In Objective-C you would do so in the init method even though you create a convenience constructor. The convenience constructor as the name suggests is a shortcut so you don't have to write out two statements namely: alloc and init.

What are Objective-C properties?

The Objective-C declared properties feature provides a simple way to declare and implement an object's accessor methods.

How do I initialize a class in Objective-C?

Test example = [[Test alloc] init]; example.name = @"s"; you can write something like this: Test example = [[Test alloc] initWithName:@"s"]; Please note that this is very common for initialization method to return newly created object, hence the initialization method usually returns 'id' type (not void).


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.