This article is a piece of my "iOS development Daily Small Notes" series, which is recorded in the development work today, can be explained with a short article or a small demo demo small tips. The classification of the article, the content of the knowledge points may be very simple, or with a short code snippet can be achieved, but in my opinion they may give the user experience, code efficiency to some improvement, or have not been in touch with the technology, very happy to learn, put here to be a little bit. In fact, 90% of the role is to help themselves review, memory, review. If crossing feel too easy, too fragmented, then there can be two options: 1, the "iOS Inquiry" classification, the article there treatise, 2, in the comments in this article mercilessly spit groove, and then turn off the page! Thank!
The Swift language is a programming language (Http://en.wikipedia.org/wiki/Swift_ (Programming_language)) published by Apple on WWDC in 2014, which has been released for almost three months, Fengfengguangguang also got a lot of spotlights, a variety of derogatory analysis of the article on the internet there are many, I do not follow the blind analysis of the booing. In this essay, I will briefly say a little bit about my practical study. At present I also in the step by step study, later had more sentiment, also will share in my blog.
A: First of all, talk about the general feeling.
Swift is a language that, in the case of the high-level, personal feeling is higher than OBJC. The code style is more flexible, more chic and simple. If you're turning from objc like me, you'll find that many Swift's writing can be saved. The simplest example:
1 0.0
1 var slice = arr[0... 1]
1, ";" is no longer required after the statement To express the end of a sentence, 2, use "..." to denote an element in the "traverse" array;
OBJC, but is another scene: All cocoa touch interface can be long, the method name of each method must be described in the "hump" style is very clear, such as:
1 -(Nsarray *) Subarraywithrange: (nsrange) range;
So, in the vast majority of cases, the OBJC syntax is more straightforward, more verbose, and closer to the basic C language. And Swift can save the province. The result is that Swift is more "elegant", while others say OBJC is more "clear". What do you think?
Two: Code style provides more secure coding
1,swift provides two types to modify all variables, one is let, a constant is represented, and the other is Var, which represents a variable. Let modified constants, can only be assigned once, it can not be changed. and Var can be changed many times.
The array,dictionary in 2,swift is no longer the same as in OBJC, and is divided into mutable and immutable. Instead, use let to modify the immutable (including internal elements immutable), var modifier variable (including internal element variable)
3, declare a variable (or constant) that can display the specified type, or do not specify the type:
1 " message "
1 " message "
According to Apple's official statement, we should normally use implicit declarations, just like the second code. The compiler will determine the type of the assigned value "message" in its own way, so that the welcomemessage is determined to be more "optimized". And if you specify the type ": string", then the compiler will determine the value you assign, whether it is a string, or only a string. This is a kind of "safety test". (though not as advanced as you might think).
Three, a lot of pits!
The pit here is not about the swift language itself. Instead, the development version is constantly being updated (currently XCODE6-BETA5), causing beginners to focus on the latest and newest tutorials, including Apple's own official documentation. If you are looking at the Chinese version of the translation on GitHub, there will be some lag in the content.
Give two examples:
1:
1 /* * 1,.. < and ... 3 4 Let array5 = [100 , 200 , 300 5 for i 0 .. <2 6 println ( \ (Array5[i]) " " 7 }
This is one way to iterate over an array: "0...3" in the previous version represents from 0 to 3, including 3, "0..3" for 0 to 3, excluding 3. And now ".." was changed to ". < "," ... "will not change.
2:
1 /*2 * 1, part of the array removed by range "Slice" is the same as array, dictionary behavior, and the element in the slice obtained by range is no longer a pointer to the original array.3 *4 */5var Arr:array = [0,0,0]6var slice = arr[0...1]7arr[1] =18slice[0] =2
From the array type, remove a portion of the element. has undergone two changes. The current "final" feature is that, after the above operation, ARR will eventually be: [0, 1, 0], and Slice eventually:[2, 0]. And here the slice variable, is a struct of the slice type, not an array. Also, note that in Swift, Array,dictionary, or even int, string are structs, not classes.
The above two examples, is to explain: Although Swift has been released, but to the official IOS8 release, XCODE6 released this period of time, the change is relatively large, especially for the basic type, the basic operator of many changes, leading to like me the first time the developer will have some discomfort, Faint among the feeling "pit" much. However, another view is that by studying Apple's step-by-turn changes to Swift, there is a deeper understanding of what is inside swift and a particular way of mastering the swift language.
In combination with the above, Swift's two-day study gave me the feeling that elegance, high-end, drawing on the strengths of a variety of other languages, patiently pondering, enduring Apple's updates, and adapting as quickly as possible.
Finally share some blog posts and learning Links:
Meow's article: http://onevcat.com/2014/06/walk-in-swift/.
A few translation tutorials: http://www.tairan.com/archives/6837 personally feel very lost level, whether in translation, or Raywenderlich in the original. Is it really a cultural difference of things? But the explanation of playground is good, beginners can use playground play.
UML Articles: http://www.uml.org.cn/mobiledev/2014071110.asp http://www.uml.org.cn/mobiledev/2014070811.asp
The Swift programming Language, Apple's official Swift course, is in Chinese version http://numbbbbb.github.io/the-swift-programming-language-in-chinese/
Basically the last official tutorial is the one you need to take the time to learn.
"iOS Development Daily Small notes (vi)" Introduction to Swift language learning random Thoughts