刚才看了tinyfool的My First Impression of Swift，发现了一些语法错误，所以做一些力所能及的修改。不过因为本人英语水平有限，可能改得不是很好。
Just after Apple announced swift at WWDC 2014, I downloaded Xcode 6 beta 1, but since then I didn’t have time to really learn swift. Today, I watched the WWDC Session videos about swift (Introduction to Swift, Intermediate Swift, and Advanced Swift).
Why Apple makes Swift?
There are so many theories about this. In the WWDC keynote, Craig Federighi said, “Now Objective-C has served us so well for 20 years. We absolutely love it. But we have to ask ourselves the question: what would it be like if we had Objective-C without the baggage of C? “.
Objective-C is a great programming language, but it is still a very old language with so many baggages.
What is Objective-C without the C?
If we think this question from the angle of programming platform, I think the Objective-C platform includes:
1.Language: Objective-C language.
2.Development tools: Xcode, Interface builder and instrument.
3.Runtime library: Objective-C Runtime.
4.Framework: Cocoa and Cocoa touch.
Apple just changed the language, but still uses the same development tools, runtime library and frameworks. So Swift can change its syntax, but must have so many same structures with Objective-C underneath. So developers who familiar with Objective-C can learn easily and fast with their old experience.
In the WWDC session I noticed that the speaker said Swift has 3 key design points: safe, modern and power.
So after combing all these things together, I think the purpose of swift is trying to create a safe (strong type), modern (convenient, easy to learn and use) and powerful (doesn’t loss any functionalities from Objective-C) language which doesn’t have any baggages from C, and can be fully supported by Apple’s development infrastructures.
My first impression of the language
1. Swift uses strong type, but the syntax looks just like weak type.
A strong type language is faster and safer than a weak type language, but it also needs more lines of code and looks more complex. On the other hand, a weak type language is convenient, easy to learn and use, but much slower and not very safe. With swift, you can declare the type of variables explicitly like this:
var somestring: String = “this is a string”
You also can declare the type of variables implicitly like this:
var somestring = “this also is a string”
When you don’t explicitly declare the type of variables, the complier will automatically inference it.
2. Swift desn’t use single root class.
Java and Objective-C both use single root class. In Objective-C your classes have to inherit NSObject directly or indirectly. But Swift doesn’t use single root class, your classes can inherit any classes or just never inherit anything.
I think that’s because at the moment when Java and Objective-C are designed, there are so many functionalities that can not be implemented by the complier easily, so just put them to the root class. Now these functionalities can be implemented by the language.
3. You don’t need to use class member variables and properties separately
In Swift, all class member variables are properties, called “stored properties”. And if you want a property that has some setters and getters, it is a computed property.
4. You don’t need to alloc memory for your objects
Yeah!!!! The complier will do it for you.
5. Structures can have computed properties and methods
But structures still have some differences with classes:
- Structures can not inherit other structures.
- Structures are passed by value, while Classes are passed by reference.
6. Extensions can extent any classes, named types , or even built-in types
Extensions just like the category of Objective-C, which can be used to extend other classes, but in Swift, extensions can even extend built-in types.
It is convenient but may be dangerous.
7. Optional and Non-Optional type
The optional type gives you the ability to do the safety-checking of variables, making sure they are valid, and it provides a easy and convenient way to do it.
Ok, there are more and more syntax and features in the books and the WWDC session videos. But I just want to talk about these, which are easily to learn and may be different to other languages.