OC Language Madness Handout study notes

Source: Internet
Author: User
Tags define null shallow copy

1. The difference between nil and nil and null:

NIL:A null pointer to an Objective-c object. (#define NIL ((ID) 0)) nil is an object value.
NIL:A null pointer to an OBJECTIVE-C class.
NULL:A null pointer to anything else. (#define NULL ((void *) 0)) Null is a general-purpose pointer (generic pointer).
Nsnull:a class defines A Singleton object used to represent null values in collection objects (which don ' t allow nil Valu ES).
[NSNull NULL]: The singleton instance of NSNull.
[NSNull NULL] is an object that he uses in situations where nil can not be used.

2. Ways to avoid using zombie objects

To prevent accidentally invoking a zombie object, you can assign a value of nil to the object (null value of the object)
?

3. Memory leaks of objects





4. @property Parameters

Memory Management Related parameters:










5, @class the use of the role

You can simply refer to a class
# # # Simple to use
@class Dog; Introduction of Class
Just tell the compiler: Dog is a class; Does not contain all the contents of the dog class.

Specific use

Use @class in the. h file to refer to a class in a. m file using #import. h files that contain this class.

There are two ways to refer to a class generally:
One is introduced by means of #import, and the other is introduced by @class; The difference between the two approaches is that:

1) #import方式会包含被引用类的所有信息, including the variables and methods of the referenced class; @class just tell the compiler in the A.h file that B *b is just the declaration of the class, what information is in this class, there is no need to know, and so on when the implementation of the file is really used, To actually see the information in Class B;

2) using the @class method because only the name of the referenced class (Class B) can be used, and in the implementation class due to the use of the entity variables and methods in the referenced class, you need to use #import to contain the header file of the referenced class;

3) through the above 2 points is also easy to know in compiling efficiency, if there are hundreds of header files are #import the same file, or these files are #improt (A->b, b->c,c->d ...), once the initial header file slightly changed, followed by reference to this All the classes of files need to be recompiled again, so the efficiency is conceivable, and relatively speaking, the use of @class way will not appear this problem;

So: in our actual development, we try to use @class in. h Header Files

4) for cyclic dependencies, for example Class A refers to Class B, and Class B also refers to the Class A, Class B code:

The difference in function import contains all the information (content) of the reference class, including the variables and methods of the reference class @class just tell the compiler that there is a class, what information is in this class, the difference between the efficiency is completely unknown

If there are hundreds of header files are #import the same file, or these files are #import, then once the initial header file changes slightly, the subsequent reference to the file of all the classes will need to recompile again, compile efficiency is very low relatively speaking, This is not the case with the @class method.

6. Circular reference loop retain scene


For example, a object retain B object, B object retain the A object loop retain the disadvantage of this will cause a object and B object can never be released

Solutions for circulating retain

When both ends are referenced, one end should be retain, one end with assign

7. Memory management of NSString Class 1), NSString, etc. memory management of class in foundation framework

Let's take a look at some of the following:

NSString *teststr1 = @ "a";
NSString *TESTSTR2 = [NSString stringwithstring:@ "a"];
NSString *TESTSTR3 = [NSString stringwithformat:@ "B"];
NSString *TESTSTR4 = [[NSString alloc] initwithstring:@ "C"];
NSString *TESTSTR5 = [[NSString alloc] initwithformat:@ "D"];
NSString *TESTSTR6 = [[NSString alloc] init];

NSLog (@ "TestStr1->%p", TESTSTR1);
NSLog (@ "TestStr2->%p", TESTSTR2);
NSLog (@ "TestStr3->%p", TESTSTR3);
NSLog (@ "TestStr4->%p", TESTSTR4);
NSLog (@ "TESTSTR5->%p", TESTSTR5);
NSLog (@ "TESTSTR6->%p", TESTSTR6);

By comparing the address can be seen, from the above can be seen, TESTSTR1,TESTSTR2,TESTSTR4 are in a memory area, that is, the constant memory area,

1 ---> NSString *str  = [[NSString alloc] Initwithstring:@ "ABC" ]; 2 ---> str  = @ "123 "; 3 ---> [str  release]; 4 ---> NSLog (@ "%@" , str );  

First, let's analyze this piece of code first.
The first sentence declares an instance of the NSString type STR, and initializes it with a value of @ "ABC" after Init, and points the STR pointer to a constant @ "123". In theory, the "ABC" initialized at the first line does not have any pointers pointing. So it caused a memory leak.
Then the third line, the reference count of STR-1
The value of the four-row output STR is 123.
First answer why not crash, because the third line of release is actually release a constant @ "123" and as a constant, its default reference meter value is very large (100k+)
NSLog (@ "Retaincount =%tu", [@ "123" retaincount]);
The final output value will be a very large number. So a single release is not going to let it out.
Then answer this will not cause a memory leak.
In fact............ Theoretically speaking!
But in fact, OBJECTIVE-C has special care for the NSString type. The default initial values for all NSString reference counters are very, very large.

2), Dangerous usage
while ([a0) {         [a release];

If the results are correct, then what a lucky man!

8. Automatic release tank and autorelease introduction automatic release tank

(1) During the operation of the iOS program, countless pools are created that are present in the stack structure (advanced post-exit). (2) When an object calls Autorelease, the object is placed in the release pool at the top of the stack

How the auto-free pool is created

(1) How IOS 5.0 was created before

*pool`````````````````[pool release];//[pool drain];用于mac

(2) After iOS5.0

@autoreleasepool{//开始代表创建自动释放池       ·······}//结束代表销毁自动释放池
Autorelease

is a memory management method that supports reference counting
It can save an object temporarily, and then send the release message to each of the objects in the memory pool's own drain (drain) Note that this is just sending the release message, if the reference count at the time (reference-counted) is still not 0, the object will still not be released. You can use this method to save an object, as well as to release the object after it is saved.

Why do you have autorelease?

(1) no longer need to care about the time the object was released
(2) No need to worry about when to call release

When will the Autorelease be released?

For Autorelease pool itself, it will be released when the following two conditions occur
1) Manually release Autorelease pool
2) automatic release after Runloop end
For objects inside the Autorelease pool
Released when the reference count is retain = = 0. Both release and autorelease pool drain trigger the retain– event.

9. Block

Static variables and global variables in addition and without __block will refer directly to the variable address. It means you can fix it.
Change the value of the amount. Without the addition of the __block parameter.
? The global block and stack block are distinguished by whether the external variables are referenced, and the heap block is the stack block copy. For global block
Copy does not have any effect, and the return is still a global block.
Two, constant volume (NSString *a = @ "Hello"; A is a constant variable, @ "Hello" is a constant.) -No __block type block will reference the address of the constant (shallow copy). The __block type block will refer to constant variables (e.g. a variable, a = @ "abc"). You can modify the contents of a point. ) address.

OC Language Madness Handout study notes

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.