I am basically having an issue with variable scope. Knowing multiple programming languages, I just can’t wrap my head around obj-c sometimes.
In my webviewcontroller class I have a variable that I call using self.var
I also have some delegate methods that reference a modified UIAlertView. Why can’t I reference this self.var in those methods without the entire app crashing?
I have tried everything and its been a few days and now I need help!
Thanks!
Edit:
Here is the code webviewcontroller.h
#import "SBTableAlert.h"
@interface WebViewController : UIViewController <SBTableAlertDelegate, SBTableAlertDataSource>{
NSMutableArray *bookmarks;
}
@property (nonatomic, retain) NSMutableArray *bookmarks;
here is the .m (yes I have an @synthesize for bookmarks), this is in view did load
NSMutableArray *tmpArray = [tmpDict objectForKey:@"myKey"];
self.bookmarks = tmpArray;
[tmpDict release];
[tmpArray release];
SBTableAlert *bookmarkAlert;
bookmarkAlert = [[SBTableAlert alloc] initWithTitle:@"Jump to:" cancelButtonTitle:@"Back" messageFormat:nil];
[bookmarkAlert.view setTag:1];
[bookmarkAlert setDelegate:self];
[bookmarkAlert setDataSource:self];
NSString *settingsicon = [[NSBundle mainBundle] pathForResource:@"gear" ofType:@"png"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:settingsicon] style:UIBarButtonItemStylePlain target:bookmarkAlert action:@selector(show)];
and later I try to call self.bookmarks
- (NSInteger)tableAlert:(SBTableAlert *)tableAlert numberOfRowsInSection:(NSInteger)section {
return [self.bookmarks count];
}
The error I get at the self.bookmarks count is NSInvalidArgumentException
It’s a bit hard to answer without seeing some of your code.
Like Farmer_Joe asked, how are you defining the property? Something like:
is different from
How are you assigning to the var?
won’t kick in the retain, but this would (if the property is defined as “retain”):
Edit: based on the new code you posted, it looks like you’re over-releasing tmpArray. Remove this line:
You shouldn’t have to release tmpArray, because this line doesn’t retain it: