Primoz Rome
Primoz Rome

Reputation: 11031

iPhone SDK - instance variable out of scope issue

I am getting crazy over this error. Compiler is saying out of scope for an instance NSSString variable. Never had this thing before and used thousands of NSString instance variables!

Here is my class .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "Snapshot.h"

@interface RecordAudioViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate> {
 NSString *filename;
}

@property (nonatomic, retain) NSString *filename;

- (IBAction) recordAudio;
- (IBAction) playAudio;

@end

Variable is synthesized properly. I initalize filename variable in viewDidLoad method. I want to use it in IBAction method recordAudio, but compiler always says out of scope? Why is that, is this a bug or something?

Here is .m code. viewDidLoad method where I set the filename instance variable:

- (void)viewDidLoad {
    [super viewDidLoad];

NSString *tmpDir = NSTemporaryDirectory(); filename = [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]; NSLog(filename); }

And the IBAction method

- (IBAction) recordAudio 
{
    NSLog(filename); // here I get out of scope message when moving over with mouse cursor and when steping over this line EXC_BAD_ACCESS
}

The entire .m file can be seen here: http://pastie.org/1021993

Upvotes: 0

Views: 813

Answers (3)

mharper
mharper

Reputation: 3272

Actually, if you set filename = [NSString stringWithFormat...], the autoreleased result is NOT retained.

However, if you use self.filename = [NSString stringWithFormat...] it WILL retain the string. Kinda looks like the string is getting released out from under you because you're not retaining it.

Upvotes: 3

JBRWilkinson
JBRWilkinson

Reputation: 4855

Is viewDidLoad actually happening? If it doesn't get called, that would perfectly explain the crash in recordAudio as it hasn't been initialised.

Upvotes: 0

Nithin
Nithin

Reputation: 6475

You mentioned that you initialize the variable filename in the viewDidLoad method. if you mean nsstring alloc and init methods by initializing, i don't think that you are going the right way. It is not necessary to initialize a synthesized string, or more generically any strings. I'm not sure whether you meant this by initializing, but i gave my opinion based on the idea that i got from your Ques.

Upvotes: 0

Related Questions