如何在swift中通过通知传递多个值 [英] how to pass multiple values with a notification in swift

查看:107
本文介绍了如何在swift中通过通知传递多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过通知发送数字和字符串......

How to send a number and a String through a notification ...

let mynumber=1;
let mytext="mytext";
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????);

并在接收方收到价值?

func refreshList(notification: NSNotification){
        let receivednumber=??????????
        let receivedString=?????????
    }


推荐答案

你可以将它们包装成一个 NSArray NSDictionary 或自定义对象。

You could wrap them in an NSArray or a NSDictionary or a custom Object.

例如:

let mynumber=1;
let mytext="mytext";

let myDict = [ "number": mynumber, "text":mytext]

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object:myDict);

func refreshList(notification: NSNotification){
    let dict = notification.object as! NSDictionary
    let receivednumber = dict["number"]
    let receivedString = dict["mytext"]
}

这篇关于如何在swift中通过通知传递多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