简体   繁体   English

Swift类型推断需要问号或感叹号

[英]Swift Type Inference requires question mark or exclamation point

After reading Apple's ARC guide, I'm slowly attempting to get a grasp on retain cycles however what isn't clear to me is swift's type inference requires the optional question mark or forced unwrapping exclamation point when inferring a variable in the global scope of a class. 在阅读了Apple的ARC指南之后,我正在慢慢尝试了解保留周期,但是我不清楚swift的类型推断在推断变量的全局范围时需要可选的问号或强制展开感叹号。类。

For example: 例如:

import XCTest
@testable import PassionProject

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()
    }

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

    func tests_ToDoCount_Is_InitiallyZero() {

        XCTAssertEqual(sut.toDoCount, 0)
    }

    func tests_DoneCount_Is_InitiallyZero(){

        XCTAssertEqual(sut.doneCount, 0)
    }
}

If I leave out the question mark or explanation point on the following line, it throws an error about the class not having initializers: 如果我在下一行省略问号或解释点,则会引发有关该类没有初始化程序的错误:

var sut: ItemManager

My question is, isn't type inference just simply saying this variable will be of this type? 我的问题是,类型推断不只是说这个变量就是这种类型吗? If so, why is Xcode considering it a property if we haven't given it an initial value? 如果是这样,如果我们没有给它一个初始值,为什么Xcode认为它是一个属性? Also second, why does force unwrapping a inferred type compile if we never set its value? 其次,如果我们从不设置值,为什么为什么要强制解包推断类型呢?

If needed, here is the code for the object we're using as an example and thank you in advance for getting a better grasp: 如果需要,下面是我们用作示例的对象的代码,在此先感谢您更好地理解:

import Foundation

class ItemManager {

    let toDoCount = 0

    let doneCount = 0

}

That's not type inference. 那不是类型推断。 If you declare a variable's type, inference doesn't happen. 如果声明变量的类型,则不会发生推断。 Inference is all about figuring ( inferring ) out what a type is if you don't say explicitly . 推理是所有关于盘算( 推断 )出类型是什么,如果你不明确地说

You're having a problem with Swift initializer rules. 您在使用Swift初始化程序规则时遇到了问题。 If you declare that a class has this property: 如果您声明一个类具有此属性:

var sut: ItemManager

Then that's non-optional, which means it must have a value by the time initialization is complete . 然后这是非可选的,这意味着在初始化完成之前它必须具有一个值 You're not doing that, so Swift is complaining about your initializers. 您没有这样做,因此Swift抱怨您的初始化程序。 You can either add an init method that assigns a value or you could declare it and assign a value at the same time-- which might look like this: 您可以添加一个分配值的init方法,也可以声明它并同时分配一个值-如下所示:

var sut: ItemManager = ItemManager()

If you declare it like this: 如果您这样声明:

var sut: ItemManager?

Then it's optional, which means if you don't assign a value then it gets a value of nil. 然后它是可选的,这意味着如果您不分配值,则它将获得nil的值。 You don't have to assign a value during initialization because it already has one. 您不必在初始化期间分配值,因为它已经有一个。

Swift, for safety reasons, requires all variables to always hold a value. 出于安全考虑,Swift要求所有变量始终保持一个值。 This prevents that scenario where the value of a variable can be unknown. 这样可以防止出现变量值未知的情况。 However, there are still cases in programming where one wants to represent the absence of a value. 但是,在编程中仍然有一些情况要表示缺少值。 A great example of this is when performing a search. 一个很好的例子是执行搜索时。 One would want to be able to return something from the search that indicates that no value was found. 希望能够从搜索中返回一些内容,表明未找到任何值。

Therefore in Swift,the class members must have a value at the time of declaration.By default a member of a specific type ,say, Int cannot be nil and it does not get a default value.If we know that the value of a variable may be nil then we define it as an Optional. 因此,在Swift中,类成员必须在声明时具有一个值。默认情况下,特定类型的成员说,Int不能为nil并且不会获得默认值。如果我们知道变量的值可能为零,那么我们将其定义为可选。

Now you have three options to provide an initial value to a class member : 现在,您有三个选项可为类成员提供初始值:

  1. By initialising it at the time of declaration 通过在声明时进行初始化

  2. By providing it a value in the init method 通过在init方法中为其提供值

  3. By defining it as an optional or unwrapped optional which depends upon the use of the variable 通过将其定义为可选变量或未包装可选变量,这取决于变量的使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM