- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在试图弄清楚发生了什么但无济于事。我已经尽可能多地提炼了代码,但如果我在项目中这样做,我仍然会收到“运算符 && 的结果未使用警告(即使它已被使用)但复制到 Playground 的相同代码工作正常,没有警告.这只是一些伪代码,在我试图找到问题的同时重写了一遍基本代码。
enum WordError: Error {
case tooShort
case tooLong
}
func isTooShort(_ word: String) throws -> Bool {
if word.count < 3 { throw WordError.tooShort }
return true }
func isTooLong(_ word: String) throws -> Bool {
if word.count > 5 { throw WordError.tooLong }
return true }
func check(_ word: String) {
do {
try isTooShort(word) && isTooLong(word)
print(word)
} catch let error as WordError {
print("\(error)")
} catch {
}
}
这只是一个错误还是我在这里做错了什么?
我想如果我使用:我可以消除警告:
try _ = isTooShort(word) && isTooLong(word)
但我不确定这是否是“修补”它的正确方法。
最佳答案
这样做没有错。 “正确”的方法是,当像 isTooShort
这样的东西属于您并且您想在不捕获结果的情况下调用它时,是用 @discardableResult
标记它。如果你这样做了,那么你就可以写
do {
try isTooShort(word)
try isTooLong(word)
print(word) // if we get here, it's neither too short nor too long
} catch ...
但在这些情况下,您所做的也是“正确的”。
真正 的问题是为什么您既要返回 Bool 又要 抛出错误。您对 isTooShort
的实现非常奇怪。您似乎在滥用 throw
。目前尚不清楚您通过以这种奇怪的方式实现它试图解决什么问题。 isTooShort
只能以一种方式失败:单词太短。那么为什么它不只返回一个 bool 值呢? isTooShort
会问一个简单的是/否问题,所以只需回答它:返回 Bool 并停止。
如果您的目标是回答一个三向问题 - 即告诉调用者这个词是太短、太长还是恰到好处,那么再次返回一个自定义枚举回答了这个问题:
enum WordLength {
case tooShort
case tooLong
case justRight
}
func howIs(_ word: String) -> WordLength {
if word.count < 3 { return .tooShort }
if word.count > 5 { return .tooLong }
return .justRight
}
关于swift - "Result of ' && do/try/catch block 中的 ' unused"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042016/
__attribute__((unused)) 和 __attribute((unused)) 可以将变量标记为未使用以避免未使用警告。 它们有什么区别? 最佳答案 在 GCC 存储库中,在文件 c-
在 Visual Studio 2010(C++,非托管)中调试,信息 unused ??? 或 unused 0 应该是什么意思?我附上了两个屏幕截图, child 是现有窗口的 HWND。 我也对
我只想在我的 Xcode 项目的一个文件中删除此编译器警告。有办法做到这一点吗? 最佳答案 您可以使用 a pragma directive and the "diagnostic" keyword
为什么.NET 警告“未使用的变量”而不是“未使用的参数”? (我相信 Java 在这两种情况下都会发出警告。) 为什么 .NET 不关心“未使用的参数”? 最佳答案 在我看来,您可能真正想要保留未使
我正在处理这个教程应用程序,代码给我这个警告: Xcode WARNING: Unused Entity Issue: Unused Variable 执行这条语句时报错: int newRowInd
考虑以下代码: class Foo { let bar = "Hello world!" init () { self // Warning: Expression o
您好,我正在尝试将 zxing 二维码阅读器整合到我的应用程序中。我直接从谷歌代码网站检查了 svn,并在我的项目设置中添加了所有标题路径。然而,当我尝试构建项目时,我遇到了很多“未使用的函数”和“未
在 Pipes Tutorial ,它说: The concrete type synonyms use () to close unused inputs and X (the uninhabite
我有工作 Swift.2.3 项目,但是当我构建并将其转换为 Swift3 时,它给我: Result of call to (_:parameters:completionHandler:)' is
我有一个基于 cmake 的包。它有几个目标可执行文件。其中一个目标时不时会崩溃。回溯如下: ... #19 XXX::~XXX (this=0x69a120, __in_chrg=) at
我刚刚用 C 编写了我的第一个应用程序,我收到了这个警告(已编辑):unused variable pp int compteur = 1; int *p = &compteur; int **pp
我的 C++ 编译器发出警告,指出以下“intVar”变量“未使用”。 void MyClass::MyMethod(bool bFlag) { int intVar = 10; if
我正在处理一个 Flask 项目,从 API 包装器获取一些数据。包装器返回一个生成器对象,所以我在将其传递给 Flask 的 render_template() 之前打印值(for obj in g
我是 goLang 的新手。我正在尝试从 intellij 构建一个项目,该项目使用 git 库中的一个包: import ( "github.com/aerospike/aerospike-
我收到很多不正确的 ESLint/TS 警告,说枚举案例“已分配一个值但从未使用过”或导入“已定义但从未使用过”。这是一些代码。 所有导入都说它们已定义但从未使用过(尽管您可以看到它们在底部的类型中)
这对我来说真是个谜。我已经这样定义了我的方法(对于类“graf”): addStatistics <- function(x) UseMethod("addStatistics") addStatis
我正在尝试使用 grunt-unused 删除多个子目录中所有未使用的图像链接。 .为清楚起见,这是我的文件夹结构: |-- dist | |-- site-1 | | |—-index.htm
我试图在我通过函数创建的条形图上注释标准消息。以下是代码: hashbar <- function(x) { suppressWarnings(library(stringr))
我在一个过程中有一个变量,我需要保持该变量的事件状态,直到该过程中的匿名方法运行为止,但我不在匿名方法中使用该变量。有没有一种惯用的方法告诉编译器无论如何都要捕获变量? 例如: procedure F
我正在使用 Delphi XE、Windows 7。 在应用程序中,我想启用不同的报告类型供用户选择。为此,我有 1 个基本报告类和每个报告类型(xml、csv、ppt 等)的子类。 {Just an
我是一名优秀的程序员,十分优秀!