ios - 如何使用通知和访问 userInfo?
问题描述
我正在用 Swift 开发一个生命游戏项目。我需要通过网格传递NotificationCenter
给View 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),
如何访问此对象中的状态?
谢谢。
解决方案
正如杰克建议将其转换为网格类型:
guard let grid = notified.userInfo?["grid"] as? gridType else {return}
//print(grid[0].state)
推荐阅读
- hyphenation - 在 DocRaptor API 中无法使用的外部语言的 Prince XML 连字符
- linux - 未找到 mingw32[64] 安装
- html - 卸载然后重新安装软件包后,我开始收到“在'@angular/core'中找不到导出”
- css - React Native 使用 Image 作为图层
- typescript - 打字稿我可以在数组中重新定义 T
在界面中 - javascript - 在像 electronjs 这样的自定义节点模块中添加命令行行为
- c# - 2D 精灵不从上方看相机
- java - 为什么虚拟线程(仅在短时间内休眠)会提高另一个线程的性能?
- puppeteer - Puppeteer:在 page.evaluate 中使用函数
- javascript - 如何从 promise 返回值并在另一种方法中使用它?