zURL聲明如下:爲什麼我無法訪問此實例變量?
.H:
@interface PanelController : NSWindowController <NSWindowDelegate>
{
NSURL *zURL;
}
@property (nonatomic, copy) NSURL *zURL;
.M:
@synthesize zURL;
在一個IBAction爲我:
- (IBAction)openBrowser:(id)sender {
NSOpenPanel *zOpenPanel = [NSOpenPanel openPanel];
[zOpenPanel setCanChooseFiles: TRUE];
[zOpenPanel setCanChooseDirectories: TRUE];
[zOpenPanel setAllowsMultipleSelection: TRUE];
[zOpenPanel setLevel:CGShieldingWindowLevel()];
NSInteger zIntResult = [zOpenPanel runModal];
if (zIntResult == NSFileHandlingPanelCancelButton) {
return;
}
// zURL set here
zURL = [NSURL alloc];
zURL = [zOpenPanel URL];
NSLog(@"url = %@", zURL); // works
NSString *zStr = [zURL absoluteString];
_fileField.stringValue = zStr;
[_importButton setEnabled:TRUE];
[self openPanel];
}
在接下來的IBAction爲:
NSLog(@"url = %@", zURL); // EXC_BAD_ACCESS error
你在哪裏申報zURL? –
我編輯我的問題,包括喬治信息.. –
首先 - 擺脫第一個[NSURL alloc]行。這是內存泄漏。第二次使用「self.zURL = [zOpenPanel URL]」。當前你直接設置實例變量的方式。使用self.zURL,您將通過將複製URL的屬性訪問器。 –