The Apple upstart Swift before the world This Life

Source: Internet
Author: User
Tags gcd

absrtact: As a 70后 programmer, Chris has invented LLVM, Clang and Swift, what have you done?

Last week's business trip, this article has not been completed several times, often lying on the bed on the sleeping past. The weather in the south is hot and humid, let me feel extremely uncomfortable, until back to the north, the sky clear, air dry, let people produce a "liberated areas of the day is bright days" feeling, I even feel that their mobile phone, Pad and Mac are full of blood resurrection ... Until someone warns that they are the reason for charging and speed. Well, the South Hotel network is really slow ah ...

The following is the text:

..................

In the summer of 2010, Chris Lattner received an unusual task: developing the next generation of new programming languages for OS X and IOS platforms. At the time, Jobs was still in control of the huge Apple empire, and whether he was involved in the research and development program was unclear, but I think he should have known it at least because the plan was highly classified, and only a handful of people knew that the original executor was only one, and that was Chris Lattner.

Since 2010 's July, Chris has been thinking, designing, programming, and debugging endlessly, and he has spent nearly a year implementing most of the basic language structures, followed by other language experts who have continued to improve. By the year 2013, the project became a top priority for the Apple Development tool group, and Chris led his team through a new language of grammar design, compilers, runtimes, frameworks, Ides, and documentation, and shocked the world at the first appearance of the 2014 WWDC Conference. The name of the language is called: "swift".

According to Chris ' personal blog (http://nondot.org/sabre/) about Swift, the language is almost as much as he has done with his own power. The famous 70后 programmer is also the author of one of the main sponsors of the LLVM project and the author of the Clang compiler, and it can be said that Swift language is inextricably linked to Chris ' previous software work.

The same is 70后 programmer, the difference how big?

About the author

Chris can be said to be synonymous with gifted teenagers and good students, and he continues to pursue his master's degree in Computer Science and PhD after graduating from 2000 years. But Chris is not an otaku, learning his hand holding "Dragon book" Travel the world, to become morality comprehensive development of good students. After the publication of an article, the master's thesis is to put forward a complete set of run-time compiling ideas, laid the foundation for the development of LLVM, LLVM compiled framework under his leadership has been a considerable development, can be based on the GCC front-end compiler semantic analysis of the results of the compilation optimization and code generation , so Chris was already an industry-renowned compiler expert when he graduated in 2005.

Note: Many computer majors often ask me what to learn in college, and see Chris on the line. With the current level of scientific and technological information openness, if you work hard in your area of interest and with a little bit of talent, it's not a problem to be an expert in a particular field when you graduate. It's not like you're looking for a job all over the job.

When Chris graduated, it was Apple's burn-in for the compiler, because Apple's previous software products relied on the entire GCC compilation chain, and the open source community didn't buy Apple's accounts, and they didn't want to optimize and improve the GCC code specifically for Apple's requirements. So Apple Rage replaced the compiler backend directly with LLVM, and took Chris into his place. Chris came into the apple of the duck, not only to greatly optimize and improve LLVM to adapt to Objective-c's grammatical changes and performance requirements, but also launched the CLang project, designed to fully replace GCC. This goal has now been achieved, starting with OS X10.9 and XCode 5, LLVM+GCC has been replaced with Llvm+clang.

Swift is the third great project for Chris after LLVM and Clang!

About language

Before 2007, Objective-c has been the Apple's own yard of the minority language, but the outbreak of IOS mobile devices so that the popularity of the language to get the rocket jump speed, as of today, objective-c in the programming language ranking third, the lake, the three elder brother, Brother two elder brother is the C and Java such old language. At the same time, Apple in 2012 and 2013, the objective-c in a large-scale optimization and upgrading, adding a variety of modern language features, making the app easier to write, more programmers into the App Store ecosystem ...

In this case, why did Apple release a new language?

There is no standard answer to this question, but we can try to analyze it and talk about Apple's mental journey ...

Objective-c was invented by Brad Cox and Tom Love in the early 80, and in 1988 Jobs's Next company was granted the language of the programming language and developed the Objective-c language Library and NeXTSTEP development environment. Later Next was bought by Apple, Objective-c quirks became Apple's language. Pinch refers to a count, 30 years suddenly, OC also grew into a grandfather's generation of programming language.

