About delegate in IOS, iosdelegate

Source: Internet
Author: User
Tags xml parser

About delegate in IOS, iosdelegate

Soon after you start writing iOS programs, you should start to face a lot of delegate,
Whether using others' libraries or writing their own libraries, you may not be able to escape the delegate.
For fear that some people do not know what delegate is, let's give a brief introduction here,
Delegate is called a delegate. It is usually used within the class to delegate some events to others.
For example, the XML Parser may know how to parse xml, but the parse may not know how to process the xml parser.
Therefore, NSXMLParser provides an NSXMLParserDelegate for client implementation,
When parse arrives at an element, it calls the message defined by callback delegate,
Let the client decide how to process the element.
Okay, I admit that my explanation is vague, but I didn't want you to understand what delegate is,
But for the use or design of delegate, you may need to pay attention to things.

When designing delegate in our class, we usually have several precautions.
If my class is called MyClass, we may define a MyClassDelegate protocol as my delegate protocol.
This may be the case in MyClass.
@ Protocol MyClassDelegate <NSObject>
-(Void) myClassOnSomeEvent :( MyClass *) myClass;
@ End

@ Interface MyClass
{
Id <MyClassDelegate> _ delegate;
}
@ Property (nonatomic, assign) delegate;
@ End
In the code above, we noticed that delegate is defined as @ property (assign ).
Why do we use assign instead of retain?
The reason is that in the iOS reference counting environment, we must solve the circular count problem.
Let's write about how we usually use delegate. The code below should be familiar to everyone.
-(Void) someAction
{
MyClass = [MyClass new];
MyClass. delegate = self;
....
}
Circular reference will appear soon.
Assume that the above code is written in an object of myViewController,
Once the reference count of myViewController becomes 1,
The two myViewController and myClass brothers only have to retain each other, which becomes an isolated island, thus causing memory leak !!!
 

In this way, we recommend that you use assign property for all the delegate files.
That is, the property of the "weak reference". Its characteristic is that although it will hold the reference of the other party, it will not increase the retain count.
In this case, when the retain count of myViewController becomes 0, dealloc will be performed.
In dealloc, The myClass is also release, and the myClass is also release.
-(Void) dealloc
{
[MyClass release];
[Super dealloc];
}

 

Is it over? Not found...
There is another key point that everyone often forgets, that is, the dealloc above may pose a potential danger.
This should be the case.
-(Void) dealloc
{
MyClass. delegate = nil;
[MyClass release];
[Super dealloc];
}
You may be wondering, isn't myClass immediately release? Why should we set his delegate to nil first?
This is because we assume that myClass will be dealloc immediately, but this is not necessarily true,
It is possible that there is an NSURLConnection in it, or you are doing something to make other objects retain myClass.
If myClass is not dealloc immediately, isn't myClass. delegate pointing to an invalid position? (This pointer is called dangling pointer)

 

Solution: In dealloc of MyViewController, before release myClass,
You must change the original delegate to nil to avoid crash.
Most of the crash projects I wrote earlier are caused by this, because this problem does not happen every time,
However, it is difficult to re-copy the data when it occurs, so do not be careful.

 

However, we are excited that the Automatic Reference Counting problem in iOS5 can be improved.
A New weak reference concept was proposed in ARC to replace the original assign,
If the weak reference indicates that the object has been dealloc because the retain count is zero, the weak reference is automatically set to nil.
The old assign method is called _ unsafe_unretained in ARC to be compatible with versions earlier than iOS4.

Review highlights:
If you write the library for use by others, remember to set your delegate to assign property so that it will not cause circular reference.
When you start using others' libraries, remember to set delegate to nil when you dealloc it yourself to avoid crash.

References
[1] Communicating with Objects


Objective-c's delegate usage # import "AppDelegate. h"

# Import "ViewController. h"

AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
AppDelegate. window. rootViewController = (UIViewController *) self. detailViewController;
UIViewController * viewCtrl = (ViewController *) appDelegate. window. rootViewController;
Nsstring * url = viewCtrl. theurl. text;

On the Code effective condition ViewControllerwindow Root View Controller knows clearly what I mean
Ask again.
If the ios delegate attribute is not set to assignarc under ARC, the pointer will be released after weak is used.

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.