xcode 快速创建全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44781071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:05:35  来源:igfitidea点击:

Creating a global variable in swift

swiftxcodeswift3

提问by Robert Smith

I am trying to create a global variable from my view controller (inside the class but outside the functions) as follows:

我正在尝试从我的视图控制器(在类内部但在函数外部)创建一个全局变量,如下所示:

var modelData: CustomTabBarController.model   // THE ERROR IS HERE

This is how that class is defined:

这是该类的定义方式:

CustomTabBarController.swift:

CustomTabBarController.swift:

import UIKit

// This class holds the data for my model.
class ModelData {
    var name = "Fred"
    var age = 50
}

class CustomTabBarController: UITabBarController {

    // Instantiate the one copy of the model data that will be accessed
    // by all of the tabs.
    var model = ModelData()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

However I am getting the following error:

但是我收到以下错误:

"'model' is not a member type of "CustomTabBarController"

How do I declare it so that I can access model? Thanks.

我如何声明它以便我可以访问模型?谢谢。

Update #1Sorry I forgot to mention this:

更新 #1对不起,我忘了提到这一点:

I need the model data to be the SAME in every tab of the tabbar. For example if I change the age to 51 in the first tab, the second tabbar should retrieve 51. Which would be the correct method above to use it this way?

我需要标签栏的每个选项卡中的模型数据都相同。例如,如果我在第一个选项卡中将年龄更改为 51,则第二个选项卡栏应该检索 51。上面使用这种方式的正确方法是什么?

Update #2

更新 #2

I am able to create the variable inside a function with dean's suggestion:

我能够在院长的建议下在函数内创建变量:

func setupModelData()
{
  var modelData = (self.tabBarController as! CustomTabBarController).model
}

However this does not work, since I need to access the modelData from other functions. When I attempt to move this line outside of the function as follows:

但是这不起作用,因为我需要从其他函数访问模型数据。当我尝试将这条线移出函数时,如下所示:

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
 var modelData = (self.tabBarController as! CustomTabBarController).model
...

I receive the error:

我收到错误:

Value of type '(NSObject) -> () -> FirstViewController' has no member 'tabBarController'

回答by Robert Smith

I ended up following holex's suggestion of creating a shared class (singleton):

我最终遵循了holex关于创建共享类(单例)的建议:

import UIKit
class ModelData: NSObject {
    static let shared: ModelData = ModelData()
    var name = "Fred"
    var age = 50
}

Writing in first view: Set age to 51:

在第一视图中写入:将年龄设置为 51:

ModelData.shared.age = 51

Reading in second view: Get age of 51

在第二个视图中阅读:获取 51 岁

let age = ModelData.shared.age

回答by deanWombourne

I'm not sure whether you truly want a global variable (i.e. a single instance of ModelData shared between all your view controllers) or an instance variable which is public, so I'll try to answer both :)

我不确定您是真的想要一个全局变量(即在所有视图控制器之间共享的 ModelData 的单个实例)还是一个公共的实例变量,所以我会尝试回答两者:)

1) global model

1) 全局模型

This line attempts to get the model property from the CustomerTabBarControllerclass- i.e. if you made multiple tab bar controllers they would all use the same model.

此行尝试从CustomerTabBarController类中获取模型属性- 即,如果您制作了多个标签栏控制器,它们都将使用相同的模型。

var modelData: CustomTabBarController.model

If this is what you want, then you need to change this line to include the static keyword.

如果这是您想要的,那么您需要更改此行以包含 static 关键字。

static var model = ModelData()

However, this almost certainly isn't what you're after.

然而,这几乎肯定不是你想要的。

2) shared instance variable

2) 共享实例变量

This means that the model is part of each instanceof CustomTabBarController. Here, you would need to change the line which is throwing the error to be something like this:

这意味着模型是 的每个实例的一部分CustomTabBarController。在这里,您需要将引发错误的行更改为如下所示:

var modelData: myCustomTabBarController.model

Without knowing more about your architecture, I can't help you get hold of your tab bar controller instance, but something like this might work (inside other view controllers):

在不了解您的架构的情况下,我无法帮助您掌握您的标签栏控制器实例,但这样的事情可能会起作用(在其他视图控制器中):

var modelData = (self.tabBarController as! CustomTabBarController).model

回答by vadian

modelis an instance variable.

model是一个实例变量。

Either create an instance of CustomTabBarController

要么创建一个实例 CustomTabBarController

var modelData =  CustomTabBarController().model

Or declare modelas class variable

或者声明model为类变量

static var model = ModelData()

...

var modelData = CustomTabBarController.model


However to use ModelDataas a single global variable with the two members, use a struct rather than a class and declare the members as static.

但是,要将ModelData两个成员用作单个全局变量,请使用结构而不是类并将成员声明为静态。

struct ModelData {
    static var name = "Fred"
    static var age = 50
}

You can access the name from everywhere for example

例如,您可以从任何地方访问该名称

let name = ModelData.name

and there is no need to create an extra variable in another class.

并且不需要在另一个类中创建额外的变量。



An – instance based –? alternative is a singleton

一个——基于实例的——?另一种选择是单例

struct ModelData {

    static let shared = ModelData()

    var name = "Fred"
    var age = 50
}

and use it

并使用它

let name = ModelData.shared.name