gpt4 book ai didi

ios - "let _ = ..."(让下划线等于)在 Swift 中有什么用吗?

转载 作者:IT王子 更新时间:2023-10-29 05:46:06 26 4
gpt4 key购买 nike

使用 let _ = ... 有任何目的吗?

我看过关于 What's the _ underscore representative of in Swift References? 的问题和答案我知道下划线 can be used to represent a variable that isn't needed .

如果我只需要元组的一个值,如上面链接中的示例所示,这将是有意义的:

let (result, _) = someFunctionThatReturnsATuple()

不过,我最近遇到了this code :

do {
let _ = try DB.run( table.create(ifNotExists: true) {t in
t.column(teamId, primaryKey: true)
t.column(city)
t.column(nickName)
t.column(abbreviation)
})

} catch _ {
// Error throw if table already exists
}

如果我只是删除 let _ =,我不会收到任何编译器警告或错误。在我看来,这样更简单、更易读。

try DB.run( table.create(ifNotExists: true) {t in
t.column(teamId, primaryKey: true)
t.column(city)
t.column(nickName)
t.column(abbreviation)
})

代码作者写了a book and a blog about Swift .我知道作者并非万无一失,但这让我想知道我是否遗漏了什么。

最佳答案

如果该方法被 developer documentation 中的 warn_unused_result 标记,您将收到编译器警告:

Apply this attribute to a method or function declaration to have the compiler emit a warning when the method or function is called without using its result.

You can use this attribute to provide a warning message about incorrect usage of a nonmutating method that has a mutating counterpart.

关于ios - "let _ = ..."(让下划线等于)在 Swift 中有什么用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36713007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com