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

时间:2021-05-24 22:26:55

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? Also second, why does force unwrapping a inferred type compile if we never set its value?

我的问题是,不是类型推断只是简单地说这个变量将属于这种类型吗?如果是这样,如果我们没有给它一个初始值,为什么Xcode认为它是一个属性?另外,如果我们从未设置其值,为什么强制解包推断类型编译?

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

}

2 个解决方案

#1


5  

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. If you declare that a class has this property:

你遇到了Swift初始化程序规则的问题。如果您声明某个类具有此属性:

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. 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:

那么这是非可选的,这意味着它必须在初始化完成时具有值。你没有这样做,所以Swift抱怨你的初始化者。您可以添加一个指定值的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. You don't have to assign a value during initialization because it already has one.

然后它是可选的,这意味着如果你没有赋值,那么它的值为nil。您不必在初始化期间分配值,因为它已经有一个值。

#2


2  

Swift, for safety reasons, requires all variables to always hold a value. 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.

出于安全原因,Swift要求所有变量始终保持一个值。这可以防止变量值可能未知的情况。但是,在编程中仍然存在需要表示缺少值的情况。一个很好的例子就是执行搜索。人们希望能够从搜索中返回一些表明没有找到值的内容。

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

    通过将其定义为可选或未包装的可选项,取决于变量的使用

#1


5  

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. If you declare that a class has this property:

你遇到了Swift初始化程序规则的问题。如果您声明某个类具有此属性:

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. 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:

那么这是非可选的,这意味着它必须在初始化完成时具有值。你没有这样做,所以Swift抱怨你的初始化者。您可以添加一个指定值的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. You don't have to assign a value during initialization because it already has one.

然后它是可选的,这意味着如果你没有赋值,那么它的值为nil。您不必在初始化期间分配值,因为它已经有一个值。

#2


2  

Swift, for safety reasons, requires all variables to always hold a value. 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.

出于安全原因,Swift要求所有变量始终保持一个值。这可以防止变量值可能未知的情况。但是,在编程中仍然存在需要表示缺少值的情况。一个很好的例子就是执行搜索。人们希望能够从搜索中返回一些表明没有找到值的内容。

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

    通过将其定义为可选或未包装的可选项,取决于变量的使用