gpt4 book ai didi

ios - 我如何像在 Objective C 宏中声明那样全局声明变量或函数

转载 作者:行者123 更新时间:2023-11-30 12:20:10 24 4
gpt4 key购买 nike

I want to make some functions and variables globally so that i can access those function and variables from any where



class:GlobalClass{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
}

我正在使用全局类,但每次我想使用函数时,我都必须初始化 GlobalClass,如下所示:

GlobalClass().appDelegate

最佳答案

您可以通过创建一个名为 GlobalClass 的新文件并在该类中导入 UIKit 和 Foundation 来实现此目的

并将变量和函数直接放入此文件中,例如:-

import UIKit
import Foundation

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let scree_width = UIScreen.main.bounds.width

func status()->Bool{
return true
}

您可以在任何地方访问此文件的变量和函数,如下所示:-

appDelegate
status()

关于ios - 我如何像在 Objective C 宏中声明那样全局声明变量或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44889984/

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