0

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
4

1 に答える 1