Turn: OC Grammar Summary

Source: Internet
Author: User

1. Define the class:
@interface Class Name: Parent class
@end

2. Using: (colon) means inheriting a class
Student:nsobject

3. Use () to define a catagory (category)

* Function: The method of extending the original class without changing the original class structure (cannot extend the attribute), but does not recommend overloading the original class method

* The default file generated by the development tool is: Class name +catagory name
* catagory can be written in a separate file, can also be written in the original class file, how to write according to the needs of the decision.


4, the use of <> to implement a protocol (protocol), if you need to implement multiple protocols, the protocol name is separated by commas are written in parentheses can

* Can be understood as an interface in Java, but the implementation of the class compiler does not enforce all interfaces defined in Protocol
* Identification on the method signature: @required, literally understand that the implementation class must implement the method, in fact, write the same effect as not writing. is also the default
* Identified on the method signature: @optional, which represents the implementation of the implementation class for the method, is optional.


5. Access rights to attributes

* Private: Only within the class can be accessed
* Protected: Class internal and subclass accessible (default)
* Public: No restrictions on access

Cases:

Defined

@interface Student:nsobject {

@private int _age;
@protected int _no;
@public float _height;

}
@end

Use
Student *stu = [[[Student alloc]init]autorelease];
Stu->_height = 20;//The value of the direct read and write property, which is not generally recommended in development. Violates the principle of object-oriented development-"encapsulation

6. Definition and encapsulation of attributes
In Student.h
@interface Student:nsobject {

//1. Define attributes, the default access rights are @protected, only the self and subclass can be accessed directly
in the INT _age;//OC syntax, it is recommended that the attribute name be preceded by an underscore to distinguish it from the parameter name
int _no;

}

2. Provide a declaration of the property's external read and write method for external invocation. OC Create property The naming convention for read-write methods is for, set method: Set+ property name, Get method: property name
Declaring the Get and set methods of age
-(int) age;
-(void) Setage: (int) age;

Get and set methods for declaring no
-(int) No;
-(void) Setno: (int) No;
@end

In STUDENT.M implementing a method in a header file
#import "Student.h"//Import header file
@implementation Student

Implementing methods in the header file
-(int) Age {//Getter method for age

return _age;

}

-(void) Setage: (int.) Age {//The setter method of age

_age = age;

}

-(int) No {

return _no;

}

-(void) Setno: (int) No {

_no = no;

}
@end

7. The implementation of the property getter and setter methods is automatically generated using @synthesize, and a member variable with an underscore (_) + property name is generated. Need to be used in conjunction with @property. Cases:
@implementation Student

@synthesize age;//This sentence the get and set methods below the top, generated automatically by @synthesize
/*
* 1. If the display implements the Get method, @synthesize automatically generates the property's Set method implementation
* 2. If the display implements the Set method, @synthesize automatically generates the property's Get method implementation
* 3. If the Get method is not implemented and the Set method is not implemented, then @synthesize automatically generates the property's get and set method implementations
*/
-(int) Age {//Getter method for age

return _age;

}

-(void) Setage: (int.) Age {//The setter method of age

_age = age;

}

@end

Note: In the post-xcode4.5 compilation environment, you do not need to write the get and set method implementations of the @synthesize Declaration generation property. The Get and set method implementations of the corresponding properties are automatically generated in the. m file only by using the @property definition in the header file

8. Using @property to declare a property, the compiler automatically generates a declaration of the property's getter and setter methods

The standard implementation of the property getter and setter methods is automatically generated in the. m file in the xcode4.5 version of the compiler environment, without the getter and setter method implementations that declare the attributes to be generated using @synthesize.


9. @property attribute Parameters
1> nonatomic: There is no need for thread protection in multi-threaded environments (no lock during read and write),
Atomic: (default) Multithreaded environments require thread protection (read and write lock).
2> ReadOnly: Implementation of Getter methods that represent only properties generated in the. m file
Readwriter: Represents the implementation of getter and setter methods that generate attributes in. m files (default)
3> retain: Represents the setter method that invokes the property, first release the old value, and then retain the new value. A generic declared member variable is a subclass of NSObject to which the parameter is added
Assign: Generate standard getter and setter method implementations (default), assigning values directly to attributes
4> getter=: Represents the Getter method name generated by the custom property
Setter=: Represents the Setter method name generated by the custom attribute

10. Method Invocation
* [Instance Object method name: Parameter list]
* [Class Name Method Name: Parameter list]
Cases:
Student *stu = [[[Student alloc]init]autorelease];
[Stu Setage:22 andno:10];//Invoke instance method

[Student initwithage:20];//Call static method

11, point (.) Grammar
Student *stu = [[[Student alloc]init]autorelease];
Stu.age = 20; Equivalent to the Setage method that called the object (write)
int age = stu.age;//equals the Getage method that called the object (read)

