macOS/iOS API解説

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

目次

typeOfFile:error:

INDEX>AppKit>NSWorkspace

指定されたファイルのUTIタイプを返します

Objective-C

- (NSString *)typeOfFile:(NSString *)absoluteFilePath
                   error:(NSError **)outError

Swift

func typeOfFile(_ absoluteFilePath: String,
          error outError: NSErrorPointer) -> String?

解説

指定されたファイルのUTIタイプを返します。
UTIタイプとは
public.png
public.data


このメソッドはスレッドセーフです。

返り値

Objective-C

Swift


    

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.5-

更新時のバージョン

OS X 10.10

参照

関連記事(外部サイト)

developer.apple.com

例文

Objective-C

Swift

    //NSWorkspace typeOfFile
    @IBAction func function017(sender: AnyObject) {
        //ファイルを選択
        var openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = false
        openPanel.canCreateDirectories = false
        openPanel.canChooseFiles = true
        openPanel.beginWithCompletionHandler { (result) -> Void in
            //オープンパネルでOKを選択したら
            if result == NSFileHandlingPanelOKButton {
                //選択したファイルを
                let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
                let theURL : NSURL =  openPanel.URL!
                let filepath : NSString = theURL.path!
                var anError : NSError?
                let utiType : NSString? = theWorkspace.typeOfFile(filepath, error: &anError)
                if utiType != nil {
                NSLog("%@", utiType!)
                //Ex.->  public.png , public.data
                }
            }//if result
        }//openPanel.beginWithCompletionHandler
    }