The terminology used in iOS growth is purpose C. It is an object-oriented terminology and hence, it would be simple for those who have some qualifications in object-oriented growth 'languages'.
Interface and Implementation
In Objective C, the computer file where the declaration of category is done is called the interface computer file and the computer file where the category is defined is called the implementation computer file.
A simple interface file MyClass.h would look like the following −
@interface MyClass:NSObject{ // class variable declared here } // class properties declared here // class methods and instance methods declared here @end
The implementation file MyClass.m would be as follows −
@implementation MyClass // class methods defined here @end
Object Creation
Object creation is done as follows −
MyClass *objectName = [[MyClass alloc]init] ;
Methods
Method is declared in Objective C as follows −
-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
An example is shown below.
-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth;
You might be thinking what the andBreadth sequence is for; actually it’s an optionally available sequence, which allows us study and comprehend the technique quickly, especially at plenty of duration of contacting. To contact this technique in the same category, we use the following declaration −
[self calculateAreaForRectangleWithLength:30 andBreadth:20];
As said above, the use of andBreadth allows us know that depth is 20. Self is used to specify that it's a category technique.
Class Methods
Category techniques can be utilized straight without developing things for the course. They don't have any factors and things associated with it. An example is proven below.
+(void)simpleClassMethod;
It can be accessed by using the class name (let's assume the class name as MyClass) as follows −
[MyClass simpleClassMethod];
Instance Methods
Example techniques can be utilized only after developing an item for the category. Storage is assigned to the instance factors. An example instance technique is proven below.
-(void)simpleInstanceMethod;
It can be accessed after creating an object for the class as follows −
MyClass *objectName = [[MyClass alloc]init] ; [objectName simpleInstanceMethod];
Important Data Types in Objective C
S.N. | Data Type |
---|---|
1 | NSString
It is used for representing a string.
|
2 | CGfloat
It is used for representing a floating point value (normal float is also allowed but it's better to use CGfloat).
|
3 | NSInteger
It is used for representing integer.
|
4 | BOOL
It is used for representing Boolean (YES or NO are BOOL types allowed).
|
Printing Logs
NSLog - used for publishing a declaration. It will be printed in the product records and debug system in launch and debug ways respectively. For example,
NSlog(@"");
Control Structures
Most of the control structures are same as in C and C++, except for a few additions like for in statement.
Properties
For an external class to access the class, variable properties are used. For example,
@property(nonatomic , strong) NSString *myString;
Accessing Properties
You can use dot operator to access properties. To access the above property, we will do the following.
self.myString = @"Test";
You can also use the set method as follows −
[self setMyString:@"Test"];
Categories
Groups are used to add methods to the current sessions. By this way, we can add method to sessions for which we don't have even execution information where the real classification is determined. A example classification for our classification is as follows −
@interface MyClass(customAdditions) - (void)sampleCategoryMethod; @end @implementation MyClass(categoryAdditions) -(void)sampleCategoryMethod{ NSLog(@"Just a test category"); }
Arrays
NSMutableArray and NSArray are the array classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.
NSMutableArray *aMutableArray = [[NSMutableArray alloc]init]; [anArray addObject:@"firstobject"]; NSArray *aImmutableArray = [[NSArray alloc] initWithObjects:@"firstObject",nil];
Dictionary
NSMutableDictionary and NSDictionary are the dictionary classes used in objective C. As the name suggests, the former is mutable and the latter is immutable. An example is shown below.
NSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init]; [aMutableDictionary setObject:@"firstobject" forKey:@"aKey"]; NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects: @"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]];
Emoticon Emoticon