2
votes
NSMutableArray *tblContents =  [NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];
[tblContents addObject:txtNewTableRow.text];

My app is crashing at line 2. Error message in console is 'NSInvalidArgumentException', reason: '-[NSCFString addObject:]: unrecognized selector sent to instance xxx

BTW, it works alright if i replace arraywithobjects initialization with alloc & init!

I am simply creating a mutable array and adding an object to it. Whats the problem in there?

Thanks Sridhar Reddy

Full code: .h:

#import <UIKit/UIKit.h>

@interface TableStarterViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *tblContents;

    IBOutlet UITextField *txtNewTableRow;
    IBOutlet UITableView *tblMain;
}

@property (nonatomic, retain) NSMutableArray *tblContents;
@property (nonatomic, retain) UITextField *txtNewTableRow;
@property (nonatomic, retain) UITableView *tblMain;

-(IBAction) addRowToTable;

@end

.m:

#import "TableStarterViewController.h"

@implementation TableStarterViewController

@synthesize tblContents;
@synthesize txtNewTableRow;
@synthesize tblMain;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tblContents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] 
                             initWithStyle:UITableViewCellStyleDefault 
                             reuseIdentifier:@"cell"];

    cell.textLabel.text = [tblContents objectAtIndex:indexPath.row];

    return cell;
}

-(IBAction) addRowToTable
{
    [tblContents addObject:txtNewTableRow.text];

    [tblMain reloadData];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    tblContents =  [NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];


    [super viewDidLoad];
}

Thats all code.

3
Is there more code between these two lines? Your error says that tblContents contains a string object, not a mutable array. - ughoavgfhw
This code as given is correct and should work. Something else is wrong, probably with memory management of something else in your method or object, although I admit that it's not obvious what could be happening in the space of these two lines. If the crash is 100% repeatable, it does imply that you're sending a string object the addObject message. In the debugger, try "po tblContents" at this line, and the XXX in your "instance XXX"-- is that the same address as tblContents? - Ben Zotto
I agree with quixoto's comment. You should try enabling NSZombie to debug that. cocoadev.com/index.pl?NSZombieEnabled - tia
Enabled zombies and this error pops up. -[__NSArrayM addObject:]: message sent to deallocated instance But I am never releasing tblContents object. Why is it getting deallocated! - Null Head

3 Answers

2
votes

Your are not retaining the array and it is being autoreleased before you are using it. Make sure to retain it after you get the pointer back from arrayWithObjects.

Also, it's not clear to me that arrayWithObjects will return a mutable array. Which, if not, will cause further problems.

Edit 1: Alloc and Init return an object with retain count 1; arrayWithObjects returns an object with retain count 0.

Edit 2: I pulled out Xcode and verified that [NSMutableArray arrayWithObjects:] return an NSMutableArray (or according to Xcode a __NSArrayM)

2
votes

instead of,

tblContents =  [NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];

try this,

tblContents =  [NSMutableArray initWithObjects: @"1", @"2", @"3", nil];
0
votes

As mentioned by aepryus, arrayWithObjects is inherited from NSArray and it might actually be returning NSArray. Hence the problem. Anyone knows how to init NSMutableArray with objects?