Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone objective-c初学者:getter setter prob和EXC_BAD_访问错误_Iphone_Objective C_Cocoa Touch_Getter Setter_Accessor - Fatal编程技术网

Iphone objective-c初学者:getter setter prob和EXC_BAD_访问错误

Iphone 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

我一直都有一个坏的EXC访问权限,我不知道为什么

简单任务:

解析器类在名为listArray的NSMutableArray中使用touchXML传递XML。 在方法中,我可以访问listArray,listArray.count工作得很好

现在我需要另一个类MasterViewController中的listArray.count。 但是我总是遇到一个EXC\u BAD\u访问错误。 请帮忙

以下是代码snipplet: 解析器.h

MasterViewController.h

#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];