开发者

Xcode warning: "NSArray may not respond to -addObject"

开发者 https://www.devze.com 2022-12-16 16:12 出处:网络
In my header file, I have this code: @interface TableViewController : UIViewController { IBOutlet UITableView *tblListData;

In my header file, I have this code:

@interface TableViewController : UIViewController 
{
    IBOutlet UITableView *tblListData;
    NSArray *arryData;
}

In my class declaration file, I have this implementation:

- (void)viewDidLoad 
{
    arryData = [[NSArray alloc] initWithObjects:@"iPhone",@"iPod",@"MacBook",nil];
    [super viewDidLoad];
}

Now I get this Xcode warning: NSArray may not respond to -addObject for the following code:

- (IBAction)AddButtonAction:(id)sender
{
    [arryData addObject:@"Mac Mini"];
    [tblListData reloadData开发者_如何学Go];
}

And, sure enough, my NSArray is not responding to addObject! :(

What should I do?


If you look up the docs, you'll see that NSArray is actually an immutable array (i.e. it can not be modified). That's why the -addObject: message is not implemented. Instead, you will need to use NSMutableArray.

0

精彩评论

暂无评论...
验证码 换一张
取 消