快速了解可选全局变量 [英] Understanding optional global variables in swift

查看:69
本文介绍了快速了解可选全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本有关Swift的书,我了解函数范围的概念,因此接下来我想了解的是为什么我们在类中使用可选类型来设置全局变量.坦白地说,我们似乎没有说每个变量,而是让类知道整个代码库中的某个特定类型的变量:var sut: ItemManager!.

I'm working through a book on Swift and I understand the idea of scope in functions so what I'd like to understand next is why we set global variables using optional types in classes. Honestly it looks like we don't set these variables per say but just let the class know that there will be a variable of a specific type somewhere throughout the code base: var sut: ItemManager!.

据我了解,变量sut是ItemManger类型的未包装的可选变量,该变量肯定具有有效值,而不是nil.我们使用感叹号或问号设置它的原因是因为此类中没有初始化程序.尚不清楚的是,由于此类没有初始化程序,因此在决定天气时使用问号或隐含地用感叹号将全局变量设置为可选变量时会影响哪些因素?

From what I understand the variable sut is an unwrapped optional of the type ItemManger which definitely has a valid value and not nil. The reason we've set it with an exclamation mark or question mark is because there isn't an initializer within this class. What isn't clear is since this class doesn't have an initializer, what factors come into play when deciding weather to set this global variable to an optional using a questions mark or implicitly unwrapped with an exclamation mark?

import XCTest
    @testable import ToDo

    class ItemManagerTests: XCTestCase {

        var sut: ItemManager!

        override func setUp() {
            super.setUp()
            // Put setup code here. This method is called before the invocation of each test method in the class.

            sut = ItemManager.init()
        }

        override func tearDown() {
            // Put teardown code here. This method is called after the invocation of each test method in the class.
            super.tearDown()
        }


        func test_ToDoCount_InitiallySetAtZero(){

            let sut = ItemManager.init()

            XCTAssertEqual(sut.toDoCount, 0)
        }

        func test_DoneCount_InitiallySetAtZero(){

            let sut = ItemManager.init()

            XCTAssertEqual(sut.doneCount, 0)
        }
    }

推荐答案

我们将其设置为感叹号或问号的原因是 因为此类中没有初始化程序

The reason we've set it with an exclamation mark or question mark is because there isn't an initializer within this class

因为初始化类的每个实例时,它必须为其属性/实例var分配内存.因此,对于var sut: ItemManager!,当init时,sut的值是nil.如果没有!?,编译器将无法分配init,因此您必须在初始化程序中手动进行init.那就是编译器告诉你的.

Because when every instance of a class is initialized, it has to allocate memory for its properties/instance var. So with var sut: ItemManager!, the value of sut is nil when init. Without !, ? the compiler can't allocate, init, so you have to init manually in an initializer. That is what compiler told you.

用于使用!?.

    当第一次分配后属性/var始终具有值时,使用
  • !.第一次分配后,以后就不能为零了
  • ?在pro/var可以有值或没有值时使用
  • ! is used when the property/var always has value after the first assigning. And after the first time it's assigned, it can't be nil later
  • ? is used when the pro/var can have value or not

这篇关于快速了解可选全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