Add a string to the NSMutable Array

Source: Internet
Author: User

Problem description: A list application is created. You only need to press the button to add the string to the mutable array. However, after the code is run, click the button and only the last array is successfully added. [Plain]-(IBAction) notebutton :( UIButton *) sender {NSMutableArray * mystr = [[NSMutableArray alloc] init]; NSString * name = _ noteField. text; [mystr addObject: name]; [self. tableView reloadData];} solution: this is because every time you click this button, the NSMutableArray object [plain]-(IBAction) notebutton (UIButton *) will be re-created *) sender {NSMutableArray * mystr = [[NSMutableArray alloc] init]; how can this problem be solved? You only need to put * mystr declaration in the header file and define it as an attribute or private variable. For example, [plain] @ interface yourClass: NSObject {NSMutableArray * mystr;} @ end in. initialize NSMutableArray [plain] @ implementation yourClass-(id) init {if (self = [super init]) {mystr = [[NSMutableArray alloc] initWithCapacity: 0] autorelease] ;}}@ end after completing these two steps, you can directly go to-(IBAction) notebutton :( UIButton *) sender {NSString * name = _ noteField. text; [mystr addObject: name]; [self. tableView reloadData];}

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.