对 NSMutableArray 进行操作时的 EXC_BAD_ACCESS
我目前正在尝试连接到网络服务器,并从中使用一个表。我已将表放入 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)在
viewDidLoad
中添加:2)在
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)中qName
3) 之后,所有解析的对象将存储在
DataSet
中1) In
viewDidLoad
add:2) In
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
3) After that in
DataSet
all parsed objects will be stored