0

私は常に EXC_BAD_ACCESS を取得していますが、その理由がわかりません...

簡単なタスク:

パーサー クラスは、listArray と呼ばれる NSMutableArray で touchXML を使用して XML を渡します。メソッドgrabCountryでは、listArrayにアクセスでき、listArray.countはうまく機能します。

ここで、MasterViewController という別のクラスに listArray.count が必要です。しかし、常に EXC_BAD_ACCESS エラーが発生します。助けてください!

コード スニペットは次のとおりです。 Parser.h

#import <Foundation/Foundation.h>

@interface Parser : NSObject

@property (strong, retain) NSMutableArray *listArray;
@property (strong, retain) NSURL *url;

-(void) grabCountry:(NSString *)xmlPath;
@end

パーサー.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];
}

ここで何が問題なのか誰か説明してもらえますか? ありがとう!

4

1 に答える 1

1

内部的には、それぞれ@propertyに対応するインスタンス変数があります。

メソッドでは、のセッター メソッドではなく-grabCountry、ステートメント内のインスタンス変数に直接アクセスしているためlistArray = [[NSMutableArray alloc] init];( と同じ)、プロパティによって保持されません。のセッター メソッドを呼び出すには、url = [NSURL URLWithString: xmlPath];@propertyNSMutableArrayalloc-init@property

NSMutableArray *temp = [[NSMutableArray alloc] init];
self.listArray = temp; // or [self setListArray:temp];
[temp release];

のインスタンス変数に直接アクセスしているときに Xcode にエラーを表示させたい場合は、インスタンス変数の名前を に変更する@propertyを使用できます。@synthesize listArray = _listArray_listArray

通常、 が存在する場合はalloc-init、対応する が存在する必要がありますrelease(自動参照カウントを使用する場合を除く)。


また、ステートメントでは、 s はそれらに追加されたすべてのオブジェクトを保持するため、[listArray addObject:[xmlItem copy]];への呼び出しcopyは必要ありません。NSArray呼び出すcopyと保持カウントも増加しますが、これは別のリークです。代わりに、あなたはただ持っているべきです[self.listArray addObject:xmlItem];


では、s 用のフォーマット指定子NSLog(@"Amount %@",[theParser.listArray count]);を使用しているため、EXC_BAD_ACCESS を取得しています。配列のカウント、整数を出力したいので、orを使用する必要があります。%@NSString%d%i

于 2012-04-06T00:12:50.833 に答える