Tối qua, 8/6/2015, Apple công bố Swift 2.0. Các thay đổi lớn của Swift 2.0: tối ưu về tốc độ hơn nữa, API mới xử lý lỗi, kiểm tra hàm, class có tồn tại hay không (tính năng này đã có từ Objective-C, rất cần thiết vì các hàm API trong iOS, MacOSX cũ (deprecated function) sẽ bị loại bỏ ở phiên bản mới hơn). Hệ thống các API của hệ điều hành sẽ thân thiện, nhuần nhuyến hơn đối với Swift 2.0 trước đó Swift 1.2 phải vay mượn, chắp vá khá nhiều từ thư viện viết bằng Objective-C.

Mở mã nguồn

Đây là thông tin mới nhưng đã dự đoán từ lâu. Objective-C sẽ chạy tốt trên iOS, MacOSX, những ứng dụng biên dịch ra mã nhị phân, nhưng không thể chạy tốt đối với ứng dụng web. Bằng chứng JavaScript là một ngôn ngữ lập trình kịch bản ban đầu thiết kế cho trình duyệt nay đã phổ biến ở cả server lẫn ứng dụng mobile, desktop. Bản thân Apple cũng phải hỗ trợ JavaScript chạy natively trong ứng dụng iOS. Vậy Swift 2.0 sẽ giúp Apple mở rộng địa bàn hoạt động của mình, không còn giới hạn trong các thiết bị phần cứng của riêng Apple nữa.

  • Mã nguồn của Swift sẽ mở theo kiểu cấp phép mã nguồn mở OSI (Open Source Intiative)
  • Đóng góp từ cộng đồng được chấp nhận và khuyến khích. Lập trình viên hãng thứ 3 có thể port Swift sang bất kỳ hệ điều hành nào họ muốn, hay viết web application bằng ngôn ngữ Swift
  • Tại thời điểm này, Apple hỗ trợ Swift trên OSX, iOS và Linux
  • Mã nguồn sẽ gồm có trình biên dịch Swift và các thư viện chuẩn, standard library

Học lập trình Swift trực tuyến

Tính năng mới

  • Xử lý lỗi, tương thích tốt hơn với NSError (kiểu thông báo lỗi của Objective-C)
  • Kiếm tra tính sẵn có (availablity checking): #availableblocks sẽ giúp lập trình viên viết đoạn mã riêng biệt cho từng phiên bản hệ điều hành đảm bảo tận dụng tính năng API mới và tương thích chạy được trên cả phiên bản hệ điều hành cũ. Apple cũng không thoát khỏi sự phân mảnh hệ điều hành. Haizz. Tuy nhiên đỡ hơn Google Android.
  • Protocol Extension: Ở Objective-C và Swift chúng ta đã biết đến Category, một hình thức mở rộng (extension) của Class. Này protocol extension cho phép mở rộng trong kỹ thuật lập trình hướng interface. Bản chất protocol trong Swift và Objective-C, tương đương với Interface trong Java. Swift 2.0 còn cho phép mở rộng các kiểu phổ biến. Lập trình viên giờ có thể viết hàm thành một chuỗi kiểu như intVariable.absolute().square()
  • SDK thuần Swift hơn. Ở Swift 1.x, Swift sử dụng SDK API chế biến từ Objective-C. Nay, Apple thiết kế lại SDK API thân thiện và thuần hơn với ngôn ngữ Swift. Hay thì có hay như chỉ khổ ông nào đã code dự án Swift giờ API đổi thì mất khá thời gian để sửa lại code, rồi dịch lại.

 

Techmaster mở cả hai khóa học lập trình iOS Objective-C (khóa 1 đến khóa 24, và khóa 30) và Swift (khóa 25, 26, 27, 28 và sắp tới là 29). Đối với lập trình viên cần đi xin việc ngay, chúng tôi khuyên các bạn nên học lập trình Objective-C vì dự án gia công rất nhiều. Làm không hết việc.  Swift còn mới, API, cấu trúc ngôn ngữ thay đổi khá nhiều. Các dự án lớn, dài hạn sẽ chưa sử dụng Swift. Nếu bạn là sinh viên trẻ, hoặc nhóm khởi nghiệp hoặc dev tự làm sản phẩm, bạn có thể học Swift. Lúc này bạn có nhiều thời gian và không gian để tận hưởng những tính năng mới của Swift.

Swift 2.0

Today at WWDC, we announced Swift 2.0. This new version has even better performance, a new error handling API, and first-class support for availability checking. And platform APIs feel even more natural in Swift with enhancements to the Apple SDKs.

Open Source

In addition to new features, the big news is that Apple will be making Swift open source later this year. We are all incredibly excited about this, and look forward to giving you a lot more information as the open source release gets nearer. Here is what we can tell you so far:

  • Swift source code will be released under an OSI-approved permissive license.
  • Contributions from the community will be accepted — and encouraged.
  • At launch we intend to contribute ports for OS X, iOS, and Linux.
  • Source code will include the Swift compiler and standard library.
  • We think it would be amazing for Swift to be on all your favorite platforms.

We are excited about the opportunities an open source Swift creates for our industry. Baked-in safety features combined with excellent speed mean it has the chance to dramatically improve software versus using C-based languages. Swift is packed with modern features, it’s fun to write, and we believe it will get used in a lot of places. Together, we have an exciting road ahead.

New Features

Swift 2.0 also includes a lot of new language features and refinements. Expect to see blog posts exploring the features in more depth, and be sure to watch for the WWDC sessions covering these topics all this week. A few of the new features include:

Error handling model: The new error handling model in Swift 2.0 will instantly feel natural, with familiar try,throw, and catch keywords. Best of all, it was designed to work perfectly with the Apple SDKs and NSError. In fact, NSError conforms to a Swift’s ErrorType. You’ll definitely want to watch the WWDC session on What’s New in Swift to hear more about it.

Availability: Using the latest SDKs ensures you get access to new features and information about platform changes. But sometimes you still need to target an older OS, and Swift makes doing so much easier and safer. The Swift compiler now shows an error when you use an API that is too new for your target OS, and #availableblocks can safely wrap lines of code to only run when on the right OS versions.

Protocol extensions: Swift is very focused on protocol-oriented development — there’s even a session on the topic at WWDC 2015. Swift 2.0 adds protocol extensions, and the standard library itself uses them extensively. Where you used to use global functions, Swift 2.0 now adds methods to common types so functions chain naturally, and your code is much more readable.

Swift-er SDKs: Swift 2 works even better with the Apple SDKs, thanks in part to two new features in Objective-C: nullability annotations and generics. The SDKs have been updated to annotate API that cannot return nil so you don’t need to use optionals as often. And with a true generics system employed by the SDKs you can more often preserve detailed type information in your Swift 2 code.

Learn more

This is just a taste of what’s new in Swift 2. You can download the latest version of The Swift Programming Language from the iBooks Store, and be sure to watch the WWDC sessions live streamed and on video throughout the week. And to read more, visit http://developer.apple.com/swift