在 Python 中花费数年之后才开始使用 Objective-C .. 仍然试图围绕一些概念来思考.. 我似乎无法弄清楚这一点,但每次我增加或减少myCount
它时都会保留旧变量记忆。我正在使用 ARC,所以它不应该自动释放吗?我有一种感觉,它与self.varOfMyCount = [NSString stringWithFormat:@"%d", myCount];
标题:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
}
- (IBAction)itemOne:(id)sender;
- (IBAction)itemTwo:(id)sender;
- (IBAction)itemThree:(id)sender;
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, copy) NSString *varOfMyCount;
@end
int myCount = 0;
执行:
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength ];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Status"];
[statusItem setHighlightMode:YES];
}
- (IBAction)itemOne:(id)sender {
myCount++;
self.varOfMyCount = [NSString stringWithFormat:@"%d", myCount];
NSLog(@"%@",self.varOfMyCount);
[statusItem setTitle:self.varOfMyCount];
}
- (IBAction)itemTwo:(id)sender {
myCount = myCount-1;
self.varOfMyCount = [NSString stringWithFormat:@"%d", myCount];
NSLog(@"%@",self.varOfMyCount);
[statusItem setTitle:self.varOfMyCount];
}
- (IBAction)itemThree:(id)sender {
NSLog(@"Quit.");
[[NSApplication sharedApplication] terminate:nil];
}
@end