CocoaのためのCポインタの取り扱いその3 [Swift]
UnsafeMutablePointer
AutoreleasingUnsafeMutablePointer
Objective-Cの返り値は(Cの拡張なので)一つしかないので、何か複数の情報をメソッドから返してもらいたい場合は、引数にポインタを渡してそこに入れてもらう感じにする。
BoolとかNSStringとかのポインタを渡すのだけど、どうするんだろうか?
NSWorkspaceの関数を見ていて疑問に思った。
これではアカンみたいだ。
var description = AutoreleasingUnsafeMutablePointer<NSString?>
こんな感じでええのかな?
var unmountable = ObjCBool(false)
var description = NSString?()
- 出版社/メーカー: EverGene
- メディア: ヘルスケア&ケア用品
- この商品を含むブログを見る
例文
//NSWorkspace getFileSystemInfoForPath:isRemovable:isWritable:isUnmountable:description:type: @IBAction func function025(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 removable = ObjCBool(false) var writable = ObjCBool(false) var unmountable = ObjCBool(false) var description = NSString?() var type = NSString?() if (theWorkspace.getFileSystemInfoForPath( filepath as! String, isRemovable: &removable, isWritable: &writable, isUnmountable: &unmountable, description: &description, type: &type ) ){ NSLog("YES") }else{ NSLog("NO") } // -> YES NSLog("%@,%@", description! ,type!) // -> hfs,hfs if removable {NSLog("removable:YES")}else{NSLog("removable:NO")} // -> removable:NO if writable {NSLog("writable:YES")}else{NSLog("writable:NO")} // -> writable:YES if removable {NSLog("removable:YES")}else{NSLog("removable:NO")} // -> removable:NO }//if result }//openPanel.beginWithCompletionHandler }
更新時のバージョン
OS X 10.10.3
Swift1.2