Swift v3.0-PREVIEW-1 发布
jopen 8年前
<p style="text-align: center;"><img alt="" src="https://simg.open-open.com/show/56f1a0b8406f370bf2d900c10dd5d82e.png" /></p> <p>Swift是一款为iOS和OS X 应用打造的新式编程语言,它吸取了C语言和Objective-C语言的精华,同时完美兼容C语言。Swift采用了安全编程模式,增加了许多现代语言的特性,使编程工作更加简单,灵活,有趣。一款重新设计的语言,再加上成熟并且深受欢迎的Cocoa框架和Cocoa Touch框架,Swift为软件开发工作带来了无限遐想。</p> <p>特点:</p> <ul> <li>从它的语法中能看到Java Objective-C JavaScript C# Python等语言的影子</li> <li>语法简单、代码简洁、使用方便</li> <li>可与Objective-C混合使用(相互调用)</li> <li>提供了类似 Java 的名字空间(namespace)、泛型(generic)、运算对象重载(operator overloading)</li> </ul> <ul> <li>让应用开发更简单、更快、更稳定</li> <li>确保最终应用有着更好的质量</li> </ul> <h2>更新日志</h2> <ul> <li> <p><a href="/misc/goto?guid=4958991366111356949">SE-0093</a>: Slice types now have a <code>base</code> property that allows public readonly access to their base collections.</p> </li> <li> <p>Nested generic functions may now capture bindings from the environment, for example:</p> <pre> func outer<T>(t: T) -> T { func inner<U>(u: U) -> (T, U) { return (t, u) } return inner(u: (t, t)).0 }</pre> </li> <li> <p>Initializers are now inherited even if the base class or derived class is generic:</p> <pre> 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) }</pre> </li> <li> <p><a href="/misc/goto?guid=4958991366228180762">SE-0081</a> "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:</p> <pre> func anyCommonElements<T : SequenceType, U : SequenceType where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, _ rhs: U) -> Bool { ... }</pre> <p>after:</p> <pre> func anyCommonElements<T : SequenceType, U : SequenceType>(lhs: T, _ rhs: U) -> Bool where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element { ... }</pre> <p>The old form is still accepted for compatibility, but will eventually be rejected.</p> </li> <li> <p><a href="/misc/goto?guid=4958991366337564850">SE-0071</a>: "Allow (most) keywords in member references" is implemented. This allows the use of members after a dot without backticks, e.g. "foo.default".</p> </li> <li> <p><a href="/misc/goto?guid=4958991366447118417">SE-0057</a>: 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:</p> <ul> <li> <p>If an ObjC generic class is used in a checked <code>as?</code>, <code>as!</code>, or <code>is</code> cast, the generic parameters are not checked at runtime. The cast succeeds if the operand is an instance of the ObjC class, regardless of parameters.</p> <pre> let x = NSFoo<NSNumber>(value: NSNumber(integer: 0)) let y: AnyObject = x let z = y as! NSFoo<NSString> // Succeeds</pre> </li> <li> <p>Swift subclasses can only inherit an ObjC generic class if its generic parameters are fully specified.</p> <pre> // 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> { }</pre> </li> <li> <p>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.</p> <pre> 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 { }</pre> </li> <li> <p>Foundation container classes <code>NS[Mutable]Array</code>, <code>NS[Mutable]Set</code>, and <code>NS[Mutable]Dictionary</code> are still imported as nongeneric classes for the time being.</p> </li> </ul> </li> <li> <p>As part of the changes for SE-0055 (see below), the <em>pointee</em> types of imported pointers (e.g. the <code>id</code> in <code>id *</code>) are no longer assumed to always be <code>_Nullable</code> even if annotated otherwise. However, an implicit or explicit annotation of<code>_Null_unspecified</code> on a pointee type is still imported as <code>Optional</code>.</p> </li> <li> <p><a href="/misc/goto?guid=4958991366551349628">SE-0055</a>: The types <code>UnsafePointer</code>, <code>UnsafeMutablePointer</code>, <code>AutoreleasingUnsafeMutablePointer</code>, <code>OpaquePointer</code>,<code>Selector</code>, and <code>Zone</code> (formerly <code>NSZone</code>) now represent non-nullable pointers, i.e. pointers that are never <code>nil</code>. A nullable pointer is now represented using <code>Optional</code>, e.g. <code>UnsafePointer<Int>?</code> For types imported from C, non-object pointers (such as <code>int *</code>) now have their nullability taken into account.</p> <p>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:</p> <pre> unsafeBitCast(nullablePointer, to: Int.self)</pre> </li> <li> <p><a href="/misc/goto?guid=4958991366664479309">SE-0046</a> 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.</p> <p>Functions that were written and called as follows</p> <pre> func foo(x: Int, y: Int) { } foo(1, y: 2) func bar(a a: Int, b: Int) { } bar(a: 3, b: 4)</pre> <p>will now be written as (to achieve the same behavior):</p> <pre> func foo(_ x: Int, y: Int) {} foo(1, y: 2) func bar(a: Int, b: Int) {} bar(a: 3, b: 4)</pre> </li> <li> <p><a href="/misc/goto?guid=4958991366770148386">SE-0037</a> Comments are now treated as whitespace when determining whether an operator is prefix, postfix, or binary. For example, these now work:</p> <pre> if /*comment*/!foo { ... } 1 +/*comment*/2</pre> <p>This also means that comments can no longer appear between a unary operator and its argument.</p> <pre> foo/* comment */! // no longer works</pre> <p>Any parse errors resulting from this change can be resolved by moving the comment outside of the expression.</p> </li> <li> <p><a href="/misc/goto?guid=4958991366870210926">SE-0031</a> The location of the inout attribute has been moved to after the <code>:</code> and before the parameter type.</p> </li> </ul> <pre> func foo(inout x: Int) { }</pre> <p>will now be written as:</p> <pre> func foo(x: inout Int) { }</pre> <ul> <li> <p><a href="/misc/goto?guid=4958991366981365821">SE-0053</a> <code>let</code> is no longer accepted as a parameter attribute for functions. The compiler provides a fixit to remove it from the function declaration.</p> </li> <li> <p><a href="/misc/goto?guid=4958991367088176493">SE-0003</a> <code>var</code> is no longer accepted as a parameter attribute for functions. The compiler provides a fixit to create a shadow copy in the function body.</p> </li> </ul> <pre> func foo(var x: Int) { }</pre> <p>will now be written as:</p> <pre> func foo(x: Int) { var x = x }</pre> <ul> <li> <p>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.</p> </li> <li> <p><a href="/misc/goto?guid=4958991367197307794">SE-0043</a> landed, adding the ability to declare variables in multiple patterns in cases.</p> </li> <li> <p>Renamification landed, so the Clang importer imports ObjC symbols substantially differently. <em>Someone should expand on this point.</em></p> </li> <li> <p><a href="/misc/goto?guid=4958991367312207541">SE-0040</a> landed, changing attributes from using <code>=</code> in parameters lists to using <code>:</code>, aligning with function call syntax.</p> </li> <li> <p>Generic typealiases are now supported, e.g.:</p> </li> </ul> <pre> 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)</pre> <p>etc.</p> <ul> <li>The <code>@noescape</code> attribute has been extended to be a more general type attribute. You can now declare values of<code>@noescape</code> function type, e.g. in manually curried function signatures. You can now also declare local variables of<code>@noescape</code> type, and use <code>@noescape</code> in <code>typealiases</code>. For example, this is now valid code:</li> </ul> <pre> func apply<T, U>(@noescape f: T -> U, @noescape g: (@noescape T -> U) -> U) -> U { return g(f) }</pre> <ul> <li> <p><a href="/misc/goto?guid=4958991367422695763">SE-0034</a> has renamed the <code>#line</code> directive (which resets the logical source location for diagnostics and debug information) to <code>#sourceLocation</code>.</p> </li> <li> <p>Curried function syntax has been removed, and now produces a compile-time error.</p> </li> <li> <p>Generic signatures can now contain superclass requirements with generic parameter types, for example:</p> <pre> func f<Food : Chunks<Meat>, Meat : Molerat>(f: Food, m: Meat) {}</pre> </li> <li> <p>Section markers are created in ELF binaries through special objects during link time. These objects allow for the deletion of <code>swift.ld</code> and 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.</p> </li> <li> <p>Catch blocks in <code>rethrows</code> functions may now <code>throw</code> errors. For example:</p> <pre> func process(f: () throws -> Int) rethrows -> Int { do { return try f() } catch is SomeError { throw OtherError() } }</pre> </li> <li> <p>Throwing closure arguments of a rethrowing function may now be optional. For example:</p> <pre> func executeClosureIfNotNil(closure: (() throws -> Void)?) rethrows { try closure?() }</pre> </li> <li> <p><a href="/misc/goto?guid=4958991367516205118">SE-0064</a> The Objective-C selectors for the getter or setter of a property can now be referenced with <code>#selector</code>. For example:</p> <pre> let sel1 = #selector(getter: UIView.backgroundColor) // sel1 has type Selector let sel2 = #selector(setter: UIView.backgroundColor) // sel2 has type Selector</pre> </li> <li> <p><a href="/misc/goto?guid=4958991367624981437">SE-0062</a>: A key-path can now be formed with <code>#keyPath</code>. For example:</p> <pre> person.valueForKeyPath(#keyPath(Person.bestFriend.lastName))</pre> </li> </ul> <h2>下载</h2> <ul> <li><a href="/misc/goto?guid=4958991367736189761" rel="nofollow"><strong>Source code</strong> (zip)</a></li> <li><a href="/misc/goto?guid=4958991367848846701" rel="nofollow"><strong>Source code</strong> (tar.gz)</a></li> <li><a href="/misc/goto?guid=4958991367957275304">官方下载</a></li> </ul>