快速了解可选全局变量

编程入门 行业动态 更新时间:2024-10-09 03:25:06
本文介绍了快速了解可选全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读一本有关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

更多推荐

快速了解可选全局变量

本文发布于:2023-11-29 06:50:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   全局变量   快速

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!