开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