ios - 实例成员不能用于“AppDelegate”类型
问题描述
我在 AppDelegate 类中声明了一个变量 itemGlobalArray,如下所示
var itemGlobalArray = NSMutableArray()
并尝试在如下视图控制器中使用它
if (AppDelegate.itemGlobalArray).count > 0 //Gives error here
{
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "BasketVC") as? BasketVC
self.navigationController?.pushViewController(vc!, animated: true)
}
else {
let alert = UIAlertController(title: "Alert", message: "cart Empty", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
self.present(alert, animated: true, completion: nil)
}
解决方案
您必须像静态成员一样声明它,静态成员可以在没有实例的情况下使用:
class AppDelegate {
static var itemGlobalArray = NSMutableArray()
//...
}
比你可以在你的情况下使用它:
if (AppDelegate.itemGlobalArray).count > 0 { ... }
推荐阅读
- math - 计算向量端点
- laravel - Laravel 发现意外数据
- parsing - 自定义解析返回值,保留未命名的终端
- python-3.x - 与另一个线程或进程共享 asyncio.Queue
- azure - 使用 Terraform 部署期间在 Azure VM 上安装 DSC 扩展的问题
- c# - 如何将 JSON 中的键值对更改为 XML 属性而不是节点
- python - 用 Python 连接 Lifx 灯
- vb.net - 从文本文件中选择随机人并更改相应的值
- postgresql - 如何在 jmeter 中通过隧道配置 PostgreSQL 数据库
- javascript - 使用 javascript 在 php 循环上构建,类只记录第一个结果