荫得到一个EXC_BAD_ACCESS所有的时间,我想不通为什么...Objective-C的初学者:吸气二传手概率和EXC_BAD_ACCESS错误
简单的任务:
解析类pases XML与touchXML在NSMutableArray调用listArray。 在方法grabCountry我可以访问listArray和listArray.count效果很好。
现在我需要在另一个类MasterViewController中的listArray.count。 但我一直得到一个EXC_BAD_ACCESS错误。 请帮忙!
下面是代码snipplet: Parser.h
#import <Foundation/Foundation.h>
@interface Parser : NSObject
@property (strong, retain) NSMutableArray *listArray;
@property (strong, retain) NSURL *url;
-(void) grabCountry:(NSString *)xmlPath;
@end
Parser.m
#import "Parser.h"
#import "TouchXML.h"
@implementation Parser
@synthesize listArray;
@synthesize url;
-(void) grabCountry:(NSString *)xmlPath {
// Initialize the List MutableArray that we declared in the header
listArray = [[NSMutableArray alloc] init];
// Convert the supplied URL string into a usable URL object
url = [NSURL URLWithString: xmlPath];
//XML stuff deleted
// Add the blogItem to the global blogEntries Array so that the view can access it.
[listArray addObject:[xmlItem copy]];
//works fine
NSLog(@"Amount: %i",listArray.count);
}
@end
MasterViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TouchXML.h"
#import "Parser.h"
@class Parser;
@interface MasterViewController : UITableViewController{
Parser *theParser;
}
@end
MasterViewControlelr.m
- (void)viewDidLoad
{
NSString *xmlPath = @"http://url/to/xml.xml";
theParser = [[Parser alloc] init];
//Starts the parser
[theParser grabCountry:xmlPath];
//Here I want to access the Array count, but getting an BAD ACCESS error
NSLog(@"Amount %@",[theParser.listArray count]);
[super viewDidLoad];
}
任何人都可以解释我这里的问题是什么? 谢谢!
感谢您的回复。我改变了一切,这听起来似乎对我来说:)但是,当Iam试图访问它时,我得到了同样的错误:[theParser.listArray count](从MasterViewController类) – Nico 2012-04-06 00:17:55
最后发现问题...您正在使用'%@ '当打印count(一个整数)时,你应该使用'%d'或'%i'。我通常也会被抓住。 =)我编辑了我的答案以反映这一点。 – neilvillareal 2012-04-06 00:31:42
%我工作...复制和粘贴错误: - /你做了我的一天! – Nico 2012-04-06 00:33:31