ios - 从通知对象传递数组会导致 iOS 10 中的应用程序崩溃

标签 ios arrays swift notifications

我将以下代码从 View Controller 发布到另一个 View Controller

self.genreString.append("Comedy")
self.sortByString.append("orderly")
let myDict:Dictionary<String, [String]> = ["sortoption": self.genreString, "contenttype":self.sortByString]

NSNotificationCenter.defaultCenter().postNotificationName("SecondViewControllerDismissed", object: nil, userInfo: myDict)

在接收 View Controller 时,我接受这样的数据

  if let info = sender.userInfo as? Dictionary<String,[String]> {
        // Check if value present before using it
        if let s = info["sortoption"] {

            if info["sortoption"]?.count > 0
            {
                JLToast.makeText("2", duration: 2).show()

                 let s :[String] = info["sortoption"]!


                self.genreFilter.removeAll()
                self.genreFilter = []
                self.genreFilter.appendContentsOf(s)


            }
        }
    }

但是数组没有初始化,它说!!!

最佳答案

以下代码在 XCode Playground 中运行良好

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

let myDict:Dictionary<String, [String]> = ["sortoption": ["Comdedy"], "contenttype": ["orderly"]]

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "SecondViewControllerDismissed"), object: nil, queue: OperationQueue.main) {
    notification in
    if let info = notification.userInfo as? Dictionary<String,[String]> {
        // Check if value present before using it
        if let s = info["sortoption"] {
            print(s)
        }
    }
}

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SecondViewControllerDismissed"), object: nil, userInfo: myDict)

关于ios - 从通知对象传递数组会导致 iOS 10 中的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361350/

相关文章:

ios - 自定义 Segue 推送/弹出 UIViewControllers

php - 引用 - 这个错误在 PHP 中意味着什么?

iphone - 使用 UIPopover 从图库中获取图像并显示它

arrays - 替换数组中的每个奇数元素

python - 如何将只有一个元素的numpy数组插入到mysql数据库中?

ios - 我的 For 循环中的计数没有递增

swift - SwiftUI 中的条件属性

ios - 为什么我的 tableView 变量返回 nil?

ios - 重命名iOS应用中的补全事件

iphone - 不使用 NavigationController 的 UIViewController 的 Push-Pop 类动画