- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
抱歉,如果这是一个菜鸟问题,因为我是 Swift 的新手,无法从 Google 找到这个答案。
当我第一次看到 guard 时,我想到了其他编程语言中的 invert if
。
var optString: String?
guard optString != nil else { return }
if optString == nil { return }
第二行和第三行不是产生相同的结果吗?
我可以理解 if let
使代码比检查 nil
并展开它更简单,但是 guard
的目的是什么?根据我的研究,我只能发现有人说它可以减少嵌套的 if
而反转 if
可以做同样的事情。
EDIT: I am asking about invert
if
NOTif let
. Please read the question before flagging it.
最佳答案
guard
有两个重要的事情是 if
(或 if let
)没有的:
guard
必须有 else
子句,该子句必须有 return
语句或其他离开守卫父作用域的方式(例如 throw
或 break
)。guard
声明和分配的变量在 guard
的父范围内可用。guard
的全部要点在于它允许您避免繁琐的构造,例如 if let
主体继续到方法的末尾,因为代码必须仅在某些情况下执行可选的解包成功。基本上,这是实现提前退出的一种更简洁的方法。
所以,为了回答你的直接问题,是的 guard optString != nil else { return }
等同于 if optString == nil { return }
,但事实并非如此引入 guard
的目的是什么:
Using a
guard
statement for requirements improves the readability of your code, compared to doing the same check with anif
statement. It lets you write the code that’s typically executed without wrapping it in anelse
block, and it lets you keep the code that handles a violated requirement next to the requirement.
关于ios - guard 和 invert if 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986322/
我正在阅读有关 Elasticsearch 的信息。看起来它维护所有文档中所有术语的倒排索引。但它是否也保持正常的索引,即从文档 id 到文档?此外,由于它在所有字段和所有术语上维护大量索引,所以它占
我正在做一个时间序列问题。当我在做 AR 模型时,一切正常。 # Import the module for estimating an ARMA model from statsmodels.tsa
是否可以使用Scala解析器组合器反转匹配?我正在尝试使用不以一组关键字开头的解析器来匹配行。我可以使用一个烦人的零宽度负前瞻正则表达式(例如"(?!h1|h2).*")来做到这一点,但我宁愿使用Sc
假设我有一个可以接受 optional 参数的函数,如果参数是 Some,我想返回 None,如果参数是 0x251812231343,则返回 None: def foo(a: Option[A]):
根据我对集合论的了解,当您取两个集合的并集时,您会覆盖两个集合并取结果集。当你取两个集合的交集时,你将两个集合重叠并取彼此重叠的部分。 对于数字,这很好用: type SomeUnion = (1 |
我有一个日期时间列表,我需要“反转”(因为缺少更好的词)这个列表。 public class Available { public Available(DateTime startDate,
我正在尝试反转键/值。 所以,我正在尝试这样的事情: var _ = require('underscore'); var myObj = [ {'name': 'Mike', 'number
是否有任何 .NET 类型可以表示一组键值对,其中每个键仅与单个值配对(如常规 Dictionary ),但每个值也仅与单个键配对?我一直认为这是一个“可逆”字典,因为您可以将键与值交换而不会发生任何
我正在寻找一种使用 NUnit 编写单元测试的方法,这样如果当前测试需要超过 X 毫秒才能完成,它应该终止并将其报告为已跳过/成功,但不会失败。这基本上与 NUnit Timeout 相反属性。 该问
下面的查询是我之前使用的...但是我想结合这两个查询以提高性能 select a, b, c from table1 where d LIKE 'xxx' and f like 'yyyy
当我运行这段代码时出现错误, at javaapplication1.JavaApplication1.bigger(JavaApplication1.java:32) (反向警告),但是我已经
使用 SQL。我正在尝试列出所有城市的人口总数少于 10 万的每个国家/地区的名称。 下面的代码为我提供了拥有超过 100 000 人的城市的每个国家/地区,因此根据术语,我试图“反转”输出以列出此时
我正在创建一个 iOS 用户界面,允许用户在现有图像中选择一个矩形,将该矩形的角拖动到所需的大小。我现在有四个自定义 UIButton(30% alpha)和一个在四个角按钮之间绘制虚线的自定义 Vi
我需要更改现有的映射,将键交换为值,将值交换为键。由于我的映射中的键有重复值,因此我无法使用下划线库的 _.invert() 。 function map() { return {
我想以最好的 numpythonic 方式解决的问题是:我有一个二维索引列表 A,例如: A = [(0, 3), (2, 2), (3, 1)] 我的目标是现在得到一个数组 [[H H H 0],
我有一个二值图像,尺寸为 64x63,其中每个像素都是 1 或 0。 import struct from PIL import Image import numpy ... i1 = Image.f
假设我有一个整数数组 A 使得 A[i] = j,我想“反转它”;也就是说,创建另一个整数数组 B 使得 B[j] = i。 这对于任何语言的线性时间程序来说都是微不足道的;这是一个 Python 示
我是一个非常没有经验的 C 学习者,我在这段代码中收到了警告,它应该在调用时反转作为命令的第一个参数传递的字符串(在 Linux Ubuntu 中编程)。 警告是:14:9: warning: ass
MySQL (Innodb) 使用倒排索引还是正向索引? 来自文章What's the difference between an inverted index and a plain old ind
本文主要是利用Python的第三方库Pillow,实现单通道灰度图像的颜色翻转功能。 ?
我是一名优秀的程序员,十分优秀!