Swift v3.0-PREVIEW-1 发布下载,Swift是一款为iOS和OS X 应用打造的新式编程语言,它吸取了C语言和Objective-C语言的精华,同时完美兼容C语言。Swift采用了安全编程模式,增加了许多现代语言的特性,使编程工作更加简单,灵活,有趣。一款重新设计的语言,再加上成熟并且深受欢迎的Cocoa框架和Cocoa Touch框架,Swift为软件开发工作带来了无限遐想。
特点:
- 从它的语法中能看到Java Objective-C JavaScript C# Python等语言的影子
- 语法简单、代码简洁、使用方便
- 可与Objective-C混合使用(相互调用)
- 提供了类似 Java 的名字空间(namespace)、泛型(generic)、运算对象重载(operator overloading)
- 让应用开发更简单、更快、更稳定
- 确保最终应用有着更好的质量
更新日志
-
SE-0093: Slice types now have a
baseproperty that allows public readonly access to their base collections. -
Nested generic functions may now capture bindings from the environment, for example:
func outer<T>(t: T) -> T { func inner<U>(u: U) -> (T, U) { return (t, u) } return inner(u: (t, t)).0 } -
Initializers are now inherited even if the base class or derived class is generic:
class Base<T> { let t: T init(t: T) { self.t = t } } class Derived<T> : Base<T> { // init(t: T) is now synthesized to call super.init(t: t) } -
SE-0081 "Move 'where' clause to end of declaration" is implemented, allowing you to write 'where' clauses after the signature for a declaration, but before its body. For example, before:
func anyCommonElements<T : SequenceType, U : SequenceType where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, _ rhs: U) -> Bool { ... }after:
func anyCommonElements<T : SequenceType, U : SequenceType>(lhs: T, _ rhs: U) -> Bool where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element { ... }The old form is still accepted for compatibility, but will eventually be rejected.
-
SE-0071: "Allow (most) keywords in member references" is implemented. This allows the use of members after a dot without backticks, e.g. "foo.default".
-
SE-0057: Objective-C lightweight generic classes are now imported as generic types in Swift. Because Objective-C generics are not represented at runtime, there are some limitations on what can be done with them in Swift:
-
If an ObjC generic class is used in a checked
as?,as!, oriscast, the generic parameters are not checked at runtime. The cast succeeds if the operand is an instance of the ObjC class, regardless of parameters.let x = NSFoo<NSNumber>(value: NSNumber(integer: 0)) let y: AnyObject = x let z = y as! NSFoo<NSString> // Succeeds
-
Swift subclasses can only inherit an ObjC generic class if its generic parameters are fully specified.
// Error: Can't inherit ObjC generic class with unbound parameter T class SwiftFoo1<T>: NSFoo<T> { } // OK: Can inherit ObjC generic class with specific parameters class SwiftFoo2<T>: NSFoo<NSString> { } -
Swift can extend ObjC generic classes, but the extensions cannot be constrained, and definitions inside the extension do not have access to the class's generic parameters.
extension NSFoo { // Error: Can't access generic param T func foo() -> T { return T() } } // Error: extension can't be constrained extension NSFoo where T: NSString { } -
Foundation container classes
NS[Mutable]Array,NS[Mutable]Set, andNS[Mutable]Dictionaryare still imported as nongeneric classes for the time being.
-
-
As part of the changes for SE-0055 (see below), the pointee types of imported pointers (e.g. the
idinid *) are no longer assumed to always be_Nullableeven if annotated otherwise. However, an implicit or explicit annotation of_Null_unspecifiedon a pointee type is still imported asOptional. -
SE-0055: The types
UnsafePointer,UnsafeMutablePointer,AutoreleasingUnsafeMutablePointer,OpaquePointer,Selector, andZone(formerlyNSZone) now represent non-nullable pointers, i.e. pointers that are nevernil. A nullable pointer is now represented usingOptional, e.g.UnsafePointer<Int>?For types imported from C, non-object pointers (such asint *) now have their nullability taken into account.One possible area of difficulty is passing a nullable pointer to a function that uses C variadics. Swift will not permit this directly, so as a workaround please use the following idiom to pass it as a pointer-sized integer value instead:
unsafeBitCast(nullablePointer, to: Int.self)
-
SE-0046 Function parameters now have consistent labelling across all function parameters. With this update the first parameter declarations will now match the existing behavior of the second and later parameters. This change makes the language simpler.
Functions that were written and called as follows
func foo(x: Int, y: Int) { } foo(1, y: 2) func bar(a a: Int, b: Int) { } bar(a: 3, b: 4)will now be written as (to achieve the same behavior):
func foo(_ x: Int, y: Int) {} foo(1, y: 2) func bar(a: Int, b: Int) {} bar(a: 3, b: 4) -
SE-0037 Comments are now treated as whitespace when determining whether an operator is prefix, postfix, or binary. For example, these now work:
if /*comment*/!foo { ... } 1 +/*comment*/2This also means that comments can no longer appear between a unary operator and its argument.
foo/* comment */! // no longer works
Any parse errors resulting from this change can be resolved by moving the comment outside of the expression.
-
SE-0031 The location of the inout attribute has been moved to after the
:and before the parameter type.
func foo(inout x: Int) {
}
will now be written as:
func foo(x: inout Int) {
}
-
SE-0053
letis no longer accepted as a parameter attribute for functions. The compiler provides a fixit to remove it from the function declaration. -
SE-0003
varis no longer accepted as a parameter attribute for functions. The compiler provides a fixit to create a shadow copy in the function body.
func foo(var x: Int) {
}
will now be written as:
func foo(x: Int) {
var x = x
}
-
The "none" members of imported NS_OPTIONS option sets are marked as unavailable when they are imported. Use [] to make an empty option set, instead of a None member.
-
SE-0043 landed, adding the ability to declare variables in multiple patterns in cases.
-
Renamification landed, so the Clang importer imports ObjC symbols substantially differently. Someone should expand on this point.
-
SE-0040 landed, changing attributes from using
=in parameters lists to using:, aligning with function call syntax. -
Generic typealiases are now supported, e.g.:
typealias StringDictionary<T> = Dictionary<String, T>
typealias IntFunction<T> = (T) -> Int
typealias MatchingTriple<T> = (T, T, T)
typealias BackwardTriple<T1, T2, T3> = (T3, T2, T1)
etc.
- The
@noescapeattribute has been extended to be a more general type attribute. You can now declare values of@noescapefunction type, e.g. in manually curried function signatures. You can now also declare local variables of@noescapetype, and use@noescapeintypealiases. For example, this is now valid code:
func apply<T, U>(@noescape f: T -> U,
@noescape g: (@noescape T -> U) -> U) -> U {
return g(f)
}
-
SE-0034 has renamed the
#linedirective (which resets the logical source location for diagnostics and debug information) to#sourceLocation. -
Curried function syntax has been removed, and now produces a compile-time error.
-
Generic signatures can now contain superclass requirements with generic parameter types, for example:
func f<Food : Chunks<Meat>, Meat : Molerat>(f: Food, m: Meat) {} -
Section markers are created in ELF binaries through special objects during link time. These objects allow for the deletion of
swift.ldand the use of non-BFD linkers. A new argument to swiftc is provided to select the linker used, and the gold linker is set as the default for arm-based platforms. -
Catch blocks in
rethrowsfunctions may nowthrowerrors. For example:func process(f: () throws -> Int) rethrows -> Int { do { return try f() } catch is SomeError { throw OtherError() } } -
Throwing closure arguments of a rethrowing function may now be optional. For example:
func executeClosureIfNotNil(closure: (() throws -> Void)?) rethrows { try closure?() } -
SE-0064 The Objective-C selectors for the getter or setter of a property can now be referenced with
#selector. For example:let sel1 = #selector(getter: UIView.backgroundColor) // sel1 has type Selector let sel2 = #selector(setter: UIView.backgroundColor) // sel2 has type Selector
-
SE-0062: A key-path can now be formed with
#keyPath. For example:person.valueForKeyPath(#keyPath(Person.bestFriend.lastName))
下载
下面关于Swift的内容你可能也喜欢:
Ubuntu 14.04.4 下安装 Swift 2.2.1 http://www.linuxidc.com/Linux/2016-05/131249.htm
Ubuntu 15.10安装部署Swift开发环境 http://www.linuxidc.com/Linux/2016-01/126995.htm
Swift 的变化:从 2.2 到 3.0 会带来什么 http://www.linuxidc.com/Linux/2015-12/126440.htm
Swift 正式开源,同时开源 Swfit 核心库和包管理器 http://www.linuxidc.com/Linux/2015-12/125847.htm
Apple Swift学习教程 http://www.linuxidc.com/Linux/2014-09/106420.htm
使用 Swift 构建一个 iOS 的邮件应用 http://www.linuxidc.com/Linux/2014-08/105542.htm
Swift 2.0开源化 http://www.linuxidc.com/Linux/2015-06/118594.htm
Linux下搭建Swift语言开发学习环境 http://www.linuxidc.com/Linux/2015-12/125983.htm
Swift 的详细介绍:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132305.htm
