Objective C Objects by Example
Objects are instance of classes that has its own memory allocated for instance variable/properties. All objects share the memory for the methods. A sample for object instantiation is shown below.
@interface SampleClass:NSObject
// Properties declaration
@property(nonatomic, strong) NSString *string;
// Method declaration
- (void)start:(id)obj;
@end
@implementation SampleClass
- (void)start:(id)obj{
// Method definition
}
@end
int main(int argc, const char * argv[])
{
// Autoreleasepool used in main for automatic memory management.
@autoreleasepool {
// A simple log
NSLog(@"Hello, World!");
// Creating object
SampleClass *sampleClass = [[SampleClass alloc] init];
// Setting property
sampleClass.color = @"test";
// Accessing instance method
[sampleClass test];
}
return 0;
}