Variables reset after calling with IBAction
In Objective-c, I have variables set in the interface file:
#import <Cocoa/Cocoa.h>
@interface TestApp_BotAppDelegate : NSObject <NSApplicationDelegate>
{
NSString * someString;
}
- (IBAction) doSomething:(id)sender;
@end
And have this
#import "TestApp_BotAppDelegate.h"
@implementati开发者_运维知识库on TestApp_BotAppDelegate
@synthesize window;
@synthesize Buildings;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
someString=@"HELLO";
}
- (IBAction) doSomething:(id) sender
{
NSLog(@"%@", someString);
}
@end
When i call doSomething from a button in the UI, I get a bad access error.
I know you that this is supposed to happen, but I don't know why or a workaround.
Thanks, Will
You are getting a bad access error because the string is not being properly managed in memory as explained in this question: EXC_BAD_ACCESS signal received. According the the code you posted you are never allocing and initializing someString, and therefore never releasing it. I believe you are not receiving null because the someString object never is taking up any memory to begin with.
精彩评论