macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

terminate

INDEX>AppKit>NSApplication

アプリケーションを終了します

Swift4.2

open func terminate(_ sender: Any?)

Objective-C

-(void)terminate:(id)sender:

解説

アプリケーションを終了します。まずapplicationShouldTerminate:を呼び出してアプリケーションが終了することをデリゲートに通知します。NOが返されると終了しません。YESが返されるとデフォルトの通知センターにNSApplicationWillTerminateNotificationをポストします。

Swiftの場合、applicationShouldTerminate(sender:で.TerminateNowが返されるとすぐに終了します。
NSApplicationTerminateReply.TerminateCancel

enum NSApplicationTerminateReply : UInt {
    case TerminateCancel//アプリケーション終了をキャンセル
    case TerminateNow//アプリケーション終了する
    case TerminateLater//あとで
}

返り値

なし

( void )

引数

( id )sender

送信オブジェクト

フレームワーク

ApplicationKit

クラス

NSApplication

使用可能

10.0

更新時のバージョン

0S X 10.14
Swift5.1

参照

- run
- stop

例文

Objective-C

#import "Controller.h"

@implementation Controller

- (IBAction)pushButton:(id)sender
{
	[info setStringValue:@"終了処理を終えてから約10秒後に終了します"];
	[NSApp terminate:sender];
	
}

NSTimer *timer=nil;

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)theApplication
{
	NSLog(@"applicationShouldTerminate");
	NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:10]; //10秒後にタイマー起動  
	//userInfoに使う辞書を作成
	NSDictionary *userInfoDictionary =[NSDictionary dictionaryWithObjectsAndKeys:
						@"value1",@"key1",
						@"終了",@"key2",
						@"value3",@"key3",nil];
	//タイマー作成
	timer = [NSTimer scheduledTimerWithTimeInterval:1.0
							target:		self
							selector:	@selector(timerControl:)
							userInfo:	userInfoDictionary
								repeats:NO];
	//起動時間セット
	[timer setFireDate:theDate];
	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSModalPanelRunLoopMode];
	
	return NSTerminateLater;
}

//2分以内に
-(void) timerControl:(NSTimer *)aTimer
{
	NSLog(@"...%@",[[aTimer userInfo] objectForKey:@"key2"]);
	//NSLog([[timer fireDate] description]);
	[NSApp replyToApplicationShouldTerminate:YES];
}
@end

Swift4.2
ボタンを押して5秒後に終了

    //NSApplication terminate:
    //
    @IBAction func function004(_ sender: AnyObject) {
        print("function004 called")
        print("I will terminate after 5 minutes")
        //タイマー作成let timer
        let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: false)
        
        print("timer Object: \(timer).")
        
    }

Swift
ボタンを押して5秒後に終了

//NSApplication terminate
    //ボタンを押す
    @IBAction func function004(sender: AnyObject) {
        NSLog("function004 called")
        NSLog("I will terminate after 5 minutes")
        //タイマー作成
        var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)

    }
    //タイマーが起動した時の実行メソッド
    func update() {
        NSLog("good bye")//ここは実行される
        let anApplication = NSApplication.sharedApplication()
        //アプリケーション終了
        anApplication.terminate(self)
        NSLog("end")//ここは実行されない
    }
    func applicationShouldTerminate(sender: NSApplication!) -> NSApplicationTerminateReply {
        NSLog("applicationShouldTerminate")
        
        //タイマー作成

        //return .TerminateCancel //アプリケーション終了をキャンセル
        //return .TerminateLater      //あとで
        return .TerminateNow    //アプリケーション終了する
        
    }