简体   繁体   中英

How to get a String out of an NSMutableArray in Objective C

So I have been stuck on this for a couple of hours and I can't figure out why I am unable to fix this. I am simply trying to get a string value out of a NSMutableArray but it comes back with an error " program received signal: "EXC_BAD_ACCESS". What is really frustrating to me is I can successfully get an object out right after I add an object to the array, however when I try and call it in my buttonClicked method later, it gives me the error. Can anyone see what I am doing wrong? I get the error the first time I try and get something out of the Array, where it says "NSMutableString *tempID = [buttonIDArray objectAtIndex:tempButtonTag];"

- buttonClicked:(id)sender
{

UIButton *selectedButton = (UIButton *)sender;
int tempButtonTag = selectedButton.tag;
NSLog(@"TempbuttonTag is %d", tempButtonTag);
Map *map =[[Map alloc] initWithNibName:nil bundle:nil];
NSMutableString *tempID = [buttonIDArray objectAtIndex:tempButtonTag];
NSMutableString *tempType = [buttonTypeArray objectAtIndex:tempButtonTag];

[map setXmlID:tempID];
[map setXmlType:tempType];
buttonIDArray = nil;
buttonTypeArray = nil;

[self presentModalViewController:map animated:YES];

}

. Here is the part that I add objects (strings) to the array. When I test it here it gives me the correct value out of the Array...but later I get the error.

if ( [elementName isEqualToString:@"Button"] ) {

    buttonViewXvalue = [tempXCorrVariable floatValue];
    buttonViewYvalue = [tempYCorrVariable floatValue];
    buttonViewWidth = [tempWidthCorrVariable floatValue];
    buttonViewLength = [tempLengthCorrVariable floatValue]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button setTag:buttonTag];        
    button.frame = CGRectMake(buttonViewXvalue, buttonViewYvalue, buttonViewLength, buttonViewWidth);
    NSLog(@"xmlID is %@", xmlID);
    NSLog(@"xmlType is %@", xmlType);
    [buttonIDArray addObject:xmlID];
    [buttonTypeArray addObject:xmlType];
    NSMutableString *test = [buttonIDArray objectAtIndex:buttonTag];
    NSLog(@"Test is %@", test);

    buttonTag++;


    [self.view addSubview:button];

    button = nil;
    currentStringValue = nil;

    return;

}

Make sure to retain buttonIDArray, buttonTypeArray arrays. I think what happens is that you construct them correctly, but don't retain them, so they get freed before you click a button.

Edit: and there's likely a memory leak when you do buttonIDArray = nil in the event handler. Unless you save the reference to the buttonIDArray elsewhere, the array won't get released and will be leaked (and same thing for buttonTypeArray). Either release them before assigning nil, or create retaining properties for both arrays and do self .buttonIDArray = nil to release the array and assign the ivar to nil. And don't forget to release the arrays in the dealloc method of course.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; returns a autoreleased object. In that case button = nil; is not needed.

Actually there are a few memory issues in your code. Read Memory Management Programming Guide carefully.

You might be using an autorelease instnace of NSMutable array. Which has de-allocated when you try to access it.

The solution is:

You can try to make buttonIDArray as instance variable and create a retain property like this:

@interface ClassName
{
    NSMutableArray *buttonIDArray;
   .........
}
@property (nonatomic, retain) NSMutableArray *buttonIDArray;
@end;

and access the variable like this.

NSMutableArray *array =  [NSMutableArray alloc] init]; //chnage it to whatever way you want to init
self.buttonIDArray = array;
[array release];

Don't forget to do

self.buttonIDArray = nil;

in dealloc

Most probably to do with xmlID and xmlType being released when you return from that function where you add them. Try this:

[buttonIDArray addObject:[xmlID retain]];
[buttonTypeArray addObject:[xmlType retain]];

Don't forget to release them though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM