My OBJECTIVE-C Study notes

Source: Internet
Author: User

1. Add tags to your code

#pragma mark-
#pragma mark

2. Dot syntax

Person *p = [person new];
The essence of point syntax or method invocation
P.age = 10; [P setage:10];
int a = P.age; [P age];

3. Scope of member variables

@public: The member variables of an object can be accessed directly from anywhere
@private: can only be accessed directly in the object method of the current class (default is @private in @implementation)
@protected: can be accessed directly in the object methods of the current class and its subclasses (the default is @protected in @interface)
@package: You can directly access the member variables of an object as long as you are in the same frame
member variables with the same name cannot be declared in the @interface and @implementation

4. Construction method

Constructor method: The method used to initialize the object, is an object method,-the beginning
Overriding the purpose of the construction method: In order for the object to be created, the member variable will have some fixed values

Note points for overriding the construction method:

1. Call the construction method of the parent class first ([Super Init])
2. Initialization of the subclass internal member variables

5.category-classification

The function of classification: You can add some methods to the class without changing the contents of the original class.

Use Note:

1. Classification can only increase the method, cannot increase the member variable
2. member variables declared in the original class can be accessed in the classification method implementation
3. Classification can re-implement the methods in the original class, but will overwrite the original method, will cause the original method can not be used
4. Priority of Method Invocation: Classification (the last category that participates in compilation takes precedence)--and the original class---The parent class

Load and initialize of the 6.category-category

1. When the program starts, all classes and classifications in the project are loaded, and the +load method for each class and classification is called after it is loaded. will only be called once.

2. When a class is used for the first time, the +initialize method of the current class is called

3. Load the parent class first, then load the subclass (call the +load method of the parent class first, and then call the +load method of the subclass)
Initializes the parent class first, initializes the subclass (invokes the +initialize method of the parent class, and then calls the +initialize method of the subclass)

7. Using NSLog and%@ output objects

By default, when you use NSLog and%@ to output objects, the result is the:< class name: Memory address >

1. The-description method that invokes the object P
2. Get the return value of the-description method (NSString *) displayed on the screen
The 3.-description method returns the "class name + memory address" by default


8.SEL

The SEL is actually a wrapper around the method, wrapping the method into an SEL type of data to find the corresponding method address. You can call a method if you find a method address
Actually, the message is sel.


SEL s = @selector (test2:);
[P performselector:s withobject:@ "456"];
[P test2];

1. Packaging test2 into SEL-type data
2. Find the corresponding method address according to the SEL data
3. Call the corresponding method according to the method address

9.ARC

ARC's judgment: The object is freed as long as no strong pointer is pointed to the object

1.ARC Features

1> does not allow calls to release, retain, Retaincount
2> allows rewriting of dealloc, but does not allow calls to [super Dealloc]
3> @property Parameters:

Strong: Member variable is strong pointer (for OC object type)
Weak: member variable is weak pointer (for OC object type)
Assign: For non-OC object types

4> former retain changed to strong

The hands are divided into 2 types:

1> Strong pointer: By default, all pointers are strong pointers __strong

2> Weak pointer: __weak

10. Circular References

When both ends are circular references, the solution:


1> ARC
1 End With strong, another 1 end with weak

2> Non-Arc
1 End with retain, another 1 end with assign

11.block

What the block needs to know.

1> How to define a block variable

Int (^sumblock) (int, int);
void (^myblock) ();

2> How to use block encapsulation code

^ (int a, int b) {
return a-B;
};


3> block access outside variables

* External variables can be accessed inside the block
* By default, the outside local variables cannot be modified inside the block
* Add __block keyword to local variable, this local variable can be modified inside block

4> defining block types with typedef

typedef int (^myblock) (int, int);
You can then use this type of Myblock to define the block variable
Myblock Block;
Myblock B1, B2;

12.protocol protocol

1. Definition of the Agreement

@protocol Agreement name <NSObject>
Method declaration List ....
@end


2. How to comply with the agreement

1> Class Compliance Agreement
@interface Class Name: Parent class name < protocol name 1, protocol name 2>

@end

2> Protocol Compliance Agreement
@protocol Agreement name < Other protocol name 1, other protocol name 2>

@end

3. Keywords for method declarations in the agreement

1> @required (default)
Requires implementation, and if not implemented, a warning is issued

2> @optional
Does not require implementation, how can there be no warning

4. When defining a variable, limit the object that this variable holds to a protocol

Class name < protocol name > * variable name;
id< protocol name > variable name;
Nsobject<myprotocol> *obj;
Id<myprotocol> Obj2;

If the corresponding protocol is not followed, the compiler warns

The attributes declared in [email protected] can also be used as a limit to comply with the agreement

@property (Nonatomic, Strong) class name < protocol name > * attribute name;
@property (nonatomic, strong) id< protocol name > attribute name;

@property (nonatomic, strong) dog<myprotocol> *dog;
@property (nonatomic, strong) id<myprotocol> dog2;

6. The agreement can be defined in a separate. h file and can also be defined in a class

1> If this protocol is used only in a class, the protocol should be defined in that class

2> If this protocol is used in many classes, it should be defined in a separate file

7. Classifications can be defined in separate. h and. m files and can also be defined in the original class

1> in general, are defined in a separate file
2> defines the classification in the original class, only requires the ability to read the grammar




My OBJECTIVE-C Study notes

Related Article

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.