Iphone objective-c初学者:getter setter prob和EXC_BAD_访问错误
我一直都有一个坏的EXC访问权限,我不知道为什么 简单任务: 解析器类在名为listArray的NSMutableArray中使用touchXML传递XML。 在方法中,我可以访问listArray,listArray.count工作得很好 现在我需要另一个类MasterViewController中的listArray.count。 但是我总是遇到一个EXC\u BAD\u访问错误。 请帮忙 以下是代码snipplet: 解析器.h MasterViewController.hIphone objective-c初学者:getter setter prob和EXC_BAD_访问错误,iphone,objective-c,cocoa-touch,getter-setter,accessor,Iphone,Objective C,Cocoa Touch,Getter Setter,Accessor,我一直都有一个坏的EXC访问权限,我不知道为什么 简单任务: 解析器类在名为listArray的NSMutableArray中使用touchXML传递XML。 在方法中,我可以访问listArray,listArray.count工作得很好 现在我需要另一个类MasterViewController中的listArray.count。 但是我总是遇到一个EXC\u BAD\u访问错误。 请帮忙 以下是代码snipplet: 解析器.h MasterViewController.h #import
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TouchXML.h"
#import "Parser.h"
@class Parser;
@interface MasterViewController : UITableViewController{
Parser *theParser;
}
@end
谁能解释一下这里的问题是什么?
谢谢 在内部,每个
@属性都有一个对应的实例变量
在-grabCountry
方法中,直接访问语句listArray=[[NSMutableArray alloc]init]中的实例变量
(与url=[NSURL URLWithString:xmlPath]相同;
),而不是@property
的setter方法,这会导致NSMutableArray
属性不会保留您的alloc init
'd。要调用@property
的setter方法,应该调用
NSMutableArray *temp = [[NSMutableArray alloc] init];
self.listArray = temp; // or [self setListArray:temp];
[temp release];
如果要让Xcode在直接访问@属性的实例变量时显示错误
,可以使用@synthesis listary=\u listary
,将实例变量的名称更改为\u listary
通常,如果存在alloc init
,则必须有相应的释放
(除非使用自动引用计数)
另外,在[listArray addObject:[xmlItem copy]]中代码>语句,不需要调用copy
,因为NSArray
s保留添加到它们的每个对象。调用copy
也会增加保留计数,这是另一个泄漏。相反,您应该只有[self.listArray addObject:xmlItem]代码>
您正在获得EXC_BAD_访问权限,因为在NSLog(@“Amount%@,[theParser.listary count])
,您正在使用%@
格式说明符,该说明符用于NSString
s。您想打印数组的计数,一个整数,因此应该使用%d
或%i
谢谢您的回复。我改变了一切,听起来似乎很合理:)但是当我试图访问它时,我得到了相同的错误:[theParser.listary count](来自MasterViewController类)最终发现了问题。。。打印计数(整数)时使用的是%@
,此时应使用%d
或%i
。我通常也会被这件事抓住我编辑了我的答案以反映这一点。
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TouchXML.h"
#import "Parser.h"
@class Parser;
@interface MasterViewController : UITableViewController{
Parser *theParser;
}
@end
- (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];
}
NSMutableArray *temp = [[NSMutableArray alloc] init];
self.listArray = temp; // or [self setListArray:temp];
[temp release];