http://ipetrovich.blogspot.kr/
http://inspired-ui.com/tagged/empty_data_sets
1 2 3 4 5 6 7 8 9 |
@interface Box:NSObject { //Instance variables double length; // Length of a box double breadth; // Breadth of a box } @property(nonatomic, readwrite) double height; // Property @end |
The instance variables are private and are only accessible inside the class implementation.
Accessing the Data Members:
The properties of objects of a class can be accessed using the direct member access operator (.)
Properties:
Properties are introduced in Objective-C to ensure that the instance variable of the class can be accessed outside the class.
The various parts are the property declaration are as follows.
- Properties begin with @property, which is a keyword
- It is followed with access specifiers, which are nonatomic or atomic, readwrite or readonly and strong, unsafe_unretained or weak. This varies based on the type of the variable. For any pointer type, we can use strong, unsafe_unretained or weak. Similarly for other types we can use readwrite or readonly.
- This is followed by the datatype of the variable.
- Finally, we have the property name terminated by a semicolon.
- We can add synthesize statement in the implementation class. But in the latest XCode, the synthesis part is taken care by the XCode and you need not include synthesize statement.
Objective-C Inheritance
@interface derived-class: base-class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *personName; NSInteger personAge; } - (id)initWithName:(NSString *)name andAge:(NSInteger)age; - (void)print; @end @implementation Person - (id)initWithName:(NSString *)name andAge:(NSInteger)age{ personName = name; personAge = age; return self; } - (void)print{ NSLog(@"Name: %@", personName); NSLog(@"Age: %ld", personAge); } @end @interface Employee : Person { NSString *employeeEducation; } - (id)initWithName:(NSString *)name andAge:(NSInteger)age andEducation:(NSString *)education; - (void)print; @end @implementation Employee - (id)initWithName:(NSString *)name andAge:(NSInteger)age andEducation: (NSString *)education { personName = name; personAge = age; employeeEducation = education; return self; } - (void)print { NSLog(@"Name: %@", personName); NSLog(@"Age: %ld", personAge); NSLog(@"Education: %@", employeeEducation); } @end int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Base class Person Object"); Person *person = [[Person alloc]initWithName:@"Raj" andAge:5]; [person print]; NSLog(@"Inherited Class Employee Object"); Employee *employee = [[Employee alloc]initWithName:@"Raj" andAge:5 andEducation:@"MBA"]; [employee print]; [pool drain]; return 0; } |
Objective-C Polymorphism
Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#import <Foundation/Foundation.h> @interface Shape : NSObject { CGFloat area; } - (void)printArea; - (void)calculateArea; @end @implementation Shape - (void)printArea{ NSLog(@"The area is %f", area); } - (void)calculateArea{ } @end @interface Square : Shape { CGFloat length; } - (id)initWithSide:(CGFloat)side; - (void)calculateArea; @end @implementation Square - (id)initWithSide:(CGFloat)side{ length = side; return self; } - (void)calculateArea{ area = length * length; } - (void)printArea{ NSLog(@"The area of square is %f", area); } @end @interface Rectangle : Shape { CGFloat length; CGFloat breadth; } - (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth; @end @implementation Rectangle - (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth{ length = rLength; breadth = rBreadth; return self; } - (void)calculateArea{ area = length * breadth; } @end int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Shape *square = [[Square alloc]initWithSide:10.0]; [square calculateArea]; [square printArea]; Shape *rect = [[Rectangle alloc] initWithLength:10.0 andBreadth:5.0]; [rect calculateArea]; [rect printArea]; [pool drain]; return 0; } |
When the above code is compiled and executed, it produces the following result:
1 2 |
2013-09-22 21:21:50.785 Polymorphism[358:303] The area of square is 100.000000 2013-09-22 21:21:50.786 Polymorphism[358:303] The area is 50.000000 |
Objective-C Data Encapsulation
Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
Objective-C Categories
Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. In order add such extension to existing classes, Objective-C provides categories and extensions.
1 2 3 |
@interface ClassName (CategoryName) @end |
Ví dụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#import <Foundation/Foundation.h> @interface NSString(MyAdditions) +(NSString *)getCopyRightString; @end @implementation NSString(MyAdditions) +(NSString *)getCopyRightString{ return @"Copyright TutorialsPoint.com 2013"; } @end int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *copyrightString = [NSString getCopyRightString]; NSLog(@"Accessing Category: %@",copyrightString); [pool drain]; return 0; } |
Objective-C Protocols
Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.
1 2 3 4 5 6 |
@protocol ProtocolName @required // list of required methods @optional // list of optional methods @end |
Here is the syntax for class conforming to protocol
1 2 3 |
@interface MyClass : NSObject <MyProtocol> ... @end |