In order to serve this "Grandpa", Apple painstakingly, the GCC's compilation chain replaced LLVM +GCC, and replaced by Llvm+clang, do syntax simplification, automatic reference counting, increase Blocks and GCD multithreading asynchronous processing technology ... Finally, OC was rejuvenated after 30 years, occupying the third position in the weapon spectrum. However, Apple is a little annoyed, OC improved for so many years, how to look like tinkering, with Blocks to achieve a Python-like lambda closure function, it seems always awkward. Well, since we've mastered LLVM and Clang, why don't we design a new language based on the current compiler? A language that belongs to Apple! You see, the neighbor Google home is called Go children are not playing?

So Swift was born ...

The truth, of course, may be the slow-moving Joe POPs, who pulls Chris aside and says:

"i want to be swift to......"

"All right, you don't have to stop, just want swift, I'll make you a go"

So Swift was born ...

Grammar

Swift is a absorbing modern language, in the process of design, Chris referred to the characteristics of excellent language such as objective-c,rust,haskell,ruby,python,c#, and eventually formed the current characteristics of Swift's grammar. After reading the official tutorials and doing some code experiments, I feel like I'm going to love this language, and here's a little bit more in-depth content that you need to learn more about.

1, Swift is the programming language for Cocoa and Cocoa Touch, the compiled language, the production environment code will need to LLVM compile cost code to execute, but Swift also has a lot of dynamic language syntax features and interactive way.

2. Swift is a type-safe language that helps developers to clearly control the type of values in a code snippet. If you expect to enter a string, the type-safe feature prevents the developer from mistakenly passing an integer to it. All this allows developers to find and fix bugs earlier.

3, support a variety of high-level language features, including closures, generics, object-oriented, multiple return values, type interfaces, tuples, collections and so on.

4, Swift can be mixed programming with OBJECTIVE-C, but the code belongs to different files.

5, full Unicode support, you can even use a?? As the variable name, do the following:

Let?? = "Big Dog Pineapple"

For n in?? {

println (N)

}

The console will output "Big dog pineapple" four words.

6, the programming statement canceled most of the language use ";" Delimiter, a semicolon is required only if you write multiple statements on one line.

7, many people simply read the SWIFT data type, think Swift does not resemble Set, List such data structure, in fact Swift provides two kinds of Collection data type: Array and dictionary (Dictionary), Expressions of two data types are identified by brackets. Where arrays can store variables of any type, or you can force declarations to store variables of the same type. The array also provides a similar Set function, where you can modify, append, replace, and delete elements of the data. In addition, Swift provides multiple return values for the function support functions of tuple (tuple).

8, Swift does not provide explicit pointers, parameter passing is divided into value types and reference types according to the different data types, value delivery is a memory copy, and the reference pass is ultimately passed a pointer to the original object. This is similar to the Java parameter passing. It is important to note that the arrays and dictionaries in Swift are struct (struct), but they are handled differently during parameter passing, the default array is a reference pass, and Dictionary is a value pass. In Java, because arrays and maps are objects, the pointers are passed.

In Swift, if you do not want to pass an array reference, you can use the copy () method to copy it first, or you can use Unshare () to indicate that this variable does not pass a pointer.

9, closures, Swift finally provides an elegant closure solution, such as in the sorting function sort in the function transfer:

Let names = ["D", "B", "R", "C", "A"]

Func Backwards (s1:string, s2:string), Bool {

return s1 > S2

}

var rnames = sort (names, backwards)

In fact, a simpler formulation is:

var rnames = sort (["D", "B", "R", "C", "A"]) {$ > $}

10. Optional variables (Optional) are introduced primarily to deal with situations where a variable may or may not be nil, and this situation exists in many high-level languages. For example, if you want to use String's ToInt method to convert a string to Int type, but you do not know whether the conversion is normal, this time the system will return an optional variable, if the conversion succeeds in returning to normal, the conversion fails to return nil, as follows:

Let str = "123A"

Let NN = Str.toint ()

This is the NN is optional variable, to get the value of NN, can be judged by the if and by appending an exclamation point to obtain the value of the variable, as follows:

If nn {

println (nn!)

}

The introduction of optional variables solves most exceptions that need to be explicitly handled, and this part of the work is also thrown to the compiler. To learn more about the use of optional variables, please read Apple's official documentation.

11. Nil in Swift is different from nil in objective-c. In Objective-c, nil is a pointer to a nonexistent object, whereas in Swift, nil is not a pointer, it indicates that a particular type of value does not exist. All types of optional values can be set to nil, not just the object type.

12. Swift does not support async and multicore from a language level, but it can implement asynchronous functionality directly in Swift with the GCD API. Also do not see the exception handling mechanism of Swift, there may be optional variables, the use of the exception is very few.

