socket.io
내 빠른 iOS 앱에서 구현 하고 있습니다.
현재 여러 패널에서 서버를 듣고 수신 메시지를 기다립니다. getChatMessage
각 패널 에서 함수를 호출하여 이렇게합니다 .
func getChatMessage(){
SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//do sth depending on which panel user is
})
}
}
그러나 나는 그것이 잘못된 접근 방식이라는 것을 알았고 그것을 변경해야합니다. 이제는 들어오는 메시지를 한 번만 듣고 메시지가 오면이 메시지를 듣는 모든 패널에 전달하고 싶습니다.
그래서 NSNotificationCenter를 통해 들어오는 메시지를 전달하고 싶습니다. 지금까지 어떤 일이 일어났다는 정보를 전달할 수 있었지만 데이터 자체는 전달할 수 없었습니다. 나는 그것을 다음과 같이하고 있었다.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil)
그런 다음 다음과 같은 함수가 있습니다.
func showSpinningWheel(notification: NSNotification) {
}
그리고 내가 그것을 부르고 싶을 때마다 나는하고 있었다.
NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self)
그렇다면 객체를 전달하고 messageInfo
호출되는 함수에 어떻게 포함시킬 수 있습니까?
답변
스위프트 2.0
userInfo
[NSObject : AnyObject] 유형의 선택적 사전을 사용하여 정보를 전달합니까?
let imageDataDict:[String: UIImage] = ["image": image]
// Post a notification
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict)
// Register to receive notification in your class
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil)
// handle notification
func showSpinningWheel(notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
Swift 3.0 버전 이상
userInfo는 이제 [AnyHashable : Any]? Swift에서 사전 리터럴로 제공하는 인수로
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
// For swift 4.0 and above put @objc attribute in front of function Definition
func showSpinningWheel(_ notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
참고 : 알림 “이름”은 더 이상 문자열이 아니지만 Notification.Name 유형이므로 NSNotification.Name(rawValue:"notificationName")
자체 사용자 지정 알림으로 Notification.Name을 사용 하고 확장 할 수 있습니다.
extension Notification.Name {
static let myNotification = Notification.Name("myNotification")
}
// and post notification like this
NotificationCenter.default.post(name: .myNotification, object: nil)
답변
Swift 3의 경우
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
func showSpinningWheel(_ notification: NSNotification) {
print(notification.userInfo ?? "")
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["image"] as? UIImage{
// do something with your image
}
}
}
Swift 4의 경우
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
@objc func showSpinningWheel(_ notification: NSNotification) {
print(notification.userInfo ?? "")
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["image"] as? UIImage{
// do something with your image
}
}
}
답변
안녕하세요 @sahil 저는 신속한 3에 대한 답변을 업데이트합니다.
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
func showSpinningWheel(_ notification: NSNotification) {
print(notification.userInfo ?? "")
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["image"] as? UIImage{
// do something with your image
}
}
}
도움이 되었기를 바랍니다. 감사
답변
이것이 내가 그것을 구현하는 방법입니다.
let dictionary = self.convertStringToDictionary(responceString)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SOCKET_UPDATE"), object: dictionary)
답변
신속한 4.2에서 NSNotification을 사용하여 코드를 표시하고 숨기려면 다음 코드를 사용했습니다.
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo? [UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardheight = keyboardSize.height
print(keyboardheight)
}
}
답변
