gpt4 book ai didi

ios - swift Appdelegate中的全局变量

转载 作者:IT王子 更新时间:2023-10-29 05:24:45 27 4
gpt4 key购买 nike

我正在将一些数据从 View Controller 保存到 appdelegate 的变量并从另一个 View Controller 获取它。下面是应用程序委托(delegate)的代码

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
var mainDic:NSMutableDictionary?

设置mainDic的代码

func filterResponse(response:NSDictionary){

var appDelegate=AppDelegate()
appDelegate.mainDic=response.mutableCopy() as? NSMutableDictionary
}

获取字典的代码。

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
println(appDelegate.mainDic)

问题是我得到的输出为零。请纠正我。

最佳答案

这是你的错误

var appDelegate=AppDelegate() //You create a new instance, not get the exist one

您需要访问退出的 AppDelegate

let appDelegate = UIApplication.shared.delegate as! AppDelegate

关于ios - swift Appdelegate中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com