Classification and memory management

Source: Internet
Author: User

I. The role of classification: You can add some methods to a 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) method with the same name, highest priority of classification (method refactoring, overriding method in original Class)

4) Method invocation Priority: Category------Parent class (after compilation overwrites first compiled (. h file not compiled))

Two. SEL:

1) Wrap the method 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

4) Each method has an object of the corresponding SEL type

Example: [P performselect: @selector (test:) withobject:@ "@123"];

[P test3:@ "123"];

SEL Type creation:

SEL s = @selector (test3:);

NSString *name = @ "Test2";

SEL s = nsselectorfromstring (name);

[P performselector:s];

Each method has a _cmd variable of type SEL that represents the current method.

NSString *str = Nsstrigfromslelector (_cmd); Print Method Name

Actually, the message is sel.

Three. Memory management

1) Memory Management scope: Any object that inherits NSObject, to other basic data types (Int,char, ... ) is invalid

2) Variable memory: At the end of the code block (stack space) local variable destruction (C-language auto-recycle (detection Scope)), (heap space dynamic allocation) but the object does not disappear, need to manually release (send message)

3) Reference counter: Each object has its own reference counter, which is an integer representing "the number of times the object is referenced", that is, how many people are using the OC object (with 4 bytes to store the value of the reference counter)

4) Reference counter operation:

Send retain message to object, counter +1 (hot state method returns the object itself)

Send release message to object, counter-1

You can send an Retaincount message to an object to get the current reference counter value

5) Destruction of objects:

Typically overrides the Dealloc method to release the associated resource

Dealloc is like an object's last word (called when an object is recycled) (call [Super Dealloc] and put it on the final side)

6) Wild pointer: Pointer to a Zombie object (the called memory has been recycled to the object, the zombie object can no longer be used) pointers (pointing to unusable memory) if you continue to access memory, a bad access error occurs: Exc_bad_access (object dead cannot be resurrected)

7) @property generated set method is a direct assignment, there is no tube, @property (retain) book *book automatically add retain to the Set method

8) Set method memory management related parameters

1.*retain:release old value, retain new value (for OC object type)

*assign: Direct Assignment (default, for non-OC object types)

*copy:release old value, copy new value

2. Whether to generate a set method

*readwrite: Simultaneous production of setters and getter declarations to achieve

*readonly: Only a getter declaration is generated, which implements

@property (ReadWrite, assign) int heightj;

3.oc Object Type:

@property (nonatomic, retain) class name, property name

4. Non-OC Object type:

@property (nonatomic, assign) type name, attribute name;

9) Multithreading Management

*nonatomic: High Performance

*atomic: Low performance (default)

The name of the setter and getter method

*setter: Determines the name of the set method and must have a colon:

*getter: Determines the name of the Get method (typically used in the bool type get method)

11) When two classes need to be included with each other, you cannot use #import in. h, you need both sides to use the @class class name (tell the compiler this is a class), you can use #import in. m files to include interfaces

12) When there is a circular reference solution at both ends with retain, one end with assign

13) automatic release of the pool: (Delay the release time of the object)

When the Autorelease method is called, the object's counter does not change

The Autorelease method returns the object itself, placing the object in an auto-free pool (after creating a multilayer pool, stored as a stack), and when the auto-free pool is destroyed, a release operation is done for all objects inside the pool, created in curly braces and

1. Do not use the release time of the object of concern

2. No more worrying about when to call release

3. Objects with large memory do not use Autorelease

Note: Release and autorelease can only use one of them and cannot be used multiple times autorelease

Sometimes the class method is used to quickly return a Autorelease object, do not use the class name directly when creating the object, generally use the self

+ (ID) person{

return [[[Self alloc] init] autorelease];

}

The system comes with a method that contains no alloc,new,copy, indicating that the returned object is Autorelease

Arc: The object is freed as long as no strong pointer is pointed to the object

There are two types of pointers:

Strong pointers: By default, all pointers are strong pointers

Weak pointer: __weak

Characteristics:

1. Calling Release,retain,retaincount is not allowed

2. Allow rewriting of dealloc, but do not allow calls to [supper Dealloc]

[Email protected] 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 Ying

If there is a circular reference then one uses __weak another with __strong

4. The previous retain was replaced with strong

If the file does not previously support arc and requires temporary support for arc, you need to change the file operation in compile sources:

-F-OBJC-ARC Support Arc

-FNO-OBJC-ARC does not support arc

15) protocol

The agreement is used only to affirm the method

@protorcol MyProtocol

-(void) test;

-(void) test;

) block

1) to save a piece of code

2) Block's logo: ^

Example:

int sum (int a, int b) {

return a+b;

}

Int (*p) (int, int) = SUM;

Int (^sumblock) (int, int) = ^ (int a, int b) {

return a+b;

};

int c = Sumblock (10, 11);

Classification and memory management

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.