In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the methods of writing code in Swift language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to write code in Swift language"?
Call willSet and didSet during initialization
In short, the willSet and didSet property watchers are not called the first time the property is initialized, but you can solve this problem by wrapping the initialization in a defer statement.
Use the Maps Security Open option
Usually we use if let or guardlet syntax to safely open options in Swift, but sometimes I wonder if there is another way to solve this problem, especially when expanding child properties. Our other option is to use the map operator to open the option.
The optional value contained in the map is evaluated only if it contains a value, ensuring that it is not nil.
In optional tuples, it is also convenient to unpack using maps. You can do this with the Swift language:
Func sampleTuple ()-> (String, String)? {return nil} let (a, b) = sampleTuple () .map {($0, $1)}? ("NA", "NA")
Use rethrow for powerful exception handling
You must be familiar with the throws keyword and use it a lot, but the rethrows function in Swift may not be used very often. A function representation declared with the rethrows keyword throws an error only if one of its function arguments throws.
That is, if the Closure parameter does not raise an error, we do not need to use other try methods when calling it, which greatly reduces the boilerplate code. As you can see in the following code, we don't have to put the same function that does not throw an exception into the do-catch block.
Use protocol extensions for default implementation
Swift protocols are powerful, but they do not allow you to specify a default implementation. This may be very convenient if you want to avoid rewriting methods. In addition, you can use the where clause in the extension to specify the protocol implementation only for certain constraints, but make sure you don't overuse the protocol extension in your code base.
Easily track changes in the dictionary
How do I make changes in the Swift dictionary (possibly for debugging)? The tedious method is achieved by distributing the content, but there is also a quick way to define a subscript on the object that holds the dictionary:
Using guardlet in different scenarios
The Guard let statement uses a fail-safe mechanism, where the Nil value ensures that you return to the upper level immediately. But in some cases, return is not the best way for us. For example, if you are in a for loop and only want continue or break, you can do this as follows:
At this point, I believe that everyone has a deeper understanding of the "Swift language coding methods", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.