对 NSMutableArray 进行操作时的 EXC_BAD_ACCESS

发布于 2024-12-27 04:13:05 字数 2270 浏览 4 评论 0原文

我目前正在尝试连接到网络服务器,并从中使用一个表。我已将表放入 NSMutable 数组(它是全局的)中,并将其命名为 DataSet(我让这部分工作)。但是,当我尝试对其执行任何类型的操作(例如:计数)时,它会给我一个 EXC_BAD_ACCESS。顺便说一句,我使用带有 ARC 的 xcode 4.2,所以这不是内存管理问题。有什么建议吗?

- (void)viewDidLoad
{

    NSURL *url = [NSURL URLWithString: @"`url.com`"];
    NSData *jsonData =[NSData dataWithContentsOfURL:url];

    if(jsonData)

    {
        xmlParser = [[NSXMLParser alloc] initWithData: jsonData];
        [xmlParser setDelegate: self];
        [xmlParser parse];
    }
    NSLog(@"This is Number of elements: %@",[DataSet count]); ///<----EXC_BAD_ACCESS

 ..... //// default stuff//.....
    }



-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{


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

        /// puts the data into Array luckyNumbers
        NSError *error;
        SBJsonParser *json = [[SBJsonParser new] init];
        DataSet = [[NSMutableArray alloc] init];
        DataSet = [json objectWithString:soapResults error:&error];

      ///  NSLog(@"%@",DataSet); /// this works

    }
}

我不确定我是否应该这样做,但是我读到我应该使用诊断,所以我打开了 NSZombie 对象、Malloc 防护和 Malloc Stack。 他们说我应该将其输入 gdb 控制台 info malloc-history (memorylocation)

但是我不知道在哪里获取问题的内存位置。我将不胜感激任何帮助。

我无法让它工作,所以我尝试使用 NSDictionary 而不是 NSArray ,令人惊讶的是它工作了!这是我更新的代码,供任何想知道我做了什么的人使用。

  • (void)viewDidLoad

{

NSURL *url = [NSURL URLWithString: @"url.com"];
NSData *jsonData =[NSData dataWithContentsOfURL:url];
DataSet = [[NSDictionary alloc] init];

if(jsonData) {

    xmlParser = [[NSXMLParser alloc] initWithData: jsonData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}
NSLog(@"%@",DataSet.allKeys);

///// 默认内容 /// } -

(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 命名空间URI:(NSString *)namespaceURI QualifiedName:(NSString *)qName

{

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


    //NSLog(@"%@",DataSet);
    DataSet = [soapResults JSONValue];
    NSLog(@"%@",DataSet);

}

}

I currently trying to connect to a Webserver, and consuming a table from it. I have put the table into a NSMutable array (it's global), named it DataSet (I got this part working). However When I try to preform any type of operation on it (Example: count), it will give me a EXC_BAD_ACCESS. BTW I'm using xcode 4.2 with ARC, so it's not a memory management issue. Any suggestions?

- (void)viewDidLoad
{

    NSURL *url = [NSURL URLWithString: @"`url.com`"];
    NSData *jsonData =[NSData dataWithContentsOfURL:url];

    if(jsonData)

    {
        xmlParser = [[NSXMLParser alloc] initWithData: jsonData];
        [xmlParser setDelegate: self];
        [xmlParser parse];
    }
    NSLog(@"This is Number of elements: %@",[DataSet count]); ///<----EXC_BAD_ACCESS

 ..... //// default stuff//.....
    }



-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{


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

        /// puts the data into Array luckyNumbers
        NSError *error;
        SBJsonParser *json = [[SBJsonParser new] init];
        DataSet = [[NSMutableArray alloc] init];
        DataSet = [json objectWithString:soapResults error:&error];

      ///  NSLog(@"%@",DataSet); /// this works

    }
}

I'm not sure if I'm suppose to but, I read that I should use Diagnostics, so I turned on NSZombie objects, Malloc guard, and Malloc Stack.
They said I should type this into the gdb console
info malloc-history (memorylocation)

However I don't know where to get the memory location of the problem. I would appreciate any help.

I couldn't get this to work, so I tried using and NSDictionary instead of NSArray and surprisingly it work! Here is my updated code for anyone who was wondering what I did.

  • (void)viewDidLoad

{

NSURL *url = [NSURL URLWithString: @"url.com"];
NSData *jsonData =[NSData dataWithContentsOfURL:url];
DataSet = [[NSDictionary alloc] init];

if(jsonData)
{

    xmlParser = [[NSXMLParser alloc] initWithData: jsonData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}
NSLog(@"%@",DataSet.allKeys);

///// default stuff ///
}
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

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


    //NSLog(@"%@",DataSet);
    DataSet = [soapResults JSONValue];
    NSLog(@"%@",DataSet);

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2025-01-03 04:13:05

1)在viewDidLoad中添加:

DataSet = [[NSMutableArray alloc] init];

2)在-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)中qName

if ([elementName isEqualToString:@"string"])
{
    NSError *error;
    SBJsonParser *json = [[SBJsonParser new] init];
    [DataSet addObject:[json objectWithString:soapResults error:&error]];
    [json release];
}

3) 之后,所有解析的对象将存储在DataSet

1) In viewDidLoad add:

DataSet = [[NSMutableArray alloc] init];

2) In -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

if ([elementName isEqualToString:@"string"])
{
    NSError *error;
    SBJsonParser *json = [[SBJsonParser new] init];
    [DataSet addObject:[json objectWithString:soapResults error:&error]];
    [json release];
}

3) After that in DataSet all parsed objects will be stored

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文