-[__nscfarray Insertobject:atindex:]: Mutating method sent to immutable Obje

Source: Internet
Author: User

Wrote a method to save the search keyword, unexpectedly appeared title said Crash, from the semantics of this is probably the case: to a non-mutable object sent Mutable method

Here's how:

+ (void) Setusersearchdynamickeyword: (NSString *) Strkeyword
{
    Nsmutablearray * searcharray = [[Nsuserdefaults Standarduserdefaults] Objectforkey:str_search_dynamic_array];
    if (Searcharray = = nil)
    {
        Searcharray = [Nsmutablearray array];
    }

    [Searcharray Addobject:strkeyword];
    
    [[Nsuserdefaults Standarduserdefaults] Setobject:searcharray Forkey:str_search_dynamic_array];
    [[Nsuserdefaults standarduserdefaults] synchronize];
}

Can be sure that Searcharray is definitely nsmutablearray, then why still say to Nsmutablearray is not a mutable object it.

Online is to say that the objects returned from Nsuserdefault are fixed and cannot be modified. The specific reason is unknown, first recorded in this, and left behind to study the study.


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.