ios-Nsnotificationcenter Removeobserver need to call after opening arc

Source: Internet
Author: User

When arc is turned on, does the Nsnotificationcenter removeobserver need to be called and when is it called?

Today in StackOverflow see a question, arc case whether need to call removeobserver, oneself think of, really is a problem, studied a bit.

On the code:

-(void) dealloc {    [[nsnotificationcenter Defaultcenter]   removeobserver:self];}

Originally thought in the Arc Dealloc Way is already abandoned, but the fact is that it still exists, but not as in the MRC also need to call [super dealloc] , in the ARC environment, this method is not available, otherwise the compiler directly error.
Then I thought the dealloc way would normally be called?

Here's how to add an observer:

[[Nsnotificationcenter defaultcenter] addobserver:self                                                     selector: @selector (method:)                                                         Name:@ "  postkey"                                                       object: nil";

The first parameter of this method observer is not retain operation, so it does not form a circular reference, but is it necessary to call Removeobserver?

The answer is of course: it is necessary!

If this object is released, when the post message is called, it will be reported that the object is released error, resulting in a flashback, so in addition to the observer's object, it must be destroyed when it is deleted from the message center!

ios-Nsnotificationcenter Removeobserver need to call after opening arc

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.