トップ «前の日記(2015-12-20) 最新 次の日記(2015-12-23)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2015-12-22 [OSX][Swift]プロセス間通信(Distributed Notifications)

以前、発表したプロセス間通信(Distributed Notifications)をSwiftで実装してみた。やってみてわかったのだが、NSConnectionはSwift未対応のようだ。使うのを控えてくれということか。

通知を受け取る側(DistributedServer)のコードは以下のとおり。

import Foundation
import Cocoa
 
protocol RemoteObjectProtocol {
    func receiveString(string: String)
}
 
class AppDelegate: NSObject, NSApplicationDelegate, RemoteObjectProtocol {
    @IBOutlet var window: NSWindow?
    @IBOutlet var label: NSTextField?
    
    func applicationDidFinishLaunching(notification: NSNotification) {
        debugPrint(__FUNCTION__)
        _registerForNotes()
        _registerForDistributedObjects()
    }
    
    func _registerForNotes() {
        debugPrint(__FUNCTION__)
        let dnc = NSDistributedNotificationCenter.defaultCenter()
        dnc.addObserver(self, selector: "_handleDistributedNote:", name: "DemoDistributedNote", object: nil)
    }
    
    func _handleDistributedNote(note: NSNotification) {
        debugPrint(__FUNCTION__, "Recieived Distributed Notification!:", note)
        if let label = self.label {
            label.stringValue = "Recieived Distributed Notification!"
        }
    }
    
    func _registerForDistributedObjects() {
        debugPrint(__FUNCTION__)
        /*
        let conn = NSConnection.defaultConnection()
        conn.rootObject = self
        if !conn.registerName("DistributedServer") {
            debugPrint(__FUNCTION__, "error")
        }
        */
    }
    
    func receiveString(string: String) {
        debugPrint(__FUNCTION__)
        if let label = self.label {
            label.stringValue = string
        }
    }
}

通知を送る側(DistributedClient)のコードは以下のとおり。

import Foundation
import Cocoa
 
protocol RemoteObjectProtocol {
    func receiveString(string: String)
}
 
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet var window: NSWindow?
    
    func applicationDidFinishLaunching(notification: NSNotification) {
        debugPrint(__FUNCTION__)
        // _postNotes()
    }
    
    @IBAction func postNotes(sender: AnyObject) {
        debugPrint(__FUNCTION__)
        _postNotes()
    }
    
    @IBAction func postForDistributedObjects(sender: AnyObject) {
        debugPrint(__FUNCTION__)
        _postForDistributedObjects()
    }
    
    func _postNotes() {
        debugPrint(__FUNCTION__)
        let dnc = NSDistributedNotificationCenter.defaultCenter()
        dnc.postNotificationName("DemoDistributedNote", object: nil)
    }
    
    func _postForDistributedObjects() {
        debugPrint(__FUNCTION__)
        /*
        let remoteObject = NSConnection.rootProxyForConnectionWithRegisteredName("DistributedServer", host: "")
        remoteObject.setProtocolForProxy("RemoteObjectProtocol")
        remoteObject.receiveString(NSDate.date().description)
        */
    }
}

DistributedServerを起動した後に、DistributedClientを起動すると、アプリケーション間で通知が送られることが確認できる。

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2015-12-20) 最新 次の日記(2015-12-23)» 編集