快速操作sql编写代码
Swift has evolved significantly over the years. It offers type safety and is a lot more concise than many languages. As the number of features continues to increase, knowing and keeping a track of some cool tricks is becoming a challenge.
多年来,Swift取得了长足的发展。 它提供类型安全性,并且比许多语言都简洁得多。 随着功能数量的不断增加,了解并掌握一些不错的技巧已成为一项挑战。
In the following sections, I’ll walk you through some Swifty ways of writing code. I hope it helps you build even better iOS apps.
在以下各节中,我将引导您完成一些Swifty编写代码的方法。 我希望它可以帮助您构建更好的iOS应用。
使用地图安全解开可选项 (Use Maps to Safely Unwrap Optionals)
Typically, we use the if let
or guard let
syntax to safely unwrap optionals in Swift. While that’s all fine, sometimes you wish there was a way without those brackets around — especially when you’re unwrapping a child property. Thankfully, we can unwrap optionals using the map operator.
通常,我们使用if let
或guard let
语法来安全地打开Swift中的可选内容。 一切都很好,但有时您希望有一种方法可以解决这些问题,尤其是在拆包子资产时。 值得庆幸的是,我们可以使用map运算符解开可选参数。
Optional values passed inside a map
closure are evaluated only if they contain some value, thereby ensuring it’s not nil
.
一个内部传递的可选值map
中是否含有一定的价值,从而确保它不封闭,只能判断nil
。

Using maps for unwrapping is handy in optional tuples as well. Here’s a Swifty way to do it:
在可选的元组中,使用映射进行解包也很方便。 这是一种快速的方法:
func sampleTuple() -> (String, String)?{
return nil
}let (a, b) = sampleTuple().map { ($0, $1) } ?? ("NA", "NA")
在初始化时调用willSet和didSet (Invoking willSet and didSet at Initialization)
In short, willSet
and didSet
property observers are not called when the property is first initialized. But you can work around this by wrapping the initialization in a defer
statement.
简而言之,在首次初始化属性时,不会调用willSet
和didSet
属性观察器。 但是您可以通过将初始化包装在defer
语句中来解决此问题。
Though this approach is a bit hacky, as you’d somehow have to set a default value (in the declaration or outside defer), it’s still good to know:
尽管这种方法有点笨拙 ,但是由于您不得不以某种方式设置默认值(在声明中或在外部defer中),因此仍然很高兴知道:

使用协议扩展进行默认实现 (Using Protocol Extensions for Default Implementations)
Swift protocols are powerful, but they don’t let you specify default implementations. This could be handy at times when you want to avoid overriding the functions.
Swift协议功能强大,但是它们不允许您指定默认实现。 当您想避免覆盖函数时,这可能会很方便。
Moreover, you could use the where
clause in the extensions to specify protocol implementations for certain constraints only. But make sure you don’t overdo protocol extensions in your codebase.
此外,您可以使用扩展中的where
子句仅针对某些约束指定协议实现。 但是请确保您不会在代码库中过度使用协议扩展。

轻松跟踪字典中的更改 (Track Changes in Dictionaries Easily)
There may come a point where you’d like to know what’s changed in a Swift dictionary (probably for debugging). The tedious way of doing this is by diffing the contents. But there’s a Swifty way of doing this as well. Simply define a subscript on an object that holds that dictionary, as shown below:
在某些情况下,您可能想知道Swift字典中的更改(可能是用于调试)。 繁琐的方法是通过分散内容。 但是,还有一种快速的方法可以做到这一点。 只需在保存该字典的对象上定义一个下标,如下所示:

在不同范围内使用防护罩 (Use guard let in Different Scopes)
The guard let
statement uses the safe fail-first approach wherein a nil
value ensures that you return immediately. But sometimes, using return
isn’t in our best interest. For instance, you could be in a for loop and only wish to continue
or break
. Gladly, you can do this in the following way:
guard let
语句使用安全的故障优先方法,其中nil
值可确保您立即返回。 但是有时候,使用return
并不符合我们的最大利益。 例如,您可能处于for循环中,只希望continue
或break
。 很高兴,您可以通过以下方式进行操作:

使用重新抛出进行强大的错误处理 (Use rethrows for Powerful Error Handling)
We all know and use the throws
keyword but rarely leverage the power of rethrows
in Swift. A function declared with the rethrows
keyword indicates that it throws an error only if one of its function parameters throws
.
我们都知道并使用throws
关键字,但是很少利用Swift中的rethrows
。 函数与宣布rethrows
关键字表明它抛出只有当它的功能参数之一的错误throws
。
This means that if the closure parameter doesn’t throw an error, we don’t need to use the different flavors of try
when calling it. Swift lets us use this elegant way to significantly reduce the boilerplate code. As you can see in the code below, we don’t need to put the same non-throwing version of a function in a do-catch
block.
这意味着,如果Closure参数没有引发错误,则在调用它时我们不需要使用其他try
风格。 Swift让我们使用这种优雅的方式来显着减少样板代码。 如您在下面的代码中看到的,我们不需要在do-catch
块中放入相同的非抛出函数版本。

结论 (Conclusion)
That’s it for this one. Thanks for reading and I hope you enjoyed the article.
这就是它了。 感谢您的阅读,希望您喜欢这篇文章。
翻译自: https://medium.com/better-programming/6-swifty-ways-of-writing-code-f260286a0dbb
快速操作sql编写代码