Objective c NSMutableArray丢失数据

Objective c NSMutableArray丢失数据,objective-c,nsmutablearray,Objective C,Nsmutablearray,这里已经有很多关于这个问题的帮助,但是没有一个实现是有效的。i、 e:创建非原子,使用self.myArray保留+使用字典而不是数组 我所做的是解析xml文档中的信息,过滤掉不需要的条目,并尝试动态存储该条目的信息 当我尝试将信息存储到两个可变数组中时,其中一个数组的信息在尝试访问我的“parser”方法之外的信息时丢失 一些背景代码。不是完整的代码。这也有字典 h m 我以完全相同的方式创建dataGroup和dataText,但只有dataText失去了它的值 非常感谢您的帮助,如果我的代

这里已经有很多关于这个问题的帮助,但是没有一个实现是有效的。i、 e:创建非原子,使用self.myArray保留+使用字典而不是数组

我所做的是解析xml文档中的信息,过滤掉不需要的条目,并尝试动态存储该条目的信息

当我尝试将信息存储到两个可变数组中时,其中一个数组的信息在尝试访问我的“parser”方法之外的信息时丢失

一些背景代码。不是完整的代码。这也有字典

h

m

我以完全相同的方式创建dataGroup和dataText,但只有dataText失去了它的值

非常感谢您的帮助,如果我的代码有任何部分不清楚,请告诉我

编辑:

找到了问题的根源

当我写入dataText数组时,我会将每个条目重写为最后一个要输入的条目。在我的测试用例中,最后一个条目是创建空数组的字符串@null

将在找到解决方案后返回

编辑2:


@RuslanSoldatenko注意到,在数组中设置对象后,我没有创建文本字符串的新实例。查看评论以获取帮助。

当试图访问信息时,您所说的迷路是什么意思?整个阵列是否已解除分配?是否只缺少一些对象?请在头文件中显示这些声明的更多上下文。它们在一个@interface声明中,对吗?而且,我看到了在哪里释放数组;您在哪里保留它们?另外:运行clang静态分析器的结果是什么?还有,为什么不使用ARC呢?关于ARC的好问题,因为在Xcode 5.x中,ARC是新项目的默认设置。
@interface WikiView : UIViewController {
    //scrollview
    UIScrollView *ScrollView;

    //init
    int isIpad;
    int orientation;
    int parseCount;

    //parse data constructs
    NSString *subplant;
    NSString *element;
    NSMutableString *text;
    NSString *oldElement;
    NSMutableDictionary *dataHolder;
    NSMutableArray *dataGroup;
    NSMutableArray *dataText;

}
@end
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    //inits
    dataGroup = [[NSMutableArray alloc] init];
    dataText = [[NSMutableArray alloc] init];
    dataHolder = [[NSMutableDictionary alloc] initWithCapacity:1];

    text = [[NSMutableString alloc] init];

    //parse the info 
    [self loadDataFromXML:xmlpath];

    //When I call the values for dataText here they are all null
    //also when called the objects for dataHolder are null as well

    //this outputs the correct array
    for (int i = 0; i<[dataGroup count]; i++) {
        NSLog(@"%@",dataGroup[i]);
    }
    //this outputs an array of null objects
    for (int i = 0; i<[dataText count]; i++) {
        NSLog(@"HI.....%@",dataText[i]);
    }
}

//parse function
//method to retrieve data
- (void)loadDataFromXML:(NSString *)xmlpath {
    //data is parsed
    NSData* data = [NSData dataWithContentsOfFile: xmlpath];
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];

    [parser setDelegate:self];

    [parser parse];

    [parser release];
}



//on found characters
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if ([subplant isEqualToString:plantid]) {

        NSString *s2 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if (![oldElement isEqualToString:element]) {

            if (oldElement != nil) {
                if (parseCount >= 1) {

                    //Here I store the values into the proper places

                    NSLog(@"%@: %@",oldElement,text);
                    [dataGroup addObject:oldElement];
                    [dataText addObject:text];
                    [dataHolder setObject:text forKey:oldElement];

                    //The values are correct here
                }
                parseCount++;
            }

            //if (new tag) reset string
            [text setString:s2];
        }
        else{
            //if not new tag append string (takes care of &apos;)
            [text appendString:s2];
        }

        oldElement = element;
    }
}

//on did start element
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    //accessing tags of this element
    if ([elementName isEqualToString:@"plant"]) {
        subplant = [attributeDict valueForKey:@"plant"];
    }
    element = elementName;
}

-(void)dealloc{
    [super dealloc];
    [text release]; [dataGroup release]; [dataText release]; [dataHolder release];
}