in Apple

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 make 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 had to ask ourselves the question what would it be like we had Objective-C without the baggage of C? “.

Screenshot 2014-07-02 20.46.10

Objective-C is a great programming language,  but it still is a very old language have so many baggages.

What is Objective-C without the C?

If we think from the angle of programming platform, I think  Objective-C platform include:

  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.

Swift just change the language, but still use the same development tools, runtime library and framework. So Swift can change syntax, but must have so many same  structure with Objective-C underneath. So developer familiar with Objective-C can learn easy and fast, and use their old experiences.

At WWDC session I noticed speaker said swift have 3 key design point: safe, modern and power.

So combine all the above, I think design swift is try to create a new language, safe (strong type), modern (convenient, easy to learn and use), power (not loss any functionality from Objective-C), don’t have any baggage from C, and can be fully supported by Apple’s development infrastructures.

My first impression of the language

1. Swift use strong type, but syntax looks just like weak type.

Strong type language is faster and safer, but need more codes and looks more complex. Weak type language is convenience, easy to learn and use, but more slower and not very safe. At swift you can just  declare type of variables explicitly, like this:

var somestring: String = “this is a string”

You also can declare type of variables implicitly, like this:

var somestring = “this also is a string”

When you don’t explicitly declare type of variables, complier will automate inference type of variables.

2. Swift don’t use single root class.

Java and Objective-C both use single root class. In Objective-C your class normally need inherit from NSObject directly or indirectly. But Swift don’t use single root class, you can inherit from any class or just never inherit anything.

I think this because at the moment of design of Java and Objective-C, there is so many functionality can not very easily implement by complier, so just put them to the root class. Now this kind functionality can implement by language.

3. You don’t need use  class member variables and properties separately

In Swift all class member variables are properties, call stored properties. And if you want a property has some setter and getter, it is a computed property.

4. You don’t need alloc memory for you object

Yeah!!!!  Complier will do it for you.

5. Structure can have computed properties and methods

But structure still have some different with class:

  1. Structure can not inherit from other structure.
  2. Structure is passed by value, Class is passed by reference.

6. Extension can extent any classes, named types even build in types

Extension just like category of Objective-C, can use to extend other classes, but in Swift extension even can extent build in types.

It is convenience and may be dangerous.

Screenshot 2014-07-02 21.18.46

 

7. Optional and Non-Optional type

Optional type give you the ability to safe checking the variables, make sure they are valid, and it provide a easy and convenience way to do it.

End

Ok, there are more and more syntax and features in the books and the WWDC session videos. But I just want talk about these, which is easy to learn and may have some different with other languages.

This is already a long article…

Write a Comment

Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

15 Comments

  1. thanks for sharing this, dude. it really helps me.
    just curious, are you writing english blogs to get ready for life on the other side (of the wall).

  • Readers who shared this
  • Quora
  • Thank you!