Iam getting an EXC_BAD_ACCESS all the time and I cannot figure out why…
Simple task:
The Parser Class pases XML with touchXML in an NSMutableArray called listArray.
In the Method grabCountry I can access the listArray and listArray.count works well.
Now I need the listArray.count in another Class the MasterViewController.
But Im getting an EXC_BAD_ACCESS error all the time.
Please help!
Here is the code 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];
}
Can anyone explain me what the problem here is?
Thanks!
Internally, each
@propertyhas a corresponding instance variable.In your
-grabCountrymethod, you are directly accessing the instance variable in the statementlistArray = [[NSMutableArray alloc] init];(same withurl = [NSURL URLWithString: xmlPath];), instead of the@property‘s setter method, causing theNSMutableArraythat youalloc-init‘d to not be retained by the property. To invoke the@property‘s setter method, you should callIf you want to have Xcode show an error when you are directly accessing the instance variable of an
@property, you can have@synthesize listArray = _listArray, which changes the name of the instance variable to_listArray.Generally, if there is an
alloc-init, there must be a correspondingrelease(except if using Automatic Reference Counting).Also, in the
[listArray addObject:[xmlItem copy]];statement, the call tocopyis not needed, asNSArrays retain every object that is added to them. Callingcopyalso increases the retain count, which is another leak. Instead, you should just have[self.listArray addObject:xmlItem];You are getting EXC_BAD_ACCESS because in
NSLog(@"Amount %@",[theParser.listArray count]);, you are using%@format specifier, which is forNSStrings. You want to print the array’s count, an integer, so you should be using%dor%i.