Advance Swift is a little bit too broad a title for a book, so I want to start the book by saying a little bit about what it’s going to be about.
Swift was about a year old at the time of writing. We started writing the book before the Swift 2.0 Beta was released, because we assumed Swift would improve considerably in its second year. There is probably no other language that has been as rapidly iterated into Beta in just a year as Swift, with a lot of developer effort.
So, one might wonder how to write native, Swift code. To some extent, the code in the standard library is worth learning from. But even the standard library has weeded out a number of older syntactic styles in the past year.
For programmers moving to Swift from another language, Swift will always be familiar to you, no matter what language you used. The low-level, bit-to-bit operations look very similar to C and are even as efficient as C, without producing many undefined behavior1. Lightweight closures and map and filter functions are familiar to Ruby developers. The Swift paradigm will be familiar to C++ engineers because it is a template in C++. The difference is that Swift writes type constraints in the definition rather than where they are actually called. Flexible use of higher-order functions and operator overloading gives your code a Haskell or F# feel. The @objc keyword allows you to use selectors and runtime as you would in OC.
Given these similarities, we can see that Swift is actually adopting some of the features of other languages. OC demonstration projects are almost always automatically converted to Swift style. Soon, you’ll see many books explaining design patterns in Swift instead of Java or C#. Then there will be a lot of individual tech blogs.
But as you get to know Swift better, frustration and disappointment follow. You may wonder why you can’t use associative types in protocol extensions when Java interfaces can. Why is the array not covariant 2 the way it should be. Why can’t I write the function 3. Sometimes the answer is that Swift doesn’t fully implement these features, but more often than not, either Swift provides its own alternative solution or you misunderstand Swift solution 4.
Swift is a complex language (and most languages are). But it hides its complexity so well that you can quickly get up to speed on Swift and start developing your app without knowing the difference between stereotype, overload, static, and dynamic distribution. You may never call the C library, or implement your own collection types. But sooner or later you need to know this, because you need to make your code elegant, readable, or fulfill certain requirements.
This book focuses on these advanced features, and we try to answer some of the most common questions we see on forums, such as “How do I implement this?” “And” Why did Swift do that? . Hopefully, after reading this book, you will not only understand the language basics of Swift, but also understand many of its advanced features, as well as have a better understanding of how Swift works. Knowing the facts in this book is a necessary but not sufficient condition for you to become an advanced Swift programmer.
# translator [1] : C developers should be familiar with UB, such as the overflow value of an int.
[2] : Covariance allows a coarse-grained interface to receive a more specific interface as a parameter.
[3] : a function is a function that makes the use of a class look like a function, such as overloading the () operator in C++.
[4] : There really is no translation into technical language. A bad example: C int is an integer, but if Swift int is really a string, then you can’t use an int any way you want.