【发布时间】:2010-06-29 00:38:20
【问题描述】:
我对这个错误感到抓狂。编译器说实例 NSSString 变量超出范围。以前从来没有这个东西,用过上千个 NSString 实例变量!
这是我的类 .h 文件
#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
变量已正确合成。我在 viewDidLoad 方法中初始化文件名变量。我想在 IBAction 方法 recordAudio 中使用它,但编译器总是说超出范围?这是为什么呢,是bug还是什么?
这是 .m 代码。我设置文件名实例变量的 viewDidLoad 方法:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *tmpDir = NSTemporaryDirectory(); 文件名 = [NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]; NSLog(文件名); }
还有IBAction方法
- (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
}
整个 .m 文件可以在这里看到:http://pastie.org/1021993
【问题讨论】:
-
我们能看到.m文件的代码吗?这是一个错误,但很可能是你的。 :-)
-
@Eiko:我已经在我的原始帖子中添加了代码。我已经做了一千次了,我不知道哪里会出错。整个 .m 文件可以在这里看到:pastie.org/1021993
-
您需要显式保留变量或让属性的语法糖执行此操作 - 请参阅 mharper 的答案。