Alienz
Alienz

Reputation: 118

Issues with variable scope I am sure

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

Upvotes: 0

Views: 184

Answers (3)

zaph
zaph

Reputation: 112857

By variable I assume you mean ivar (instance variable). Did you create a @property for the ivar and if so please provide the code. Did you provide a @synthesize statement for the ivar?

If not you probably need to provide them.

self.var = x; calls the var's setter the same as: [var setVar:x];

Assuming that the variable is designed to be accesses from outside the class add a @property statement to the .h file:

@property (nonatomic, retain) ClassName *var;

in the associated .m file add:

@synthesize var;

In the .m file method dealloc add:

[var release];

The all used should access var as: In the defining class: self.var In other classes referencing by the instantiatedVariable.var

Consider studying Apple's Memory Management Programming Guide, it will save a lot of time and frustration.

Upvotes: 0

memmons
memmons

Reputation: 40502

I see the following issues:

  • Don't do this: [tmpArray release]. You don't own the reference to tmpArray, it's auto-released.

  • Do you own the reference for tmpDict? -- also, that is an awful var name ;)

  • Can you confirm [tmpDict objectForKey:@"myKey"] returns an array?

Upvotes: 0

alex_c
alex_c

Reputation: 2046

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:

@property (nonatomic, retain) MyVar *var;

is different from

@property MyVar *var;

How are you assigning to the var?

var = value;

won't kick in the retain, but this would (if the property is defined as "retain"):

self.var = value

Edit: based on the new code you posted, it looks like you're over-releasing tmpArray. Remove this line:

[tmpArray release];

You shouldn't have to release tmpArray, because this line doesn't retain it:

NSMutableArray *tmpArray = [tmpDict objectForKey:@"myKey"];

Upvotes: 1

Related Questions