如何使用NSNotification将sqlite.swift的行传递给另一个控制器

时间:2015-11-11 13:20:37

标签: swift sqlite.swift

我在我的项目中使用sqlite.swift。

let inputdata = row as Row

NSNotificationCenter.defaultCenter().postNotificationName("navigateToProductDetail",object: inputdata)

我无法通过" inputdata"

inputdata将是AnyObject,在我的例子中是它的行

所以它抛出错误,帮我解决这个问题或者告诉我将这个行对象传递给另一个控制器的替代方法

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以像这样通过userInfo传递它

let userInfo = [ "inputData" : inputdata ]
NSNotificationCenter.defaultCenter().postNotificationName("navigateToProductDetail", object: nil, userInfo: userInfo)

您可以使用NSNotification属性

userInfo对象获取此信息
func handleNotification(notification: NSNotification){
    print(notification.userInfo)
    print(notification.userInfo!["inputData"])
}

如果Rowstruct,首先必须将其包装到类对象中,然后将类对象传递给此函数。

创建包装类

class Wrapper<T> {
    var wrappedValue: T
    init(theValue: T) {
        wrappedValue = theValue
    }
}    

包裹你的行

let wrappedInputData = Wrapper(theValue: inputdata)
let userInfo = [ "inputData" : wrappedInputData ]
NSNotificationCenter.defaultCenter().postNotificationName("navigateToProductDetail", object: nil, userInfo: userInfo)   

取回您的inputData

func handleNotification(notification: NSNotification){
    print(notification.userInfo)

    if let info = notification.userInfo {
        if let wrappedInputData = info["inputData"] {
            let inputData : Row = (wrappedInputData as? Wrapper)!.wrappedValue
            print(inputData)
        }

    }
}