简体   繁体   English

如何从目标C的NSMutableArray中获取字符串

[英]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];" 我只是想从NSMutableArray中获取一个字符串值,但是它返回一个错误“程序收到信号:“ EXC_BAD_ACCESS”。真正让我感到沮丧的是,我可以在向对象添加对象之后就成功获取对象数组,但是当我稍后尝试在我的buttonClicked方法中调用它时,它给了我错误。有人可以看到我在做什么吗?第一次尝试从数组中获取某些东西时,我会收到该错误。说:“ 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. 确保保留buttonIDArray,buttonTypeArray数组。 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. 编辑:在事件处理程序中执行buttonIDArray = nil时,可能会发生内存泄漏。 Unless you save the reference to the buttonIDArray elsewhere, the array won't get released and will be leaked (and same thing for buttonTypeArray). 除非您将对buttonIDArray的引用保存到其他位置,否则该数组将不会被释放并且会泄漏(对于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. 要么在分配nil之前释放它们,要么创建两个数组的保留属性,并执行self .buttonIDArray = nil释放该数组并将ivar分配给nil。 And don't forget to release the arrays in the dealloc method of course. 而且,当然不要忘记使用dealloc方法释放数组。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; returns a autoreleased object. 返回一个自动释放的对象。 In that case button = nil; 在这种情况下, 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. 您可能正在使用NSMutable数组的自动释放实例。 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: 您可以尝试将buttonIDArray设置为实例变量,并创建如下的keep属性:

@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. 当您从添加它们的函数返回时,很可能与xmlID和xmlType被释放有关。 Try this: 尝试这个:

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

Don't forget to release them though 别忘了释放它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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