Swift 2.0
来源:互联网 发布:swift编程指南 编辑:程序博客网 时间:2024/06/06 22:03
Error handling model
An advanced error handling model provides clear, expressive syntax for catching and throwing errors. It’s also easy to create your own custom error types so you can describe error cases with clear, meaningful names. The new error model was designed to work seamlessly with NSError and the Cocoa frameworks. Error handling code now looks like:
func loadData() throws { }func test() {do {try loadData()} catch {print(error)}}
Availability
You should always use the latest SDK to get access to the latest features, documentation, and API changes, but sometimes your app needs to run on an older OS. Swift 2.0 has built-in availability checking to make it easy to build the best possible app for each target OS version. The compiler will give you an error when using an API too new for your minimum target OS, and a new keyword lets you wrap blocks of code in a conditional version check to run only on specific OS releases.
Syntax improvements
New syntax features let you write more expressive code while improving consistency across the language. The SDKs have employed new Objective-C features such as generics and nullability annotation to make Swift code even cleaner and safer. Here is just a sampling of Swift 2.0 enhancements.
- Powerful control flow with do, guard, defer, and repeat
- Keyword naming rules unified for functions and methods
- Protocol extensions and default implementations
- Extended pattern matching to work in if clauses and for loops
Xcode 7 includes a powerful migrator that will help convert your application and playground code to work with the latest syntax improvements in Swift 2.0.
Open Source
Later this year Swift will be released as open source. Swift’s unique combination of elegance, power, and safety has the opportunity to move the entire software industry forward. It is exciting to imagine what we will build together.
Modern
Swift is the result of the latest research on programming languages, combined with decades of experience building Apple platforms. Named parameters brought forward from Objective-C are expressed in a clean syntax that makes APIs in Swift even easier to read and maintain. Inferred types make code cleaner and less prone to mistakes, while modules eliminate headers and provide namespaces. Memory is managed automatically, and you don’t even need to type semi-colons. All this modern thinking results in a language that is easy and fun to use.
extension String { var banana : String { let shortName = String(dropFirst(characters)) return "\(self) \(self) Bo B\(shortName) Banana Fana Fo F\(shortName)"}}let bananaName = "Jimmy".banana
Swift has many other features to make your code more expressive:
- Closures unified with function pointers
- Tuples and multiple return values
- Generics
- Fast and concise iteration over a range or collection
- Structs that support methods, extensions, and protocols
- Functional programming patterns, e.g., map and filter
- Native error handling using try / catch / throw
- Swift 2.0
- [swift 2.0] swift 2.0 之枚举类型
- Swift 2.0初探
- SWIFT 2.0 复习笔记
- Swift 2.0新特性
- Swift 2.0关键字guard
- Swift 2.0中的数据类型
- swift 2.0 数组
- swift 2.0 字典
- swift 2.0 元组
- swift 2.0 循环语句
- swift 2.0 函数
- swift 2.0 枚举
- swift 2.0 结构体
- swift 2.0 ? ! 到底是什么意思
- swift 2.0 延展 Extension
- swift 2.0 闭包
- Swift 2.0 异常处理
- NSClassFromString isKindOfClass
- Metaio in Unity3d 教程---Metaio扫描图片要求(Image Tracking)
- Android学习笔记01:Paint及Canvas的简单应用
- python编辑器对比和推荐
- 算法练习之DP 求LCM (最长公共子序列)
- Swift 2.0
- python模块 - re模块使用示例
- Android笔记之获取视频、图片缩略图
- Android自适应不同屏幕几种方法
- matlab中 tic,toc函数的用法
- Metaio in Unity3d 教程--- 三. 使用ID Marker作为扫描目标
- 扫盲:字符编码与字符集的区别(2010年10月9日修改)
- Ubuntu下安装PHP的mongodb扩展
- 首字母获取