macOS/iOS API解説

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

目次

alertWithError:

INDEX>AppKit>NSAlert>

エラーオブジェクトでアラートオブジェクトを作って返します

Objective-C

+(NSAlert *)alertWithError:(NSError *)anError:

Swift

init(error error: NSError) -> NSAlert

解説

エラーオブジェクトでアラートオブジェクトを作って返します。
SwiftにはalertWithError()という関数はなく、NSAlert(error: anError!)で作成します。

返り値

( NSAlert * )

Swift

NSAlert

アラートオブジェクト

引数

( NSError * )anError

Swift

NSError

エラーオブジェクト

フレームワーク

ApplicationKit

クラス

NSAlert

使用可能

10.4

編集時のバージョン

OS X 10.10

参照

例文

Objective-C

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
NSError *error = [NSError errorWithDomain:@"NSOSStatusErrorDomain"
                            code:-10
                            userInfo:[NSDictionary dictionary]
                            ];


NSAlert *alert = [NSAlert alertWithError:error];
//表示します、
[alert beginSheetModalForWindow:[sender window]
            modalDelegate:self
            didEndSelector:@selector(endAlert)
            contextInfo:nil
            ];

}
//閉じる時の処理
-(void)endAlert
{
NSLog(@"end");
}

@end

Swift

    //NSApplication alertWithError
    @IBAction func function002(sender: AnyObject) {
        //なにかモーダルがあっても終わり
        NSApp.abortModal()
        var anError: NSError? = NSError(domain: NSMachErrorDomain,
                                    code: -1,
                                    userInfo: nil)
        //NSAlertの作成
        let anAlert:NSAlert = NSAlert(error: anError!)
        let response:Int = anAlert.runModal()
    }