'-[__nscfarray Insertobject:atindex:]: Mutating method sent to immutable object ' _ Crash tip

Source: Internet
Author: User
Implementation requirements for the most recent browsing features:

There is such a requirement in the project, and the user pushed into the homepage to go to the next page to browse the information, and then to the "my" page has a recently browsed feature, showing the most recently browsed data, up to 3. Ideas:

1, in the Browse details page record ID array, stored in the Nsuserdefault;
1.1, first take out the data in the Nsuserdefault to determine whether it is empty;
1.2, empty, then the description of the previous data, then initialize the array, add data, save;
1.3, is not empty, then the data before the description, to determine whether the word browsing ID in the array;
If in, then delete the duplicate element first, if not, do not do operation;
1.4, to determine whether the array count is greater than or equal to 3;
If greater than or equal to 3, then delete the first element, if not, do not do the operation;
2, in my page, take out the array from the Nsuserdefault, request the network, get the data display; question:

When I store an array of IDs, the first store is correct, but the second time, the page crashed, I really do not understand AH. Error Tip:

'-[__nscfarray Insertobject:atindex:]: Mutating method sent to immutable object ' Error reason:

means to change the method sent to a constant object (do not understand). To modify the method:

Then declare a variable array of 2 and manipulate the variable array 2 data .... Modified code:

#pragma mark-Recent browse-(void) Nearlylookcarwithmodel: (Detailchoosecarmodel *) Detailmodel {//remove array Nsmutablearray *ar
    Ray = [[Nsmutablearray alloc] init];

    Array = [[Nsuserdefaults standarduserdefaults] objectforkey:mylookcar];

        if (array) {NSLog (@ "not nil");
        The presence of this array nsmutablearray *marr = [[Nsmutablearray alloc] initwitharray:array];

        NSLog (@ "Last marr:%@", MARR);
                                Saved into a dictionary, including CID, phono, title nsdictionary *cardic = [nsdictionary dictionarywithobjectsandkeys:self.cid,@ "CID", detailmodel.main_photo,@ "Imgname", detailmodel.car_subject,@ "Ti

        Tle ", nil];

            if ([MArr containsobject:cardic]) {NSLog (@ "contains duplicate elements");

        Delete duplicate elements [mArr removeobject:cardic];

            } if ([MArr Count] >= 3) {NSLog (@ ">3");

        Deletes the first element [MARR removeobjectatindex:0];
} [MArr Addobject:cardic];        NSLog (@ "This marr:%@", MARR);

    Operation Array [[Nsuserdefaults standarduserdefaults] Setobject:marr Forkey:mylookcar];

        }else {NSLog (@ "nil");

        Initialize MARR nsmutablearray *marr = [[Nsmutablearray alloc] init];
                                Saved into a dictionary, including CID, phono, title nsdictionary *cardic = [nsdictionary dictionarywithobjectsandkeys:self.cid,@ "CID", detailmodel.main_photo,@ "Imgname", detailmodel.car_subject,@ "Ti

        Tle ", nil];
        [MArr Addobject:cardic];
    [[Nsuserdefaults Standarduserdefaults] Setobject:marr Forkey:mylookcar];
 }

}

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.