2
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.

4
  • Is there more code between these two lines? Your error says that tblContents contains a string object, not a mutable array. Commented Dec 19, 2010 at 0:46
  • 2
    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? Commented Dec 19, 2010 at 2:27
  • 1
    I agree with quixoto's comment. You should try enabling NSZombie to debug that. cocoadev.com/index.pl?NSZombieEnabled Commented Dec 19, 2010 at 7:57
  • 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! Commented Dec 22, 2010 at 1:34

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

7 Comments

Well, to keep things simple (in one line, for that) I gave that code. But actually I defined instance var in .h file. Made it a property, synthesized it and all the while, it was NSMutableArray. But as you said, there might be problems with arrayWithObjects.
He is trying to access tblContents and it is showing up as a NSCFString. The only time I've seen that is when an object gets released and the memory space gets reused for a different type of object. Obviously, we don't have a lot of code. But, some how the memory is getting released and reinitialized.
As far as the data type of arrayWithObjects, the documentation simply says it returns NSArray; if it does in fact return NSMutableArray, fine, but as I said, it's not clear to me what arrayWithObjects returns.
Here is the documentation for NSMutableArray: developer.apple.com/library/mac/#documentation/Cocoa/Reference/… . There is no arrayWithObjects method. It is however on NSArray and lists a return type of NSArray.
If tblContents is defined in the header it's funny you're not getting a warning about redeclaring it in that code. You know, 'local declaration hides instance variable' or whatever.
|
2

instead of,

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

try this,

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

Comments

0

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?

2 Comments

When called on the NSMutableArray class, it will return an NSMutableArray. Even if it returned an NSArray, he would not get that error.
I verified that [NSMutableArray arrayWithObjects:] does in fact return a NSMutableArray, so you are ok with that. The problem is that your Array is being released before you are using it somehow. Double check that the tblContents object isn't being released somewhere without being retained first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.