Objective C language is used in iOS development, it is an object-oriented language
Interface and implementation
The file completed in Objective is called the interface file, and the definition of this type of file is called the implementation file.
A simple interface file MyClass.h will:
1 @interace MyClass: NSObject {
2 // class variable declaration
3}
4 // class attribute declaration
5 // class methods and declarations
6 @end
Execute MyClass.m file as shown below
1 @implementation MyClass
2 // class method definition
3 @end
Create object
Finish creating the object as shown below
MyClass * objectName = [[MyClass alloc] init];
Methods
The methods declared in Objective C are as follows:
-(returnType) methodName: (typeName) variable1: (typeName) variable2;
An example is shown below:
-(void) calculateAreaForRectangleWithLength: (CGfloat) length
andBreadth: (CGfloat) breadth;
What you might think of is the andBreadth string, but its optional string can help us read and understand the method, especially when the method is called.
To call this method in the same class, we use the following statement.
[self calculateAreaForRectangleWithLength: 30 andBreadth: 20];
As mentioned above, the use of andBreath helps us understand that breath is 20. Self is used to specify that it is a method of a class.
Class methods
You can access class methods directly without creating objects. They do not have any variables associated with it. Examples are as follows:
+ (void) simpleClassMethod;
It can be accessed by using the class name (assuming as MyClass class name) as follows:
[MyClass simpleClassMethod];
Instance method
You can only create instance of class objects after accessing instance methods and memory to which instance variables are allocated. The instance method looks like this:
-(void) simpleInstanceMethod;
After the object of the class is created, it can access it. As follows:
MyClass * objectName = [[MyClass alloc] init];
[objectName simpleInstanceMethod];
Important data types in Objective C
Number data type
1 NSString string
2 Basic types of CGfloat floating point values
3 NSInteger integer
4 BOOL
Print log
NSLog is used to print a statement, which will be printed on the device log and debug versions of the console and separate debug modes.
Such as
NSlog (@ "");
Control structure
Except for a few additional clauses, most control structures are the same as C and C ++
Properties
Variable properties for accessing external classes of the class
For example: @property (nonatomic, strong) NSString * myString
Access attribute
You can use the dot operator to access attributes. To access the previous attribute, you can do the following:
self.myString = @ "Test";
You can also use the set method as follows:
[self setMyString: @ "Test"];
Categories
Classes are used to add methods to existing classes. This way you can add methods to the class, and you can define the actual class in it without even executing the file. The sample classes of MyClass are as follows:
-(void) sampleCategoryMethod {
NSLog (@ "Just a test category");
}
Arrays
NSMutable and NSArray are array classes used in ObjectiveC. The former is a mutable array and the latter is an immutable array. as follows:
NSMutableArray * aMutableArray = [[NSMutableArray alloc] init];
[anArray addObject: @ "firstobject"];
NSArray * aImmutableArray = [[NSArray alloc]
initWithObjects: @ "firstObject", nil];
dictionary
NSMutableDictionary and NSDictionary are dictionaries used in Objective, the former is a variable dictionary, and the latter is an immutable dictionary, as shown below:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.