首页 > 解决方案 > 如何使用通知和访问 userInfo?

问题描述

我正在用 Swift 开发一个生命游戏项目。我需要通过网格传递NotificationCenterView Controller. 我传递的信息如下:

let nc = NotificationCenter.default
let info = ["grid": self.grid]
nc.post(name: EngineNoticationName, object: nil, userInfo:info)

我正在接收Notification. ViewController当我打印出 userInfo 时:

let grid = notified.userInfo?["grid"]
print("\(grid!)")

我明白了(它适用于 10x10 网格,但我相信这对我的问题来说已经足够了):

网格(_cells:[[Assignment4.Cell(位置:(行:0,col:0),状态:Assignment4.CellState.empty),Assignment4.Cell(位置:(行:0,col:1),状态:Assignment4 .CellState.empty),Assignment4.Cell(位置:(行:0,列:2),状态:Assignment4.CellState.empty),Assignment4.Cell(位置:(行:0,列:3),状态:Assignment4 .CellState.empty),Assignment4.Cell(位置:(行:0,列:4),状态:Assignment4.CellState.empty),Assignment4.Cell(位置:(行:0,列:5),状态:Assignment4 .CellState.empty),Assignment4.Cell(位置:(行:0,列:6),状态:Assignment4.CellState.empty),

如何访问此对象中的状态?

谢谢。

标签: iosswift3notificationsnsnotificationcenter

解决方案


正如杰克建议将其转换为网格类型:

guard let grid = notified.userInfo?["grid"] as? gridType else {return}
//print(grid[0].state)

推荐阅读