0

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

let inputdata = row as Row

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

我不能通过“输入数据”

inputdata 将是 AnyObject ,在我的情况下是 Row

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

在此处输入图像描述

4

1 回答 1

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"])
}

如果Row是 a struct,首先你必须将它包装成一个类对象,然后你可以将类对象传递给这个函数。

创建包装类

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)
        }

    }
}
于 2015-11-11T15:27:39.620 回答