12. Self keyword
The equivalent of this in Java, the difference is that self in different environments, the role is different. In an instance method, self can be used when the object is used, and in a static method, self is available when the class object is in use.
Such as:
-(void) Age {
Return self.age;//Here Self is the instance object itself
}

+ (ID) newinstace {

//student *stu = [[[Student alloc]init]autorelease];
Student *stu = [[self alloc]init]autorelease];//The effect of this sentence and the previous code is equivalent, whereas in Java syntax it is not allowed to appear in a static method

return Stu;
}

13. @class: Declare the existence of a class in the header file

To improve efficiency, you do not need to import the header files of a class in situations where you only have to know the existence of the class.

#import "Book.h"
@class book; You do not need to import the Book.h header file and import it when the. m file is actually used.
@interface Student:nsobject
@property book *book;
@end

14. @protocol: Declare the existence of a protocol in the header file

The same purpose as @class.


15. ^: Defines a block type that is very similar to the notation for function pointer types in standard C syntax
For example, define a block type of sum with a return value of int, with two int parameters
Int (^sum) (int, int) = ^ (int a, int b) {

return a + B;

};

16.#import: Header file for importing a class
#pragma mark: Notes for writing methods
#pragma mark-: Method Comment grouping
+: Declare or define a static method
-: Declaring or defining an instance method

17. Method Definition (a colon is a parameter, and a colon is part of a property method)
Method type (return value type) method name [parameter list] {(a colon represents a parameter, and a colon is part of a property method)
Method body
}

* Instance method, method name: Sumage:andno:
-(int) Setage: (int) Age Andno: (int) No {
Insert the code logic here
}

* Static method
+ (ID) initwithage: (int) Age {
Insert the code logic here
}
Note:
* Instance method accessed through the instance object of the class
* Static method access by class name or self

18. Memory Management
1> All classes inherited from the NSObject need to manage their own memory, in OC syntax, any object created has a reference counter, the first time the reference counter is 1, when the value of the reference counter is 0 o'clock, the object is destroyed. Memory management involves the following interfaces:
* Release: object's reference counter minus 1
* Retain: Object reference counter plus 1
* Retaincount: Gets the number of object current reference counters

Object life Cycle Callback interface:
* Init: The default constructor method for an object, if the custom constructor method is used to initialize the member variable, you must first call the parent class's construction method and determine whether the obtained object is nil, and then initialize the member variable.
such as:-(void) Initwithage: (int) Age {

if (self = [super init]) {
_age = age;
 }
return self;

}

* Dealloc: When an object is destroyed, the system automatically calls the method, usually freeing memory or other resources in the method. When overriding the Dealloc method, notice that the Dealloc method of the parent class is called at the end of the code to release related resources such as memory.

Such as:

-(void) Dealloc {

[_book release];//release member variable
[Super Dealloc];

}

2> objects that do not need to manage memory
* Basic Data type
* The system comes with a class that calls its own static method to create an object that is automatically freed of memory and does not need to be managed

3> Memory Management Principles
The release operation is only necessary if the ALLOC,RETAIN,COPY,NEW message is sent to the object
* Who Alloc,retain,copy,new who release
* Who created who released (release)
* Who doesn't have allock,retain,copy,new, you don't want to do the release operation

4> Automatic memory management (managed by Autoreleasepool)
When an object is created, the Autorelease method is called, and a reference to the object is automatically stored in a recently created automatic release pool. In the future, the object does not need to manually release the operation, Xu Fei did the retain,copy and so on to modify the reference counter operation. When the auto-free pool is destroyed, a release message is sent to all objects in the pool, and the reference counter for all objects in the pool is reduced by 1, and the object is completely destroyed only if the reference counter in the pool is 0 o'clock. This is not to say that the pool is destroyed and all objects in the pool will be destroyed as long as the object is handed over to the automatic release tank.

such as: @autoreleasepool {

Student *stu = [[[Student alloc] init]autorelease];//This time the Stu object will be placed in the auto-release pool in this curly brace

[Stu retain];//If this sentence is added, at this point the Stu reference counter is 2, this time if the pool is destroyed before the release message is sent to the object, even if the pool is destroyed, the object will still cause memory leaks

}//The program executes here, representing the automatic release pool being destroyed, which means that all objects in the pool will receive a release message

19. Java-like ToString method in OC
Student *stu = [[[Student alloc]init]autorelease];
NSLog (@ "%@", Stu);//The memory address of the Stu object is printed by default

* Override the Description method of the parent class to customize the information of the printed object
-(NSString *) Description {
NSLog (@ "Age is%i", _age);

}

Turn: OC Grammar Summary

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.