For grammar-related content, write these points first.

To recommend a Wang Wei (@onevcat) written "Walking in the world of Swift", deep reading will have a harvest. http://onevcat.com/2014/06/walk-in-swift/

Basically, Swift is definitely not a toy language, but an industrial-grade programming language that can be accepted by the public. Believe that in time, Swift will shine in APP development.

Performance

The performance of Swift on the WWDC is still very surprising, in the complex object ordering, OC performance is 2.8 times times python, Swift is Python 3.9 times times, in the implementation of RC4 encryption algorithm, OC performance is 127 times times python, Sw IFT is 220 times times the size of Python. In short Python in a deep pit in the knee arrow, OC is not good to where to go, and Swift, is fast AH is fast!

I don't quite understand that, first of all, the language level benchmarks shown on WWDC are too simplistic, and OC and Swift are executed by LLVM compiled cost code, and the theory of Swift optimization can also be applied to OC, but Swift is a little faster than OC. Point, did LLVM individually optimize for Swift? I'm not aware of that.

Of course, there are more serious programmers, he in the first time for the loop, increment, array, string splicing and other functions were tested, found that Swift performance than OC or a little bit less (http://www.splasmata.com/?p=2798).

Whether these test data are accurate, I think performance is the least we need to worry about, Apple has mastered all aspects of the language, from the bottom of the compiler framework to the compiler to language design, optimization of the road is just beginning, we just give this new language a little patience.

The code is the income (Playground)

For developers, Playground is the biggest highlight of the WWDC. The ability to preview the output in real time while encoding is the dream of every developer, and this time Apple offers this benefit for everyone.

Playground not only realizes the interactive programming supported by many scripting languages, but also provides the functions of console output, real-time graphics image, time line (timeline) variable tracking and so on, the developer can see the real-time running result of the code, and can read the change of the value of a variable in the code fragment according to the time line. This is really great!

When I first saw this feature, I even thought that every Swift file could have a real-time code preview based on Playground, and after reading the document, it was only possible to implement the above functionality in the Playground file provided by XCode. It seems that Playground, as its name implies, is just a place for developers to play with code.

Of course not just play, we can do these things based on Playground:

1. Learning: Learn swift through Playground, make swift tutorials to realize interactive learning, and also train other beginners.

2, Code development: Execution algorithm program, quickly see the results of the algorithm, tracking variables, execute the drawing program, immediately see the image results, timely adjustment. Execute common code to see how variables change.

3, Experimental code: No need to create a project, directly open a separate playground file can write code, try to invoke the new API.

For Playground, the designer Chris describes this: the Playground function has poured into my personal heart and passion, and I hope that the new programming language will be more interactive, friendlier and more interesting ... We want to redefine "How to teach computer science" through this language! 」

Getting Started with Swift

As a new language, Swift is clearly positioned to attract more developers to Apple's software ecosystem, develop richer apps for IOS and OS X and, if you're a developer of the app Store, recommend learning and mastering this new Apple language as early as possible. For most new things, the sooner you get involved, the more profit you get. If you're a web-related developer, instead of waiting for Swift to add features to Web development, learn about Go language Web programming.

How do I start Swift?

1, download the Xcode6-beta version.

2. Download Apple's official Swift programming language E-book (https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11), English version: http://yuedu.baidu.com/ebook/6f6c3b1ef01dc281e43af000. Read.

3. Download WWDC Swift's Session video and PDF. See.

4. Create a Swfit language project based on Xcode6, create a playground in the project, and debug play in it.

5, according to the official guidedtour.playground to learn Swift grammar characteristics. : https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/ GuidedTour.playground.zip

6, familiar with the basic grammatical characteristics, mixed with OC, and Cocoa and Cocoa touch interaction, debugging and other functions, you can build your first swift App.

..................

It can be said that Swift is the most attention-grabbing new language I have ever seen, and the media and developers have voluminous and discussed swift in a few days, and the English manual has been quickly translated into Chinese, even though the other big guy in the lake has launched the Go in 2009. Language, there is no such a vast momentum. Of course, this is related to the positioning of the Go language, as a system-level server-side language, the developer's choice is too big, if Google launched Go is to replace the Java development Android APP, it may be completely different.

After WWDC2014, Apple has fully demonstrated a software company's innovation capabilities and technical details, whether it is the operating system, programming language, or application development, Apple is ready to concentrate on static, poised to go. As a developer, all we have to do is Write the code, change the world, and look forward to the next harvest season!

The Apple upstart Swift before the world This Life

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.