When we see a string literal used to initializer a Selector, and that string literal names the selector for the getter or setter of an Objective-C property, suggest #selector(getter: ...) or This completes SE-0064. Big thanks for Alex Hoppen (@ahoppen) for his contributions here.
181 KiB
Note: This is in reverse chronological order, so newer entries are added to the top.
Swift 3.0
-
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] (https://github.com/apple/swift-evolution/blob/master/proposals/0046-first-label.md) 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 worksAny 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
Swift 2.2
-
Associated types in protocols can now be specified with a new
associatedtypedeclaration, to replace the use oftypealias:protocol P { associatedtype Ty }The
typealiaskeyword is still allowed (but deprecated and produces a warning) in Swift 2.2. This warning will become an error in Swift 3.0. -
Curried function syntax has been deprecated, and is slated to be removed in Swift 3.0.
-
The
++and--operators have been deprecated, and are slated to be removed in Swift 3.0. As a replacement, please usex += 1on integer or floating point types, andx = x.successor()on Index types. -
The implicit tuple splat behavior in function application has been deprecated and will be removed in Swift 3.0. For example, this code:
func foo(a : Int, b : Int) { ... } let x = (1, b: 2) foo(x) // Warning, deprecated.should move to being written as:
foo(x.0, x.b)For more information and rationale, see SE-0029.
-
New
#file,#line,#column, and#functionexpressions have been introduced to replace the existing__FILE__,__LINE__,__COLUMN__, and__FUNCTION__symbols. The__FILE__-style symbols have been deprecated, and will be removed in Swift 3.0. -
The operator identifier lexer grammar has been revised to simplify the rules for operators that start with a dot ("."). The new rule is that an operator that starts with a dot may contain other dots in it, but operators that start with some other character may not contain dots. For example:
x....foo --> "x" "...." "foo" x&%^.foo --> "x" "&%^" ".foo"This eliminates a special case for the
..<operator, folding it into a simple and consistent rule. -
The "C-style for loop", which is spelled
for init; comparison; increment {}has been deprecated and is slated for removal in Swift 3.0. See SE-0007 for more information. -
Three new doc comment fields, namely
- keyword:,- recommended:and- recommendedover:, allow Swift users to cooperate with code completion engine to deliver more effective code completion results. The- keyword:field specifies concepts that are not fully manifested in declaration names.- recommended:indicates other declarations are preferred to the one decorated; to the contrary,- recommendedover:indicates the decorated declaration is preferred to those declarations whose names are specified. -
Designated class initializers declared as failable or throwing may now return
nilor throw an error, respectively, before the object has been fully initialized. For example:class Widget : Gadget { let complexity: Int init(complexity: Int, elegance: Int) throws { if complexity > 3 { throw WidgetError.TooComplex } self.complexity = complexity try super.init(elegance: elegance) } } -
All slice types now have
removeFirst()andremoveLast()methods. -
ArraySlice.removeFirst()now preserves element indices. -
Global
anyGenerator()functions have been changed into initializers onAnyGenerator, making the API more intuitive and idiomatic. They have been deprecated in Swift 2.2, and will be removed in Swift 3.0. -
Closures appearing inside generic types and generic methods can now be converted to C function pointers, as long as no generic type parameters are referenced in the closure's argument list or body. A conversion of a closure that references generic type parameters now produces a diagnostic instead of crashing.
(rdar://problem/22204968)
-
Anonymously-typed members of C structs and unions can now be accessed from Swift. For example, given the following struct 'Pie', the 'crust' and 'filling' members are now imported:
struct Pie { struct { bool crispy; } crust; union { int fruit; } filling; }Since Swift does not support anonymous structs, these fields are imported as properties named
crustandfillinghaving nested types namedPie.__Unnamed_crustandPie.__Unnamed_filling.(rdar://problem/21683348)
-
Argument labels and parameter names can now be any keyword except
var,let, orinout. For example:NSURLProtectionSpace(host: "somedomain.com", port: 443, protocol: "https", realm: "Some Domain", authenticationMethod: "Basic")would previously have required
protocolto be surrounded in back-ticks. For more information, see SE-0001. -
Tuples (up to arity 6) whose elements are all
ComparableorEquatablenow implement the full set of comparison/equality operators. The comparison operators are defined in terms of lexicographical order. See SE-0015 for more information.
-
The
@objc(SomeName)attribute is now supported on enums and enum cases to rename the generated Objective-C declaration.(rdar://problem/21930334)
-
When referencing a function or initializer, one can provide the complete name, including argument labels. For example:
let fn1 = someView.insertSubview(_:at:) let fn2 = someView.insertSubview(_:aboveSubview:) let buttonFactory = UIButton.init(type:)For more information, see SE-0021.
-
There is a new build configuration function,
#if swift(>=x.y), which tests if the current Swift language version is at leastx.y. This allows you to conditionally compile code for multiple language versions in the same file, even with different syntax, by deactivating parsing in inactive code blocks. For example:#if swift(>=2.2) // Only this code will be parsed in Swift 3.0 func foo(x: Int) -> (y: Int) -> () {} #else // This code is ignored entirely. func foo(x: Int)(y: Int) {} #endifFor more information, see SE-0020.
-
The Objective-C selector of a Swift method can now be determined directly with the #selector expression, e.g.,:
let sel = #selector(insertSubview(_:aboveSubview:)) // sel has type SelectorAlong with this change, the use of string literals as selectors has been deprecated, e.g.,
let sel: Selector = "insertSubview:aboveSubview:"Generally, such string literals should be replaced with uses of
#selector, and the compiler will provide Fix-Its that use#selector. In cases where this is not possible (e.g., when referring to the getter of a property), one can still directly construct selectors, e.g.:let sel = Selector("propertyName")Note that the compiler is now checking the string literals used to construct Selectors to ensure that they are well-formed Objective-C selectors and that there is an
@objcmethod with that selector.
2015-09-17 [Xcode 7.1, Swift 2.1]
-
Enums imported from C now automatically conform to the
Equatableprotocol, including a default implementation of the==operator. This conformance allows you to use C enum pattern matching in switch statements with no additional code. (17287720) -
The
NSNumber.unsignedIntegerValueproperty now has the typeUIntinstead ofInt, as do other methods and properties that use theNSUIntegertype in Objective-C and whose names containunsigned... Most other uses ofNSUIntegerin system frameworks are imported asIntas they were in Xcode 7. (19134055) -
Field getters and setters are now created for named unions imported from C. In addition, an initializer with a named parameter for the field is provided. For example, given the following Objective-C
typedef:typedef union IntOrFloat { int intField; float floatField; } IntOrFloat;Importing this
typedefinto Swift generates the following interface:struct IntOrFloat { var intField: Int { get set } init(intField: Int) var floatField: Float { get set } init(floatField: Float) }(19660119)
-
Bitfield members of C structs are now imported into Swift. (21702107)
-
The type
dispatch_block_tnow refers to the type@convention(block) () -> Void, as it did in Swift 1.2. This change allows programs usingdispatch_block_createto work as expected, solving an issue that surfaced in Xcode 7.0 with Swift 2.0.Note: Converting to a Swift closure value and back is not guaranteed to preserve the identity of a
dispatch_block_t. (22432170) -
Editing a file does not trigger a recompile of files that depend upon it if the edits only modify declarations marked private. (22239821)
-
Expressions interpolated in strings may now contain string literals. For example,
My name is \(attributes["name"]!)is now a valid expression. (14050788) -
Error messages produced when the type checker cannot solve its constraint system continue to improve in many cases.
For example, errors in the body of generic closures (for instance, the argument closure to
map) are much more usefully diagnosed. (18835890) -
Conversions between function types are supported, exhibiting covariance in function result types and contravariance in function parameter types. For example, it is legal to assign a function of type
Any -> Intto a variable of typeString -> Any. (19517003)
2015-09-17 [Xcode 7.0, Swift 2.0]
Swift Language Features
-
New
deferstatement. This statement runs cleanup code when the scope is exited, which is particularly useful in conjunction with the new error handling model. For example:func xyz() throws { let f = fopen("x.txt", "r") defer { fclose(f) } try foo(f) // f is closed if an error is propagated. let f2 = fopen("y.txt", "r") defer { fclose(f2) } try bar(f, f2) // f2 is closed, then f is closed if an error is propagated. } // f2 is closed, then f is closed on a normal path(17302850)
-
Printing values of certain
enumtypes shows the enumcaseand payload, if any. This is not supported for@objcenums or certain enums with multiple payloads. (18334936) -
You can specify availability information on your own declarations with the
@available()attribute.For example:
@available(iOS 8.0, OSX 10.10, *) func startUserActivity() -> NSUserActivity { ... }This code fragment indicates that the
startUserActivity()method is available on iOS 8.0+, on OS X v10.10+, and on all versions of any other platform. (20938565) -
A new
@nonobjcattribute is introduced to selectively suppress ObjC export for instance members that would otherwise be@objc. (16763754) -
Nongeneric classes may now inherit from generic classes. (15520519)
-
Public extensions of generic types are now permitted.
public extension Array { … }(16974298)
-
Enums now support multiple generic associated values, for example:
enum Either<T, U> { case Left(T), Right(U) }(15666173)
-
Protocol extensions: Extensions can be written for protocol types. With these extensions, methods and properties can be added to any type that conforms to a particular protocol, allowing you to reuse more of your code. This leads to more natural caller side "dot" method syntax that follows the principle of "fluent interfaces" and that makes the definition of generic code simpler (reducing "angle bracket blindness"). (11735843)
-
Protocol default implementations: Protocols can have default implementations for requirements specified in a protocol extension, allowing "mixin" or "trait" like patterns.
-
Availability checking. Swift reports an error at compile time if you call an API that was introduced in a version of the operating system newer than the currently selected deployment target.
To check whether a potentially unavailable API is available at runtime, use the new
#available()condition in an if or guard statement. For example:if #available(iOS 8.0, OSX 10.10, *) { // Use Handoff APIs when available. let activity = NSUserActivity(activityType:"com.example.ShoppingList.view") activity.becomeCurrent() } else { // Fall back when Handoff APIs not available. }(14586648)
-
Native support for C function pointers: C functions that take function pointer arguments can be called using closures or global functions, with the restriction that the closure must not capture any of its local context. For example, the standard C qsort function can be invoked as follows:
var array = [3, 14, 15, 9, 2, 6, 5] qsort(&array, array.count, sizeofValue(array[0])) { a, b in return Int32(UnsafePointer<Int>(a).memory - UnsafePointer<Int>(b).memory) } print(array)(16339559)
-
Error handling. You can create functions that
throw,catch, and manage errors in Swift.Using this capability, you can surface and deal with recoverable errors, such as "file-not-found" or network timeouts. Swift's error handling interoperates with
NSError. (17158652) -
Testability: Tests of Swift 2.0 frameworks and apps are written without having to make internal routines public.
Use
@testable import {ModuleName}in your test source code to make all public and internal routines usable. The app or framework target needs to be compiled with theEnable Testabilitybuild setting set toYes. TheEnable Testabilitybuild setting should be used only in your Debug configuration, because it prohibits optimizations that depend on not exporting internal symbols from the app or framework. (17732115) -
if statements can be labeled, and labeled break statements can be used as a jump out of the matching if statement.
An unlabeled break does not exit the if statement. It exits the enclosing loop or switch statement, or it is illegal if none exists. (19150249)
-
A new
x?pattern can be used to pattern match against optionals as a synonym for.Some(x). (19382878) -
Concatenation of Swift string literals, including across multiple lines, is now a guaranteed compile-time optimization, even at
-Onone. (19125926) -
Nested functions can now recursively reference themselves and other nested functions. (11266246)
-
Improved diagnostics:
- A warning has been introduced to encourage the use of let instead of var when appropriate.
- A warning has been introduced to signal unused variables.
- Invalid mutation diagnostics are more precise.
- Unreachable switch cases cause a warning.
- The switch statement "exhaustiveness checker" is smarter. (15975935,20130240)
-
Failable convenience initializers are allowed to return
nilbefore callingself.init.Designated initializers still must initialize all stored properties before returning
nil; this is a known limitation. (20193929) -
A new
readLine()function has been added to the standard library. (15911365) -
SIMD Support: Clang extended vectors are imported and usable in Swift.
This capability enables many graphics and other low-level numeric APIs (for example,
simd.h) to be usable in Swift. -
New
guardstatement: This statement allows you to model an early exit out of a scope.For example:
guard let z = bar() else { return } use(z)The
elseblock is required to exit the scope (for example, withreturn,throw,break,continue, and so forth) or end in a call to a@noreturnfunction. (20109722) -
Improved pattern matching:
switch/casepattern matching is available to many new conditional control flow statements, includingif/case,while/case,guard/case, andfor-in/case.for-instatements can also havewhereclauses, which combine to support many of the features of list comprehensions in other languages. -
A new
dostatement allows scopes to be introduced with thedostatement.For example:
do { //new scope do { //another scope } }
Swift Enhancements and Changes
-
A new keyword
try?has been added to Swift.try?attempts to perform an operation that may throw. If the operation succeeds, the result is wrapped in an optional; if it fails (that is, if an error is thrown), the result isniland the error is discarded.For example:
func produceGizmoUsingTechnology() throws -> Gizmo { … } func produceGizmoUsingMagic() throws -> Gizmo { … } if let result = try? produceGizmoUsingTechnology() { return result } if let result = try? produceGizmoUsingMagic() { return result } print("warning: failed to produce a Gizmo in any way") return niltry?always adds an extra level ofOptionalto the result type of the expression being evaluated. If a throwing function's normal return type isInt?, the result of calling it withtry?will beInt??, orOptional<Optional<Int>>. (21692467) -
Type names and enum cases now print and convert to
Stringwithout qualification by default.debugPrintorString(reflecting:)can still be used to get fully qualified names. For example:enum Fruit { case Apple, Banana, Strawberry } print(Fruit.Apple) // "Apple" debugPrint(Fruit.Apple) // "MyApp.Fruit.Apple")(21788604)
-
C
typedefs of block types are imported astypealiass for Swift closures.The primary result of this is that
typedefs for blocks with a parameter of typeBOOLare imported as closures with a parameter of typeBool(rather thanObjCBoolas in the previous release). This matches the behavior of block parameters to imported Objective-C methods. (22013912) -
The type
BooleaninMacTypes.his imported asBoolin contexts that allow bridging between Swift and Objective-C types.In cases where the representation is significant,
Booleanis imported as a distinctDarwinBooleantype, which isBooleanLiteralConvertibleand can be used in conditions (much like theObjCBooltype). (19013551) -
Fields of C structs that are marked
__unsafe_unretainedare presented in Swift usingUnmanaged.It is not possible for the Swift compiler to know if these references are really intended to be strong (+1) or unretained (+0). (19790608)
-
The
NS_REFINED_FOR_SWIFTmacro can be used to move an Objective-C declaration aside to provide a better version of the same API in Swift, while still having the original implementation available. (For example, an Objective-C API that takes aClasscould offer a more precise parameter type in Swift.)The
NS_REFINED_FOR_SWIFTmacro operates differently on different declarations:-
initmethods will be imported with the resulting Swift initializer having__prepended to its first external parameter name.// Objective-C - (instancetype)initWithClassName:(NSString *)name NS_REFINED_FOR_SWIFT;// Swift init(__className: String) -
Other methods will be imported with
__prepended to their base name.// Objective-C - (NSString *)displayNameForMode:(DisplayMode)mode NS_REFINED_FOR_SWIFT;// Swift func __displayNameForMode(mode: DisplayMode) -> String -
Subscript methods will be treated like any other methods and will not be imported as subscripts.
-
Other declarations will have
__prepended to their name.// Objective-C @property DisplayMode mode NS_REFINED_FOR_SWIFT;// Swift var __mode: DisplayMode { get set }
(20070465)
-
-
Xcode provides context-sensitive code completions for enum elements and option sets when using the shorter dot syntax. (16659653)
-
The
NSManagedattribute can be used with methods as well as properties, for access to Core Data's automatically generated Key-Value-Coding-compliant to-many accessors.@NSManaged var employees: NSSet @NSManaged func addEmployeesObject(employee: Employee) @NSManaged func removeEmployeesObject(employee: Employee) @NSManaged func addEmployees(employees: NSSet) @NSManaged func removeEmployees(employees: NSSet)These can be declared in your
NSManagedObjectsubclass. (17583057) -
The grammar has been adjusted so that lines beginning with '.' are always parsed as method or property lookups following the previous line, allowing for code formatted like this to work:
foo .bar .bas = 68000It is no longer possible to begin a line with a contextual static member lookup (for example, to say
.staticVar = MyType()). (20238557) -
Code generation for large
structandenumtypes has been improved to reduce code size. (20598289) -
Nonmutating methods of structs, enums, and protocols may now be partially applied to their self parameter:
let a: Set<Int> = [1, 2, 3] let b: [Set<Int>] = [[1], [4]] b.map(a.union) // => [[1, 2, 3], [1, 2, 3, 4]](21091944)
-
Swift documentation comments recognize a new top-level list item:
- Throws: ...This item is used to document what errors can be thrown and why. The documentation appears alongside parameters and return descriptions in Xcode QuickHelp. (21621679)
-
Unnamed parameters now require an explicit
_:to indicate that they are unnamed. For example, the following is now an error:func f(Int) { }and must be written as:
func f(_: Int) { }This simplifies the argument label model and also clarifies why cases like
func f((a: Int, b: Int))do not have parameters namedaandb. (16737312) -
It is now possible to append a tuple to an array. (17875634)
-
The ability to refer to the 0 element of a scalar value (producing the scalar value itself) has been removed. (17963034)
-
Variadic parameters can now appear anywhere in the parameter list for a function or initializer. For example:
func doSomethingToValues(values: Int... , options: MyOptions = [], fn: (Int) -> Void) { … }(20127197)
-
Generic subclasses of Objective-C classes are now supported. (18505295)
-
If an element of an enum with string raw type does not have an explicit raw value, it will default to the text of the enum's name. For example:
enum WorldLayer : String { case Ground, BelowCharacter, Character }is equivalent to:
enum WorldLayer : String { case Ground = "Ground" case BelowCharacter = "BelowCharacter" case Character = "Character" }(15819953)
-
The
performSelectorfamily of APIs is now available for Swift code. (17227475) -
When delegating or chaining to a failable initializer (for example, with
self.init(…)orsuper.init(…)), one can now force-unwrap the result with!. For example:extension UIImage { enum AssetIdentifier: String { case Isabella case William case Olivia } convenience init(assetIdentifier: AssetIdentifier) { self.init(named: assetIdentifier.rawValue)! } }(18497407)
-
Initializers can now be referenced like static methods by referring to
.initon a static type reference or type object. For example:let x = String.init(5) let stringType = String.self let y = stringType.init(5) let oneTwoThree = [1, 2, 3].map(String.init).reduce("", combine: +).initis still implicit when constructing using a static type, as inString(5)..initis required when using dynamic type objects or when referring to the initializer as a function value. (21375845) -
Enums and cases can now be marked indirect, which causes the associated value for the enum to be stored indirectly, allowing for recursive data structures to be defined. For example:
enum List<T> { case Nil indirect case Cons(head: T, tail: List<T>) } indirect enum Tree<T> { case Leaf(T) case Branch(left: Tree<T>, right: Tree<T>) }(21643855)
-
Formatting for Swift expression results has changed significantly when using
poorexpr -O. Customization that was introduced has been refined in the following ways:-
The formatted summary provided by either
debugDescriptionordescriptionmethods will always be used for types that conform toCustomDebugStringConvertibleorCustomStringConvertiblerespectively. When neither conformance is present, the type name is displayed and reference types also display the referenced address to more closely mimic existing behavior for Objective-C classes. -
Value types such as enums, tuples, and structs display all members indented below the summary by default, while reference types will not. This behavior can be customized for all types by implementing
CustomReflectable.
These output customizations can be bypassed by using
porexprwithout the-Oargument to provide a complete list of all fields and their values. (21463866) -
-
Properties and methods using
Unmanagedcan now be exposed to Objective-C. (16832080) -
Applying the
@objcattribute to a class changes that class's compile-time name in the target's generated Objective-C header as well as changing its runtime name. This applies to protocols as well. For example:// Swift @objc(MyAppDelegate) class AppDelegate : NSObject, UIApplicationDelegate { // ... }// Objective-C @interface MyAppDelegate : NSObject <UIApplicationDelegate> // ... @end(17469485)
-
Collections containing types that are not Objective-C compatible are no longer considered Objective-C compatible types themselves.
For example, previously
Array<SwiftClassType>was permitted as the type of a property marked@objc; this is no longer the case. (19787270) -
Generic subclasses of Objective-C classes, as well as nongeneric classes that inherit from such a class, require runtime metadata instantiation and cannot be directly named from Objective-C code.
When support for generic subclasses of Objective-C classes was first added, the generated Objective-C bridging header erroneously listed such classes, which, when used, could lead to incorrect runtime behavior or compile-time errors. This has been fixed.
The behavior of the
@objcattribute on a class has been clarified such that applying@objcto a class which cannot appear in a bridging header is now an error.Note that this change does not result in a change of behavior with valid code because members of a class are implicitly
@objcif any superclass of the class is an@objcclass, and all@objcclasses must inherit from NSObject. (21342574) -
The performance of
-Onone(debug) builds has been improved by using prespecialized instances of generics in the standard library. It produces significantly faster executables in debug builds in many cases, without impacting compile time. (20486658) -
AnyObjectandNSObjectvariables that refer to class objects can be cast back to class object types. For example, this code succeeds:let x: AnyObject = NSObject.self let y = x as! NSObject.TypeArrays, dictionaries, and sets that contain class objects successfully bridge with
NSArray,NSDictionary, andNSSetas well. Objective-C APIs that provideNSArray<Class> *objects, such as-[NSURLSessionConfiguration protocolClasses], now work correctly when used in Swift. (16238475) -
print()and reflection via Mirrors is able to report both the current case and payload for all enums with multiple payload types. The only remaining enum types that do not support reflection are@objcenums and enums imported from C. (21739870) -
Enum cases with payloads can be used as functions. For example:
enum Either<T, U> { case Left(T), Right(U) } let lefts: [Either<Int, String>] = [1, 2, 3].map(Either.Left) let rights: [Either<Int, String>] = ["one", "two", "three"].map(Either.Right)(19091028)
-
ExtensibleCollectionTypehas been folded intoRangeReplaceableCollectionType. In addition, default implementations have been added as methods, which should be used instead of the free Swift module functions related to these protocols. (18220295)
Swift Standard Library
-
The standard library moved many generic global functions (such as
map,filter, andsort) to be methods written with protocol extensions. This allows those methods to be pervasively available on all sequence and collection types and allowed the removal of the global functions. -
Deprecated enum elements no longer affect the names of nondeprecated elements when an Objective-C enum is imported into Swift. This may cause the Swift names of some enum elements to change. (17686122)
-
All enums imported from C are
RawRepresentable, including those not declared withNS_ENUMorNS_OPTIONS. As part of this change, the value property of such enums has been renamedrawValue. (18702016) -
Swift documentation comments use a syntax based on the Markdown format, aligning them with rich comments in playgrounds.
-
Outermost list items are interpreted as special fields and are highlighted in Xcode QuickHelp.
-
There are two methods of documenting parameters: parameter outlines and separate parameter fields. You can mix and match these forms as you see fit in any order or continuity throughout the doc comment. Because these are parsed as list items, you can nest arbitrary content underneath them.
-
Parameter outline syntax:
- Parameters: - x: ... - y: ... -
Separate parameter fields:
- parameter x: ... - parameter y: ... -
Documenting return values:
- returns: ...
Other special fields are highlighted in QuickHelp, as well as rendering support for all of Markdown. (20180161)
-
-
The
CFunctionPointer<T -> U>type has been removed. C function types are specified using the new@convention(c)attribute. Like other function types,@convention(c) T -> Uis not nullable unless made optional. The@objc_blockattribute for specifying block types has also been removed and replaced with@convention(block). -
Methods and functions have the same rules for parameter names. You can omit providing an external parameter name with
_. To further simplify the model, the shorthand#for specifying a parameter name has been removed, as have the special rules for default arguments.// Declaration func printFunction(str: String, newline: Bool) func printMethod(str: String, newline: Bool) func printFunctionOmitParameterName(str: String, _ newline: Bool) // Call printFunction("hello", newline: true) printMethod("hello", newline: true) printFunctionOmitParameterName("hello", true)(17218256)
-
NS_OPTIONStypes get imported as conforming to theOptionSetTypeprotocol, which presents a set-like interface for options. Instead of using bitwise operations such as:// Swift 1.2: object.invokeMethodWithOptions(.OptionA | .OptionB) object.invokeMethodWithOptions(nil) if options @ .OptionC == .OptionC { // .OptionC is set }Option sets support set literal syntax, and set-like methods such as contains:
object.invokeMethodWithOptions([.OptionA, .OptionB]) object.invokeMethodWithOptions([]) if options.contains(.OptionC) { // .OptionC is set }A new option set type can be written in Swift as a struct that conforms to the
OptionSetTypeprotocol. If the type specifies arawValueproperty and option constants asstatic letconstants, the standard library will provide default implementations of the rest of the option set API:struct MyOptions: OptionSetType { let rawValue: Int static let TuringMachine = MyOptions(rawValue: 1) static let LambdaCalculus = MyOptions(rawValue: 2) static let VonNeumann = MyOptions(rawValue: 4) } let churchTuring: MyOptions = [.TuringMachine, .LambdaCalculus](18069205)
-
Type annotations are no longer allowed in patterns and are considered part of the outlying declaration. This means that code previously written as:
var (a : Int, b : Float) = foo()needs to be written as:
var (a, b) : (Int, Float) = foo()if an explicit type annotation is needed. The former syntax was ambiguous with tuple element labels. (20167393)
-
The
do/whileloop is renamed torepeat/whileto make it obvious whether a statement is a loop from its leading keyword.In Swift 1.2:
do { ... } while <condition> In Swift 2.0: repeat { ... } while <condition>(20336424)
-
forEachhas been added toSequenceType. This lets you iterate over elements of a sequence, calling a body closure on each. For example:(0..<10).forEach { print($0) }This is very similar to the following:
for x in 0..<10 { print(x) }But take note of the following differences:
-
Unlike for-in loops, you can't use
breakorcontinueto exit the current call of the body closure or skip subsequent calls. -
Also unlike for-in loops, using
returnin the body closure only exits from the current call to the closure, not any outer scope, and won't skip subsequent calls.
(18231840)
-
-
The
WordandUWordtypes have been removed from the standard library; useIntandUIntinstead. (18693488) -
Most standard library APIs that take closures or
@autoclosureparameters now userethrows. This allows the closure parameters to methods likemapandfilterto throw errors, and allows short-circuiting operators like&&,||, and??to work with expressions that may produce errors. (21345565) -
SIMD improvements: Integer vector types in the simd module now only support unchecked arithmetic with wraparound semantics using the
&+,&-, and&*operators, in order to better support the machine model for vectors. The+,-, and*operators are unavailable on integer vectors, and Xcode automatically suggests replacing them with the wrapping operators.Code generation for vector types in the simd module has been improved to better utilize vector hardware, leading to dramatically improved performance in many cases. (21574425)
-
All
CollectionTypeobjects are now sliceable.SequenceTypenow has a notion ofSubSequence, which is a type that represents only some of the values but in the same order. For example, theArraySubSequencetype isArraySlice, which is an efficient view on theArraytype's buffer that avoids copying as long as it uniquely references theArrayfrom which it came.The following free Swift functions for splitting/slicing sequences have been removed and replaced by method requirements on the
SequenceTypeprotocol with default implementations in protocol extensions.CollectionTypehas specialized implementations, where possible, to take advantage of efficient access of its elements./// Returns the first `maxLength` elements of `self`, /// or all the elements if `self` has fewer than `maxLength` elements. prefix(maxLength: Int) -> SubSequence /// Returns the last `maxLength` elements of `self`, /// or all the elements if `self` has fewer than `maxLength` elements. suffix(maxLength: Int) -> SubSequence /// Returns all but the first `n` elements of `self`. dropFirst(n: Int) -> SubSequence /// Returns all but the last `n` elements of `self`. dropLast(n: Int) -> SubSequence /// Returns the maximal `SubSequence`s of `self`, in order, that /// don't contain elements satisfying the predicate `isSeparator`. split(maxSplits maxSplits: Int, allowEmptySlices: Bool, @noescape isSeparator: (Generator.Element) -> Bool) -> [SubSequence]The following convenience extension is provided for
split:split(separator: Generator.Element, maxSplit: Int, allowEmptySlices: Bool) -> [SubSequence]Also, new protocol requirements and default implementations on
CollectionTypeare now available:/// Returns `self[startIndex..<end]` prefixUpTo(end: Index) -> SubSequence /// Returns `self[start..<endIndex]` suffixFrom(start: Index) -> SubSequence /// Returns `prefixUpTo(position.successor())` prefixThrough(position: Index) -> SubSequence(21663830)
-
The
printanddebugPrintfunctions are improved:-
Both functions have become variadic, and you can print any number of items with a single call.
-
separator: String = " "was added so you can control how the items are separated. -
terminator: String = "\n"replacedappendNewline: bool = true.With this change,print(x, appendNewline: false)is expressed asprint(x, terminator: ""). -
For the variants that take an output stream, the argument label
toStreamwas added to the stream argument.
The
printlnfunction from Swift 1.2 has been removed. (21788540) -
-
For consistency and better composition of generic code,
ArraySliceindices are no longer always zero-based but map directly onto the indices of the collection they are slicing and maintain that mapping even after mutations.Before:
var a = Array(0..<10) var s = a[5..<10] s.indices // 0..<5 s[0] = 111 s // [111, 6, 7, 8, 9] s.removeFirst() s.indices // 1..<5After:
var a = Array(0..<10) var s = a[5..<10] s.indices // 5..<10 s[5] = 99 s // [99, 6, 7, 8, 9] s.removeFirst() s.indices // 6..<10Rather than define variants of collection algorithms that take explicit subrange arguments, such as
a.sortSubrangeInPlace(3..<7), the Swift standard library provides "slicing," which composes well with algorithms. This enables you to writea[3..<7].sortInPlace(), for example. With most collections, these algorithms compose naturally.For example, before this change was incorporated:
extension MyIntCollection { func prefixThroughFirstNegativeSubrange() -> SubSequence { // Find the first negative element let firstNegative = self.indexOf { $0 < 0 } ?? endIndex // Slice off non-negative prefix let startsWithNegative = self.suffixFrom(firstNegative) // Find the first non-negative position in the slice let end = startsWithNegative.indexOf { $0 >= 0 } ?? endIndex return self[startIndex..<end] } }The above code would work for any collection of
Ints unless the collection is anArray<Int>. Unfortunately, when array slice indices are zero-based, the last two lines of the method need to change to:let end = startsWithNegative.indexOf { $0 >= 0 } ?? startsWithNegative.endIndex return self[startIndex..<end + firstNegative]These differences made working with slices awkward, error-prone, and nongeneric.
After this change, Swift collections start to provide a guarantee that, at least until there is a mutation, slice indices are valid in the collection from which they were sliced, and refer to the same elements. (21866825)
-
The method
RangeReplaceableCollectionType.extend()was renamed toappendContentsOf(), and thesplice()method was renamed toinsertContentsOf(). (21972324) -
findhas been renamed toindexOf(), sort has been renamed tosortInPlace(), andsorted()becomessort(). -
String.toInt()has been renamed to a failableInt(String)initializer, since initialization syntax is the preferred style for type conversions. -
Stringno longer conforms toSequenceTypein order to prevent non-Unicode correct sequence algorithms from being prominently available on String. To perform grapheme-cluster-based, UTF-8-based, or UTF-16-based algorithms, use the.characters,.utf8, and.utf16projections respectively. -
Generic functions that declare type parameters not used within the generic function's type produce a compiler error. For example:
func foo<T>() { } // error: generic parameter 'T' is not used in function signature -
The
Dictionary.removeAtIndex(_:)method now returns the key-value pair being removed as a two-element tuple (rather than returningVoid). Similarly, theSet.removeAtIndex(_:)method returns the element being removed. (20299881) -
Generic parameters on types in the Swift standard library have been renamed to reflect the role of the types in the API. For example,
Array<T>becameArray<Element>,UnsafePointer<T>becameUnsafePointer<Memory>, and so forth. (21429126) -
The
SinkTypeprotocol andSinkOfstruct have been removed from the standard library in favor of(T) -> ()closures. (21663799)
2015-04-08 [Xcode 6.3, Swift 1.2]
Swift Language Changes
-
The notions of guaranteed conversion and "forced failable" conversion are now separated into two operators. Forced failable conversion now uses the
as!operator. The!makes it clear to readers of code that the cast may fail and produce a runtime error. Theasoperator remains for upcasts (e.g.someDerivedValue as Base) and type annotations (0 as Int8) which are guaranteed to never fail. (19031957) -
Immutable (
let) properties instructandclassinitializers have been revised to standardize on a general "lets are singly initialized but never reassigned or mutated" model. Previously, they were completely mutable within the body of initializers. Now, they are only allowed to be assigned to once to provide their value. If the property has an initial value in its declaration, that counts as the initial value for all initializers. (19035287) -
The implicit conversions from bridged Objective-C classes (
NSString/NSArray/NSDictionary) to their corresponding Swift value types (String/Array/Dictionary) have been removed, making the Swift type system simpler and more predictable.This means that the following code will no longer work:
import Foundation func log(s: String) { println(x) } let ns: NSString = "some NSString" // okay: literals still work log(ns) // fails with the error // "'NSString' is not convertible to 'String'"In order to perform such a bridging conversion, make the conversion explicit with the as keyword:
log(ns as String) // succeedsImplicit conversions from Swift value types to their bridged Objective-C classes are still permitted. For example:
func nsLog(ns: NSString) { println(ns) } let s: String = "some String" nsLog(s) // okay: implicit conversion from String to NSString is permittedNote that these Cocoa types in Objective-C headers are still automatically bridged to their corresponding Swift type, which means that code is only affected if it is explicitly referencing (for example)
NSStringin a Swift source file. It is recommended you use the corresponding Swift types (for example,String) directly unless you are doing something advanced, like implementing a subclass in the class cluster. (18311362) -
The
@autoclosureattribute is now an attribute on a parameter, not an attribute on the parameter's type.Where before you might have used:
func assert(predicate : @autoclosure () -> Bool) {...} you now write this as: func assert(@autoclosure predicate : () -> Bool) {...}(15217242)
-
The
@autoclosureattribute on parameters now implies the new@noescapeattribute. -
Curried function parameters can now specify argument labels.
For example:
func curryUnnamed(a: Int)(_ b: Int) { return a + b } curryUnnamed(1)(2) func curryNamed(first a: Int)(second b: Int) -> Int { return a + b } curryNamed(first: 1)(second: 2)(17237268)
-
Swift now detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime.
For example, the following conflict between the Objective-C setter for
propertyin a class and the methodsetPropertyin its extension is now diagnosed:class A : NSObject { var property: String = "Hello" // note: Objective-C method 'setProperty:' // previously declared by setter for // 'property' here } extension A { func setProperty(str: String) { } // error: method 'setProperty' // redeclares Objective-C method //'setProperty:' } Similar checking applies to accidental overrides in the Objective-C runtime: class B : NSObject { func method(arg: String) { } // note: overridden declaration // here has type '(String) -> ()' } class C : B { func method(arg: [String]) { } // error: overriding method with // selector 'method:' has incompatible // type '([String]) -> ()' } as well as protocol conformances: class MyDelegate : NSObject, NSURLSessionDelegate { func URLSession(session: NSURLSession, didBecomeInvalidWithError: Bool){ } // error: Objective-C method 'URLSession:didBecomeInvalidWithError:' // provided by method 'URLSession(_:didBecomeInvalidWithError:)' // conflicts with optional requirement method // 'URLSession(_:didBecomeInvalidWithError:)' in protocol // 'NSURLSessionDelegate' }(18391046, 18383574)
-
The precedence of the Nil Coalescing Operator (
??) has been raised to bind tighter than short-circuiting logical and comparison operators, but looser than as conversions and range operators. This provides more useful behavior for expressions like:if allowEmpty || items?.count ?? 0 > 0 {...} -
The
&/and&%operators were removed, to simplify the language and improve consistency.Unlike the
&+,&-, and&*operators, these operators did not provide two's-complement arithmetic behavior; they provided special case behavior for division, remainder by zero, andInt.min/-1. These tests should be written explicitly in the code as comparisons if needed. (17926954) -
Constructing a
UInt8from an ASCII value now requires the ascii keyword parameter. Using non-ASCII unicode scalars will cause this initializer to trap. (18509195) -
The C
size_tfamily of types are now imported into Swift asInt, since Swift prefers sizes and counts to be represented as signed numbers, even if they are non-negative.This change decreases the amount of explicit type conversion between
IntandUInt, better aligns withsizeofreturningInt, and provides safer arithmetic properties. (18949559) -
Classes that do not inherit from
NSObjectbut do adopt an@objcprotocol will need to explicitly mark those methods, properties, and initializers used to satisfy the protocol requirements as@objc.For example:
@objc protocol SomethingDelegate { func didSomething() } class MySomethingDelegate : SomethingDelegate { @objc func didSomething() { … } }
Swift Language Fixes
-
Dynamic casts (
as!,as?andis) now work with Swift protocol types, so long as they have no associated types. (18869156) -
Adding conformances within a Playground now works as expected.
For example:
struct Point { var x, y: Double } extension Point : Printable { var description: String { return "(\(x), \(y))" } } var p1 = Point(x: 1.5, y: 2.5) println(p1) // prints "(1.5, 2.5)" -
Imported
NS_ENUMtypes with undocumented values, such asUIViewAnimationCurve, can now be converted from their raw integer values using theinit(rawValue:)initializer without being reset tonil. Code that usedunsafeBitCastas a workaround for this issue can be written to use the raw value initializer.For example:
let animationCurve = unsafeBitCast(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue, UIViewAnimationCurve.self) can now be written instead as: let animationCurve = UIViewAnimationCurve(rawValue: userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!(19005771)
-
Negative floating-point literals are now accepted as raw values in enums. (16504472)
-
Unowned references to Objective-C objects, or Swift objects inheriting from Objective-C objects, no longer cause a crash if the object holding the unowned reference is deallocated after the referenced object has been released. (18091547)
-
Variables and properties with observing accessors no longer require an explicit type if it can be inferred from the initial value expression. (18148072)
-
Generic curried functions no longer produce random results when fully applied. (18988428)
-
Comparing the result of a failed
NSClassFromStringlookup againstnilnow behaves correctly. (19318533) -
Subclasses that override base class methods with co- or contravariance in Optional types no longer cause crashes at runtime.
For example:
class Base { func foo(x: String) -> String? { return x } } class Derived: Base { override func foo(x: String?) -> String { return x! } }(19321484)
Swift Language Enhancements
-
Swift now supports building targets incrementally, i.e. not rebuilding every Swift source file in a target when a single file is changed.
The incremental build capability is based on a conservative dependency analysis, so you may still see more files rebuilding than absolutely necessary. If you find any cases where a file is not rebuilt when it should be, please file a bug report. Running Clean on your target afterwards should allow you to complete your build normally. (18248514)
-
A new
Setdata structure is included which provides a generic collection of unique elements with full value semantics. It bridges withNSSet, providing functionality analogous toArrayandDictionary. (14661754) -
The
if-letconstruct has been expanded to allow testing multiple optionals and guarding conditions in a singleif(orwhile) statement using syntax similar to generic constraints:if let a = foo(), b = bar() where a < b, let c = baz() { }This allows you to test multiple optionals and include intervening boolean conditions, without introducing undesirable nesting (for instance, to avoid the optional unwrapping "pyramid of doom").
Further,
if-letnow also supports a single leading boolean condition along with optional bindingletclauses. For example:if someValue > 42 && someOtherThing < 19, let a = getOptionalThing() where a > someValue { }(19797158, 19382942)
-
The
if-letsyntax has been extended to support a single leading boolean condition along with optional bindingletclauses.For example:
if someValue > 42 && someOtherThing < 19, let a = getOptionalThing() where a > someValue { }(19797158)
-
letconstants have been generalized to no longer require immediate initialization. The new rule is that aletconstant must be initialized before use (like avar), and that it may only be initialized: not reassigned or mutated after initialization. This enables patterns such as:let x: SomeThing if condition { x = foo() } else { x = bar() } use(x)which formerly required the use of a
var, even though there is no mutation taking place. (16181314) -
staticmethods and properties are now allowed in classes (as an alias forclass final). You are now allowed to declare static stored properties in classes, which have global storage and are lazily initialized on first access (like global variables). Protocols now declare type requirements as static requirements instead of declaring them as class requirements. (17198298) -
Type inference for single-expression closures has been improved in several ways:
- Closures that are comprised of a single return statement are now type checked as single-expression closures.
- Unannotated single-expression closures with non-
Voidreturn types can now be used inVoidcontexts. - Situations where a multi-statement closure's type could not be inferred because of a missing return-type annotation are now properly diagnosed.
-
Swift enums can now be exported to Objective-C using the
@objcattribute.@objcenums must declare an integer raw type, and cannot be generic or use associated values. Because Objective-C enums are not namespaced, enum cases are imported into Objective-C as the concatenation of the enum name and case name.For example, this Swift declaration:
// Swift @objc enum Bear: Int { case Black, Grizzly, Polar }imports into Objective-C as:
// Objective-C typedef NS_ENUM(NSInteger, Bear) { BearBlack, BearGrizzly, BearPolar };(16967385)
-
Objective-C language extensions are now available to indicate the nullability of pointers and blocks in Objective-C APIs, allowing your Objective-C APIs to be imported without
ImplicitlyUnwrappedOptional. (See items below for more details.) (18868820) -
Swift can now partially import C aggregates containing unions, bitfields, SIMD vector types, and other C language features that are not natively supported in Swift. The unsupported fields will not be accessible from Swift, but C and Objective-C APIs that have arguments and return values of these types can be used in Swift. This includes the Foundation
NSDecimaltype and theGLKitGLKVectorandGLKMatrixtypes, among others. (15951448) -
Imported C structs now have a default initializer in Swift that initializes all of the struct's fields to zero.
For example:
import Darwin var devNullStat = stat() stat("/dev/null", &devNullStat)If a structure contains fields that cannot be correctly zero initialized (i.e. pointer fields marked with the new
__nonnullmodifier), this default initializer will be suppressed. (18338802) -
New APIs for converting among the
Indextypes forString,String.UnicodeScalarView,String.UTF16View, andString.UTF8Vieware available, as well as APIs for converting each of theStringviews intoStrings. (18018911) -
Type values now print as the full demangled type name when used with
printlnor string interpolation.toString(Int.self) // prints "Swift.Int" println([Float].self) // prints "Swift.Array<Swift.Float>" println((Int, String).self) // prints "(Swift.Int, Swift.String)"(18947381)
-
A new
@noescapeattribute may be used on closure parameters to functions. This indicates that the parameter is only ever called (or passed as an@noescapeparameter in a call), which means that it cannot outlive the lifetime of the call. This enables some minor performance optimizations, but more importantly disables theself.requirement in closure arguments. This enables control-flow-like functions to be more transparent about their behavior. In a future beta, the standard library will adopt this attribute in functions likeautoreleasepool().func autoreleasepool(@noescape code: () -> ()) { pushAutoreleasePool() code() popAutoreleasePool() }(16323038)
-
Performance is substantially improved over Swift 1.1 in many cases. For example, multidimensional arrays are algorithmically faster in some cases, unoptimized code is much faster in many cases, and many other improvements have been made.
-
The diagnostics emitted for expression type check errors are greatly improved in many cases. (18869019)
-
Type checker performance for many common expression kinds has been greatly improved. This can significantly improve build times and reduces the number of "expression too complex" errors. (18868985)
-
The
@autoclosureattribute has a second form,@autoclosure(escaping), that provides the same caller-side syntax as@autoclosurebut allows the resulting closure to escape in the implementation.For example:
func lazyAssertion(@autoclosure(escaping) condition: () -> Bool, message: String = "") { lazyAssertions.append(condition) // escapes } lazyAssertion(1 == 2, message: "fail eventually")(19499207)
Swift Performance
- A new compilation mode has been introduced for Swift called Whole Module
Optimization. This option optimizes all of the files in a target together
and enables better performance (at the cost of increased compile time). The
new flag can be enabled in Xcode using the
Whole Module Optimizationbuild setting or by using theswiftccommand line tool with the flag-whole-module-optimization. (18603795)
Swift Standard Library Enhancements and Changes
-
flatMapwas added to the standard library.flatMapis the function that maps a function over something and returns the result flattened one level.flatMaphas many uses, such as to flatten an array:[[1,2],[3,4]].flatMap { $0 }or to chain optionals with functions:
[[1,2], [3,4]].first.flatMap { find($0, 1) }(19881534)
-
The function
zipwas added. It joins two sequences together into one sequence of tuples. (17292393) -
utf16Countis removed fromString. Instead use count on theUTF16view of theString.For example:
count(string.utf16)(17627758)
2014-12-02 [Xcode 6.1.1]
-
Class methods and initializers that satisfy protocol requirements now properly invoke subclass overrides when called in generic contexts. For example:
protocol P { class func foo() } class C: P { class func foo() { println("C!") } } class D: C { override class func foo() { println("D!") } } func foo<T: P>(x: T) { x.dynamicType.foo() } foo(C()) // Prints "C!" foo(D()) // Used to incorrectly print "C!", now prints "D!"(18828217)
2014-10-09 [Xcode 6.1 Release Notes, Swift 1.1]
-
Values of type
Anycan now contain values of function type. (16406907) -
Documentation for the standard library (displayed in quick help and in the synthesized header for the Swift module) is improved. (16462500)
-
Class properties don't need to be marked final to avoid
O(n)mutations on value semantic types. (17416120) -
Casts can now be performed between
CFtypes (such asCFString,CGImage, andSecIdentity) and AnyObject. Such casts will always succeed at run-time. For example:var cfStr: CFString = ... var obj: AnyObject = cfStr as AnyObject var cfStr = obj as CFString(18088474)
2014-10-09 [Roughly Xcode 6.1, and Swift 1.1]
-
HeapBuffer<Value, Element>,HeapBufferStorage<Value, Element>, andOnHeap<Value>were never really useful, because their APIs were insufficiently public. They have been replaced with a single class,ManagedBuffer<Value, Element>. See also the new functionisUniquelyReferenced(x)which is often useful in conjunction withManagedBuffer. -
The
Characterenum has been turned into a struct, to avoid exposing its internal implementation details. -
The
countElementsfunction has been renamedcount, for better consistency with our naming conventions. -
Mixed-sign addition and subtraction operations, that were unintentionally allowed in previous versions, now cause a compilation error.
-
OS X apps can now apply the
@NSApplicationMainattribute to their app delegate class in order to generate an implicitmainfor the app. This works like the@UIApplicationMainattribute for iOS apps. -
Objective-C
initand factory methods are now imported as failable initializers when they can returnnil. In the absence of information about a potentially-nilresult, an Objective-Cinitor factory method will be imported asinit!.As part of this change, factory methods that have NSError** parameters, such as
+[NSString stringWithContentsOfFile:encoding:error:], will now be imported as (failable) initializers, e.g.,init?(contentsOfFile path: String, encoding: NSStringEncoding, error: NSErrorPointer) -
Nested classes explicitly marked
@objcwill now properly be included in a target's generated header as long as the containing context is also (implicitly or explicitly)@objc. Nested classes not explicitly marked@objcwill never be printed in the generated header, even if they extend an Objective-C class. -
All of the
*LiteralConvertibleprotocols, as well asStringInterpolationConvertible, now use initializers for their requirements rather than static methods starting withconvertFrom. For example,IntegerLiteralConvertiblenow has the following initializer requirement:init(integerLiteral value: IntegerLiteralType)Any type that previously conformed to one of these protocols will need to replace its
convertFromXXXstatic methods with the corresponding initializer.
2014-09-15
-
Initializers can now fail by returning
nil. A failable initializer is declared withinit?(to return an explicit optional) orinit!(to return an implicitly-unwrapped optional). For example, you could implementString.toIntas a failable initializer ofIntlike this:extension Int { init?(fromString: String) { if let i = fromString.toInt() { // Initialize self = i } else { // Discard self and return 'nil'. return nil } } }The result of constructing a value using a failable initializer then becomes optional:
if let twentytwo = Int(fromString: "22") { println("the number is \(twentytwo)") } else { println("not a number") }In the current implementation, struct and enum initializers can return
nilat any point inside the initializer, but class initializers can only returnnilafter all of the stored properties of the object have been initialized andself.initorsuper.inithas been called. Ifself.initorsuper.initis used to delegate to a failable initializer, then thenilreturn is implicitly propagated through the current initializer if the called initializer fails. -
The
RawRepresentableprotocol that enums with raw types implicitly conform to has been redefined to take advantage of failable initializers. ThefromRaw(RawValue)static method has been replaced with an initializerinit?(rawValue: RawValue), and thetoRaw()method has been replaced with arawValueproperty. Enums with raw types can now be used like this:enum Foo: Int { case A = 0, B = 1, C = 2 } let foo = Foo(rawValue: 2)! // formerly 'Foo.fromRaw(2)!' println(foo.rawValue) // formerly 'foo.toRaw()'
2014-09-02
- Characters can no longer be concatenated using
+. UseString(c1) + String(c2)instead.
2014-08-18
- When force-casting between arrays of class or
@objcprotocol types usinga as [C], type checking is now deferred until the moment each element is accessed. Because bridging conversions from NSArray are equivalent to force-casts from[NSArray], this makes certain Array round-trips through Objective-C codeO(1)instead ofO(N).
2014-08-04
-
RawOptionSetTypenow implementsBitwiseOperationsType, so importedNS_OPTIONSnow support the bitwise assignment operators|=,&=, and^=. It also no longer implementsBooleanType; to check if an option set is empty, compare it tonil. -
Types implementing
BitwiseOperationsTypenow automatically support the bitwise assignment operators|=,&=, and^=. -
Optionals can now be coalesced with default values using the
??operator.??is a short-circuiting operator that takes an optional on the left and a non-optional expression on the right. If the optional has a value, its value is returned as a non-optional; otherwise, the expression on the right is evaluated and returned:var sequence: [Int] = [] sequence.first ?? 0 // produces 0, because sequence.first is nil sequence.append(22) sequence.first ?? 0 // produces 22, the value of sequence.first -
The optional chaining
?operator can now be mutated through, like!. The assignment and the evaluation of the right-hand side of the operator are conditional on the presence of the optional value:var sequences = ["fibonacci": [1, 1, 2, 3, 4], "perfect": [6, 28, 496]] sequences["fibonacci"]?[4]++ // Increments element 4 of key "fibonacci" sequences["perfect"]?.append(8128) // Appends to key "perfect" sequences["cubes"]?[3] = 3*3*3 // Does nothing; no "cubes" keyNote that optional chaining still flows to the right, so prefix increment operators are not included in the chain, so this won't type-check:
++sequences["fibonacci"]?[4] // Won't type check, can't '++' Int?
2014-07-28
-
The swift command line interface is now divided into an interactive driver
swift, and a batch compilerswiftc:swift [options] input-file [program-arguments] Runs the script 'input-file' immediately, passing any program-arguments to the script. Without any input files, invokes the repl. swiftc [options] input-filenames The familiar swift compiler interface: compiles the input-files according to the mode options like -emit-object, -emit-executable, etc. -
For greater clarity and explicitness when bypassing the type system,
reinterpretCasthas been renamedunsafeBitCast, and it has acquired a (required) explicit type parameter. Solet x: T = reinterpretCast(y)becomes
let x = unsafeBitCast(y, T.self) -
Because their semantics were unclear, the methods
asUnsigned(on the signed integer types) andasSigned(on the unsigned integer types) have been replaced. The new idiom is explicit construction of the target type using thebitPattern:argument label. So,myInt.asUnsigned()has become
UInt(bitPattern: myInt) -
To better follow Cocoa naming conventions and to encourage immutability, The following pointer types were renamed:
Old Name New Name UnsafePointer<T>UnsafeMutablePointer<T>ConstUnsafePointer<T>UnsafePointer<T>AutoreleasingUnsafePointer<T>AutoreleasingUnsafeMutablePointer<T>Note that the meaning of
UnsafePointerhas changed from mutable to immutable. As a result, some of your code may fail to compile when assigning to anUnsafePointer.memoryproperty. The fix is to change yourUnsafePointer<T>into anUnsafeMutablePointer<T>. -
The optional unwrapping operator
x!can now be assigned through, and mutating methods and operators can be applied through it:var x: Int! = 0 x! = 2 x!++ // Nested dictionaries can now be mutated directly: var sequences = ["fibonacci": [1, 1, 2, 3, 0]] sequences["fibonacci"]![4] = 5 sequences["fibonacci"]!.append(8) -
The
@auto_closureattribute has been renamed to@autoclosure. -
There is a new
dynamicdeclaration modifier. When applied to a method, property, subscript, or initializer, it guarantees that references to the declaration are always dynamically dispatched and never inlined or devirtualized, and that the method binding can be reliably changed at runtime. The implementation currently relies on the Objective-C runtime, sodynamiccan only be applied to@objc-compatibledeclarations for now.@objcnow only makes a declaration visible to Objective-C; the compiler may now use vtable lookup or direct access to access (non-dynamic)@objcdeclarations.class Foo { // Always accessed by objc_msgSend dynamic var x: Int // Accessed by objc_msgSend from ObjC; may be accessed by vtable // or by static reference in Swift @objc var y: Int // Not exposed to ObjC (unless Foo inherits NSObject) var z: Int }dynamicenables KVO, proxying, and other advanced Cocoa features to work reliably with Swift declarations. -
Clang submodules can now be imported:
import UIKit.UIGestureRecognizerSubclass -
The numeric optimization levels
-O[0-3]have been removed in favor of the named levels-Ononeand-O. -
The
-Ofastoptimization flag has been renamed to-Ounchecked. We will accept both names for now and remove-Ofastin a later build. -
An initializer that overrides a designated initializer from its superclass must be marked with the
overridekeyword, so that all overrides in the language consistently require the use ofoverride. For example:class A { init() { } } class B : A { override init() { super.init() } } -
Required initializers are now more prominent in several ways. First, a (non-final) class that conforms to a protocol that contains an initializer requirement must provide a required initializer to satisfy that requirement. This ensures that subclasses will also conform to the protocol, and will be most visible with classes that conform to
NSCoding:class MyClass : NSObject, NSCoding { required init(coder aDecoder: NSCoder!) { /*... */ } func encodeWithCoder(aCoder: NSCoder!) { /* ... */ } }Second, because
requiredplaces a significant requirement on all subclasses, therequiredkeyword must be placed on overrides of a required initializer:class MySubClass : MyClass { var title: String = "Untitled" required init(coder aDecoder: NSCoder!) { /*... */ } override func encodeWithCoder(aCoder: NSCoder!) { /* ... */ } }Finally, required initializers can now be inherited like any other initializer:
class MySimpleSubClass : MyClass { } // inherits the required init(coder:).
2014-07-21
-
Access control has been implemented.
publicdeclarations can be accessed from any module.internaldeclarations (the default) can be accessed from within the current module.privatedeclarations can be accessed only from within the current file.
There are still details to iron out here, but the model is in place. The general principle is that an entity cannot be defined in terms of another entity with less accessibility.
Along with this, the generated header for a framework will only include public declarations. Generated headers for applications will include public and internal declarations.
-
CGFloatis now a distinct floating-point type that wraps either aFloat(on 32-bit architectures) or aDouble(on 64-bit architectures). It provides all of the same comparison and arithmetic operations of Float and Double, and can be created using numeric literals. -
The immediate mode
swift -inow works for writing#!scripts that take command line arguments. The-ioption to the swift driver must now come at the end of the compiler arguments, directly before the input filename. Any arguments that come after-iand the input filename are treated as arguments to the interpreted file and forwarded toProcess.arguments. -
Type inference for
for..inloops has been improved to consider the sequence along with the element pattern. For example, this accepts the following loops that were previously rejected:for i: Int8 in 0..<10 { } for i: Float in 0.0...10.0 { } -
Introduced the new
BooleanLiteralConvertibleprotocol, which allows user-defined types to support Boolean literals.trueandfalseare nowBooleanconstants and keywords. -
The
@final,@lazy,@requiredand@optionalattributes are now considered to be declaration modifiers - they no longer require (or allow) an@sign. -
The
@prefix,@infix, and@postfixattributes have been changed to declaration modifiers, so they are no longer spelled with an@sign.
Operator declarations have been rearranged fromoperator prefix +toprefix operator +for consistency.
2014-07-03
-
C function pointer types are now imported as
CFunctionPointer<T>, whereTis a Swift function type.CFunctionPointerandCOpaquePointercan be explicitly constructed from one another, but they do not freely convert, nor isCFunctionPointercompatible with Swift closures.Example:
int (*)(void)becomesCFunctionPointer<(Int) -> Void>. -
The interop model for pointers in C APIs has been simplified. Most code that calls C functions by passing arrays, UnsafePointers, or the addresses of variables with
&xdoes not need to change. However, theCConstPointerandCMutablePointerbridging types have been removed, and functions and methods are now imported as and overridden by taking UnsafePointer andConstUnsafePointerdirectly.Voidpointers are now imported as(Const)UnsafePointer<Void>;COpaquePointeris only imported for opaque types now. -
Arraytypes are now spelled with the brackets surrounding the element type. For example, an array ofIntis written as:var array: [Int] -
Dictionarytypes can now be spelled with the syntax[K : V], whereKis the key type andVis the value type. For example:var dict: [String : Int] = ["Hello" : 1, "World" : 2]The type
[K : V]is syntactic sugar forDictionary<K, V>; nothing else has changed. -
The
@IBOutletattribute no longer implicitly (and invisibly) changes the type of the declaration it is attached to. It no longer implicitly makes variables be an implicitly unwrapped optional and no longer defaults them to weak. -
The
\x,\uand\Uescape sequences in string literals have been consolidated into a single and less error prone\u{123456}syntax.
2014-06-23
-
The half-open range operator has been renamed from
..to..<to reduce confusion. The..<operator is precedented in Groovy (among other languages) and makes it much more clear that it doesn't include the endpoint. -
Class objects such as
NSObject.selfcan now be converted toAnyObjectand used as object values. -
Objective-C protocol objects such as
NSCopying.selfcan now be used as instances of theProtocolclass, such as in APIs such as XPC. -
Arrays now have full value semantics: both assignment and initialization create a logically-distinct object
-
The
sortfunction and array method modify the target in-place. A newsortedfunction and array method are non-mutating, creating and returning a new collection.
2014-05-19
-
sort,map,filter, andreducemethods onArrays accept trailing closures:let a = [5, 6, 1, 3, 9] a.sort{ $0 > $1 } println(a) // [9, 6, 5, 3, 1] println(a.map{ $0 * 2 }) // [18, 12, 10, 6, 2] println(a.map{ $0 * 2 }.filter{ $0 < 10}) // [6, 2] println(a.reduce(1000){ $0 + $1 }) // 1024 (no kidding) -
A lazy
map()function in the standard library works on anySequence. Example:class X { var value: Int init(_ value: Int) { self.value = value println("created X(\(value))") } } // logically, this sequence is X(0), X(1), X(2), ... X(50) let lazyXs = map(0..50){ X($0) } // Prints "created X(...)" 4 times for x in lazyXs { if x.value == 4 { break } } -
There's a similar lazy
filter()function:// 0, 10, 20, 30, 40 let tens = filter(0..50) { $0 % 10 == 0 } let tenX = map(tens){ X($0) } // 5 lazy Xs let tenXarray = Array(tenX) // Actually creates those Xs -
Weak pointers of classbound protocol type work now.
-
IBOutletsnow default to weak pointers with implicit optional type (T!). -
NSArray*parameters and result types of Objective-C APIs are now imported asAnyObject[]!, i.e., an implicitly unwrapped optional array storingAnyObjectvalues. For example,NSView's constraints property@property (readonly) NSArray *constraints;is now imported as
var constraints: AnyObject[]!Note that one can implicitly convert between an
AnyObject[]and anNSArray(in both directions), so (for example) one can still explicitly useNSArrayif desired:var array: NSArray = view.constraintsSwift arrays bridge to
NSArraysimilarly to the way Swift strings bridge toNSString. -
ObjCMutablePointerhas been renamedAutoreleasingUnsafePointer. -
UnsafePointer(andAutoreleasingUnsafePointer)'sset()andget()have been replaced with a property calledmemory.-
Previously you would write:
val = p.get() p.set(val) -
Now you write:
val = p.memory p.memory = val
-
-
Removed shorthand
x as T!; instead use(x as T)!x as T!now means "x as implicitly unwrapped optional".
-
Range operators
..and...have been switched.1..3now means 1,21...3now means 1,2,3
-
The pound sign (
#) is now used instead of the back-tick (`) to mark an argument name as a keyword argument, e.g.,func moveTo(#x: Int, #y: Int) { ... } moveTo(x: 5, y: 7) -
Objective-C factory methods are now imported as initializers. For example,
NSColor's+colorWithRed:green:blue:alphabecomesinit(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)which allows an
NSColorto be created as, e.g.,NSColor(red: 0.5, green: 0.25, blue: 0.25, alpha: 0.5)Factory methods are identified by their kind (class methods), name (starts with words that match the words that end the class name), and result type (
instancetypeor the class type). -
Objective-C properties of some
CFtype are no longer imported asUnmanaged. -
REPL mode now uses LLDB, for a greatly-expanded set of features. The colon prefix now treats the rest of the line as a command for LLDB, and entering a single colon will drop you into the debugging command prompt. Most importantly, crashes in the REPL will now drop you into debugging mode to see what went wrong.
If you do have a need for the previous REPL, pass
-integrated-repl. -
In a UIKit-based application, you can now eliminate your 'main.swift' file and instead apply the
@UIApplicationMainattribute to yourUIApplicationDelegateclass. This will cause themainentry point to the application to be automatically generated as follows:UIApplicationMain(argc, argv, nil, NSStringFromClass(YourApplicationDelegate.self))If you need nontrivial logic in your application entry point, you can still write out a
main.swift. Note that@UIApplicationMainandmain.swiftare mutually exclusive.
2014-05-13
-
weak pointers now work with implicitly unchecked optionals, enabling usecases where you don't want to
!every use of a weak pointer. For example:weak var myView : NSView!of course, they still work with explicitly checked optionals like
NSView? -
Dictionary subscripting now takes/returns an optional type. This allows querying a dictionary via subscripting to gracefully fail. It also enables the idiom of removing values from a dictionary using
dict[key] = nil. As part of this,deleteKeyis no longer available. -
Stored properties may now be marked with the
@lazyattribute, which causes their initializer to be evaluated the first time the property is touched instead of when the enclosing type is initialized. For example:func myInitializer() -> Int { println("hello\n"); return 42 } class MyClass { @lazy var aProperty = myInitializer() } var c = MyClass() // doesn't print hello var tmp = c.aProperty // prints hello on first access tmp = c.aProperty // doesn't print on subsequent loads. c = MyClass() // doesn't print hello c.aProperty = 57 // overwriting the value prevents it from ever runningBecause lazy properties inherently rely on mutation of the property, they cannot be
lets. They are currently also limited to being members of structs and classes (they aren't allowed as local or global variables yet) and cannot be observed withwillSet/didSetyet. -
Closures can now specify a capture list to indicate with what strength they want to capture a value, and to bind a particular field value if they want to.
Closure capture lists are square-bracket delimited and specified before the (optional) argument list in a closure. Each entry may be specified as
weakorunownedto capture the value with a weak or unowned pointer, and may contain an explicit expression if desired. Some examples:takeClosure { print(self.title) } // strong capture takeClosure { [weak self] in print(self!.title) } // weak capture takeClosure { [unowned self] in print(self.title) } // unowned captureYou can also bind arbitrary expression to named values in the capture list. The expression is evaluated when the closure is formed, and captured with the specified strength. For example:
// weak capture of "self.parent" takeClosure { [weak tmp = self.parent] in print(tmp!.title) }The full form of a closure can take a signature (an argument list and optionally a return type) if needed. To use either the capture list or the signature, you must specify the context sensitive
inkeyword. Here is a (weird because there is no need forunowned) example of a closure with both:myNSSet.enumerateObjectsUsingBlock { [unowned self] (obj, stop) in self.considerWorkingWith(obj) } -
The word
withis now removed from the first keyword argument name if an initialized imported from Objective-C. For example, instead of buildingUIColoras:UIColor(withRed: r, green: g, blue: b, alpha: a)it will now be:
UIColor(red: r, green: g, blue: b, alpha: a) -
Dictionarycan be bridged toNSDictionaryand vice versa:-
NSDictionaryhas an implicit conversion toDictionary<NSObject, AnyObject>. It bridges in O(1), without memory allocation. -
Dictionary<K, V>has an implicit conversion toNSDictionary.Dictionary<K, V>bridges toNSDictionaryiff bothKandVare bridged. Otherwise, a runtime error is raised.Depending on
KandVthe operation can beO(1)without memory allocation, orO(N)with memory allocation.
-
-
Single-quoted literals are no longer recognized. Use double-quoted literals and an explicit type annotation to define
CharactersandUnicodeScalars:var ch: Character = "a" var us: UnicodeScalar = "a"
2014-05-09
-
The use of keyword arguments is now strictly enforced at the call site. For example, consider this method along with a call to it:
class MyColor { func mixColorWithRed(red: Float, green: Float, blue: Float) { /* ... */ } } func mix(color: MyColor, r: Float, g: Float, b: Float) { color.mixColorWithRed(r, g, b) }The compiler will now complain about the missing
green:andblue:labels, with a Fix-It to correct the code:color.swift:6:24: error: missing argument labels 'green:blue:' in call color.mixColorWithRed(r, g, b) ^ green: blue:The compiler handles missing, extraneous, and incorrectly-typed argument labels in the same manner. Recall that one can make a parameter a keyword argument with the back-tick or remove a keyword argument with the underscore.
class MyColor { func mixColor(`red: Float, green: Float, blue: Float) { /* ... */ } func mixColorGuess(red: Float, _ green: Float, _ blue: Float) { /* ... */ } } func mix(color: MyColor, r: Float, g: Float, b: Float) { color.mixColor(red: r, green: g, blue: b) // okay: all keyword arguments color.mixColorGuess(r, g, b) // okay: no keyword arguments }Arguments cannot be re-ordered unless the corresponding parameters have default arguments. For example, given:
func printNumber(`number: Int, radix: Int = 10, separator: String = ",") { }The following three calls are acceptable because only the arguments for defaulted parameters are re-ordered relative to each other:
printNumber(number: 256, radix: 16, separator: "_") printNumber(number: 256, separator: "_") printNumber(number: 256, separator: ",", radix: 16)However, this call:
printNumber(separator: ",", radix: 16, number: 256)results in an error due to the re-ordering:
printnum.swift:7:40: error: argument 'number' must precede argument 'separator' printNumber(separator: ",", radix: 16, number: 256) ~~~~~~~~~~~~~~ ^ ~~~ -
;can no longer be used to demarcate an empty case in a switch statement, usebreakinstead.
2014-05-07
-
The compiler's ability to diagnose many common kinds of type check errors has improved. (
expression does not type-checkhas been retired.) -
Ranges can be formed with floating point numbers, e.g.
0.0 .. 100.0. -
Convenience initializers are now spelled as
convenience initinstead of with the-> Selfsyntax. For example:class Foo { init(x : Int) {} // designated initializer convenience init() { self.init(42) } // convenience initializer }You still cannot declare designated initializers in extensions, only convenience initializers are allowed.
-
Reference types using the CoreFoundation runtime are now imported as class types. This means that Swift will automatically manage the lifetime of a
CFStringRefthe same way that it manages the lifetime of anNSString.In many common cases, this will just work. Unfortunately, values are returned from
CF-style APIs in a wide variety of ways, and unlike Objective-C methods, there simply isn't enough consistency for Swift to be able to safely apply the documented conventions universally. The framework teams have already audited many of the most importantCF-style APIs, and those APIs should be imported without a hitch into Swift. For all the APIs which haven't yet been audited, we must import return types using theUnmanagedtype. This type allows the programmer to control exactly how the object is passed.For example:
// CFBundleGetAllBundles() returns an Unmanaged<CFArrayRef>. // From the documentation, we know that it returns a +0 value. let bundles = CFBundleGetAllBundles().takeUnretainedValue() // CFRunLoopCopyAllModes() returns an Unmanaged<CFArrayRef>. // From the documentation, we know that it returns a +1 value. let modes = CFRunLoopCopyAllModes(CFRunLoopGetMain()).takeRetainedValue()You can also use
Unmanagedtypes to pass and return objects indirectly, as well as to generate unbalanced retains and releases if you really require them.The API of the Unmanaged type is still in flux, and your feedback would be greatly appreciated.
2014-05-03
-
The
@NSManagedattribute can be applied to the properties of anNSManagedObjectsubclass to indicate that they should be handled by CoreData:class Employee : NSManagedObject { @NSManaged var name: String @NSManaged var department: Department } -
The
@weakand@unownedattributes have become context sensitive keywords instead of attributes. To declare aweakorunownedpointer, use:weak var someOtherWindow : NSWindow? unowned var someWindow : NSWindow... with no
@on theweak/unowned.
2014-04-30
-
Swift now supports a
#elseifform for build configurations, e.g.:#if os(OSX) typealias SKColor = NSColor #elseif os(iOS) typealias SKColor = UIColor #else typealias SKColor = Green #endif -
You can now use the
trueandfalseconstants in build configurations, allowing you to emulate the C idioms of#if 0(but spelled#if false). -
breaknow breaks out of switch statements. -
It is no longer possible to specify
@mutatingas an attribute, you may only use it as a keyword, e.g.:struct Pair { var x, y : Int mutating func nuke() { x = 0; y = 0 } }The former
@!mutatingsyntax used to mark setters as non-mutating is now spelled with thenonmutatingkeyword. Both mutating and nonmutating are context sensitive keywords. -
NSLogis now available from Swift code. -
The parser now correctly handles expressions like
var x = Int[]()to create an empty array of integers. Previously you'd have to use syntax likeArray<Int>()to get this. Now that this is all working, please prefer to useInt[]consistently instead ofArray<Int>. -
Characteris the new character literal type:var x = 'a' // Infers 'Character' typeYou can force inference of
UnicodeScalarlike this:var scalar: UnicodeScalar = 'a'Charactertype represents a Unicode extended grapheme cluster (to put it simply, a grapheme cluster is what users think of as a character: a base plus any combining marks, or other cases explained in Unicode Standard Annex #29).
2014-04-22
-
Loops and switch statements can now carry labels, and you can
break/continueto those labels. These use conventional C-style label syntax, and should be dedented relative to the code they are in. An example:func breakContinue(x : Int) -> Int { Outer: for a in 0..1000 { Switch: switch x { case 42: break Outer case 97: continue Outer case 102: break Switch case 13: continue // continue always works on loops. case 139: break // break will break out of the switch (but see below) } } } -
We are changing the behavior of
breakto provide C-style semantics, to allow breaking out of a switch statement. Previously, break completely ignored switches so that it would break out of the nearest loop. In the example above,case 139would break out of theOuterloop, not theSwitch.In order to avoid breaking existing code, we're making this a compile time error instead of a silent behavior change. If you need a solution for the previous behavior, use labeled break.
This error will be removed in a week or two.
-
Cocoa methods and properties that are annotated with the
NS_RETURNS_INNER_POINTERattribute, including-[NSData bytes]and-[{NS,UI}Color CGColor], are now safe to use and follow the same lifetime extension semantics as ARC.
2014-04-18
-
Enabling/disabling of asserts
assert(condition, msg)is enabled/disabled dependent on the optimization level. In debug mode at
-O0asserts are enabled. At higher optimization levels asserts are disabled and no code is generated for them. However, asserts are always type checked even at higher optimization levels.Alternatively, assertions can be disabled/enabled by using the frontend flag
-assert-config Debug, or-assert-config Release. -
Added optimization flag
-Ofast. It disables all assertions (assert), and runtime overflow and type checks. -
The "selector-style" function and initializer declaration syntax is being phased out. For example, this:
init withRed(red: CGFloat) green(CGFloat) blue(CGFloat) alpha(CGFloat)will now be written as:
init(withRed red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)For each parameter, one can have both an argument API name (i.e.,
withRed, which comes first and is used at the call site) and an internal parameter name that follows it (i.e.red, which comes second and is used in the implementation). When the two names are the same, one can simply write the name once and it will be used for both roles (as withgreen,blue, andalphaabove). The underscore (_) can be used to mean "no name", as when the following function/method:func murderInRoom(room:String) withWeapon(weapon: String)is translated to:
func murderInRoom(_ room: String, withWeapon weapon: String)The compiler now complains when it sees the selector-style syntax and will provide Fix-Its to rewrite to the newer syntax.
Note that the final form of selector syntax is still being hammered out, but only having one declaration syntax, which will be very close to this, is a known.
-
Stored properties can now be marked with the
@NSCopyingattribute, which causes their setter to be synthesized with a copy tocopyWithZone:. This may only be used with types that conform to theNSCopyingprotocol, or option types thereof. For example:@NSCopying var myURL : NSURLThis fills the same niche as the (
copy) attribute on Objective-C properties.
2014-04-16
-
Optional variables and properties are now default-initialized to
nil:class MyClass { var cachedTitle: String? // "= nil" is implied } -
@IBOutlethas been improved in a few ways:-
IBOutletscan now be@uncheckedoptional. -
An
IBOutletdeclared as non-optional, i.e.,@IBOutlet var button: NSButtonwill be treated as an
@uncheckedoptional. This is considered to be the best practice way to write an outlet, unless you want to explicitly handle the null case - in which case, useNSButton?as the type. Either way, the= nilthat was formerly required is now implicit.
-
-
The precedence of
isandasis now higher than comparisons, allowing the following sorts of things to be written without parens:if x is NSButton && y is NSButtonCell { ... } if 3/4 as Float == 6/8 as Float { ... } -
Objective-C blocks are now transparently bridged to Swift closures. You never have to write
@objc_blockwhen writing Objective-C-compatible methods anymore. Block parameters are now imported as unchecked optional closure types, allowingnilto be passed.
2014-04-09
-
Dictionarychanges:-
Elementsare now tuples, so you can writefor (k, v) in d { // ... } -
keysandvaluesproperties, which areCollectionsprojecting the corresponding aspect of each element.Dictionaryindices are usable with theirkeysandvaluesproperties, so:for i in indices(d) { let (k, v) = d[i] assert(k == d.keys[i]) assert(v == d.values[i]) }
-
-
Semicolon can be used as a single no-op statement in otherwise empty cases in
switchstatements:switch x { case 1, 2, 3: print("x is 1, 2 or 3") default: ; } -
overrideis now a context sensitive keyword, instead of an attribute:class Base { var property: Int { return 0 } func instanceFunc() {} class func classFunc() {} } class Derived : Base { override var property: Int { return 1 } override func instanceFunc() {} override class func classFunc() {} }
2014-04-02
-
Prefix splitting for imported enums has been revised again due to feedback:
- If stripping off a prefix would leave an invalid identifier (like
10_4), leave one more word in the result than would otherwise be there (Behavior10_4). - If all enumerators have a
kprefix (forconstant) and the enum doesn't, thekshould not be considered when finding the common prefix. - If the enum name is a plural (like
NSSomethingOptions) and the enumerator names use the singular form (NSSomethingOptionMagic), this is considered a matching prefix (but only if nothing follows the plural).
- If stripping off a prefix would leave an invalid identifier (like
-
Cocoa APIs that take pointers to plain C types as arguments now get imported as taking the new
CMutablePointer<T>andCConstPointer<T>types instead ofUnsafePointer<T>. These new types allow implicit conversions from Swiftinoutparameters and from Swift arrays:let rgb = CGColorSpaceCreateDeviceRGB() // CGColorRef CGColorCreate(CGColorSpaceRef, const CGFloat*); let white = CGColorCreate(rgb, [1.0, 1.0, 1.0]) var s = 0.0, c = 0.0 // void sincos(double, double*, double*); sincos(M_PI/2, &s, &c)Pointers to pointers to ObjC classes, such as
NSError**, get imported asObjCMutablePointer<NSError?>. This type doesn't work with arrays, but accepts inouts ornil:var error: NSError? = nil let words = NSString.stringWithContentsOfFile("/usr/share/dict/words", encoding: .UTF8StringEncoding, error: &error)Voidpointer parameters can be passed an array or inout of any type:// + (NSData*)dataWithBytes:(const void*)bytes length:(NSUInteger)length; let data = NSData.dataWithBytes([1.5, 2.25, 3.125], length: sizeof(Double.self) * 3) var fromData = [0.0, 0.0, 0.0] // - (void)getBytes:(void*)bytes length:(NSUInteger)length; data.getBytes(&fromData, length: sizeof(Double.self) * 3)Note that we don't know whether an API reads or writes the C pointer, so you need to explicitly initialize values (like
sandcabove) even if you know that the API overwrites them.This pointer bridging only applies to arguments, and only works with well- behaved C and ObjC APIs that don't keep the pointers they receive as arguments around or do other dirty pointer tricks. Nonstandard use of pointer arguments still requires
UnsafePointer. -
Objective-C pointer types now get imported by default as the
@unchecked T?optional type. Swift class types no longer implicitly includenil.A value of
@unchecked T?can be implicitly used as a value ofT. Swift will implicitly cause a reliable failure if the value isnil, rather than introducing undefined behavior (as in Objective-C ivar accesses or everything in C/C++) or silently ignoring the operation (as in Objective-C message sends).A value of
@unchecked T?can also be implicitly used as a value ofT?, allowing you explicitly handle the case of anilvalue. For example, if you would like to just silently ignore a message send a la Objective-C, you can use the postfix?operator like so:fieldsForKeys[kHeroFieldKey]?.setEditable(true)This design allows you to isolate and handle
nilvalues in Swift code without requiring excessive "bookkeeping" boilerplate to use values that you expect to be non-nil.For now, we will continue to import C pointers as non-optional
UnsafePointerandC*Pointertypes; that will be evaluated separately.We intend to provide attributes for Clang to allow APIs to opt in to importing specific parameters, return types, etc. as either the explicit optional type
T?or the simple non-optional typeT. -
The "separated" call syntax, i.e.,
NSColor.colorWithRed(r) green(g) blue(b) alpha(a) UIColor.init withRed(r) green(g) blue(b) alpha(a)is being removed. The compiler will now produce an error and provide Fix-Its to rewrite calls to the "keyword-argument" syntax:
NSColor.colorWithRed(r, green: g, blue: b, alpha: a) UIColor(withRed: r, green:g, blue:b, alpha: a) -
The
objcattribute now optionally accepts a name, which can be used to provide the name for an entity as seen in Objective-C. For example:class MyType { var enabled: Bool { @objc(isEnabled) get { // ... } } }The
@objcattribute can be used to name initializers, methods, getters, setters, classes, and protocols. -
Methods, properties and subscripts in classes can now be marked with the
@finalattribute. This attribute prevents overriding the declaration in any subclass, and provides better performance (since dynamic dispatch is avoided in many cases).
2014-03-26
-
Attributes on declarations are no longer comma separated.
Old syntax:
@_silgen_name("foo"), @objc func bar() {}New syntax:
@_silgen_name("foo") @objcThe
,was vestigial when the attribute syntax consisted of bracket lists. -
switchnow always requires a statement after acaseordefault.Old syntax:
switch x { case .A: case .B(1): println(".A or .B(1)") default: // Ignore it. }New syntax:
switch x { case .A, .B(1): println(".A or .B(1)") default: () // Ignore it. }The following syntax can be used to introduce guard expressions for patterns inside the
case:switch x { case .A where isFoo(), .B(1) where isBar(): ... } -
Observing properties can now
@overrideproperties in a base class, so you can observe changes that happen to them.class MyAwesomeView : SomeBasicView { @override var enabled : Bool { didSet { println("Something changed") } } ... }Observing properties still invoke the base class getter/setter (or storage) when accessed.
-
An
ascast can now be forced using the postfix!operator without using parens:class B {} class D {} let b: B = D() // Before let d1: D = (b as D)! // After let d2: D = b as D!Casts can also be chained without parens:
// Before let b2: B = (((D() as B) as D)!) as B // After let b3: B = D() as B as D! as B -
ascan now be used inswitchcases to match the result of a checked cast:func printHand(hand: Any) { switch hand { case 1 as Int: print("ace") case 11 as Int: print("jack") case 12 as Int: print("queen") case 13 as Int: print("king") case let numberCard as Int: print("\(numberCard)") case let (a, b) as (Int, Int) where a == b: print("two ") printHand(a) print("s") case let (a, b) as (Int, Int): printHand(a) print(" and a ") printHand(b) case let (a, b, c) as (Int, Int, Int) where a == b && b == c: print("three ") printHand(a) print("s") case let (a, b, c) as (Int, Int, Int): printHand(a) print(", ") printHand(b) print(", and a ") printHand(c) default: print("unknown hand") } } printHand(1, 1, 1) // prints "three aces" printHand(12, 13) // prints "queen and a king" -
Enums and option sets imported from C/Objective-C still strip common prefixes, but the name of the enum itself is now taken into consideration as well. This keeps us from dropping important parts of a name that happen to be shared by all members.
// NSFileManager.h typedef NS_OPTIONS(NSUInteger, NSDirectoryEnumerationOptions) { NSDirectoryEnumerationSkipsSubdirectoryDescendants = 1UL << 0, NSDirectoryEnumerationSkipsPackageDescendants = 1UL << 1, NSDirectoryEnumerationSkipsHiddenFiles = 1UL << 2 } NS_ENUM_AVAILABLE(10_6, 4_0);// Swift let opts: NSDirectoryEnumerationOptions = .SkipsPackageDescendants -
initmethods in Objective-C protocols are now imported as initializers. To conform toNSCoding, you will now need to provideinit withCoder(aDecoder: NSCoder) { ... }rather than
func initWithCoder(aDecoder: NSCoder) { ... }
2014-03-19
-
When a class provides no initializers of its own but has default values for all of its stored properties, it will automatically inherit all of the initializers of its superclass. For example:
class Document { var title: String init() -> Self { self.init(withTitle: "Default title") } init withTitle(title: String) { self.title = title } } class VersionedDocument : Document { var version = 0 // inherits 'init' and 'init withTitle:' from Document }When one does provide a designated initializer in a subclass, as in the following example:
class SecureDocument : Document { var key: CryptoKey init withKey(key: CryptoKey) -> Self { self.init(withKey: key, title: "Default title") } init withKey(key: CryptoKey) title(String) { self.key = key super.init(withTitle: title) } }the compiler emits Objective-C method stubs for all of the designated initializers of the parent class that will abort at runtime if called, and which indicate which initializer needs to be implemented. This provides memory safety for cases where an Objective-C initializer (such as
-[Document init]in this example) appears to be inherited, but isn't actually implemented. -
nilmay now be used as a Selector value. This allows calls to Cocoa methods that acceptnilselectors. -
[]and[:]can now be used as the empty array and dictionary literal, respectively. Because these carry no information about their element types, they may only be used in a context that provides this information through type inference (e.g. when passing a function argument). -
Properties defined in classes are now dynamically dispatched and can be overridden with
@override. Currently@overrideonly works with computed properties overriding other computed properties, but this will be enhanced in coming weeks.
2014-03-12
-
The
didSetaccessor of an observing property now gets passed in the old value, so you can easily implement an action for when a property changes value. For example:class MyAwesomeView : UIView { var enabled : Bool = false { didSet(oldValue): if oldValue != enabled { self.needsDisplay = true } } ... } -
The implicit argument name for set and willSet property specifiers has been renamed from
(value)to(newValue). For example:var i : Int { get { return 42 } set { // defaults to (newValue) instead of (value) print(newValue) } } -
The magic identifier
__FUNCTION__can now be used to get the name of the current function as a string. Like__FILE__and__LINE__, if__FUNCTION__is used as a default argument, the function name of the caller is passed as the argument.func malkovich() { println(__FUNCTION__) } malkovich() // prints "malkovich" func nameCaller(name: String = __FUNCTION__) -> String { return name } func foo() { println(nameCaller()) // prints "foo" } func foo(x: Int) bar(y: Int) { println(nameCaller()) // prints "foo:bar:" }At top level,
__FUNCTION__gives the module name:println(nameCaller()) // prints your module name -
Selector-style methods can now be referenced without applying arguments using member syntax
foo.bar:bas:, for instance, to test for the availability of an optional protocol method:func getFrameOfObjectValueForColumn(ds: NSTableViewDataSource, tableView: NSTableView, column: NSTableColumn, row: Int) -> AnyObject? { if let getObjectValue = ds.tableView:objectValueForTableColumn:row: { return getObjectValue(tableView, column, row) } return nil } -
The compiler now warns about cases where a variable is inferred to have
AnyObject,AnyClass, or()type, since type inference can turn a simple mistake (e.g. failing to cast anAnyObjectwhen you meant to) into something with ripple effects. Here is a simple example:t.swift:4:5: warning: variable 'fn' inferred to have type '()', which may be unexpected var fn = abort() ^ t.swift:4:5: note: add an explicit type annotation to silence this warning var fn = abort() ^ : ()If you actually did intend to declare a variable of one of these types, you can silence this warning by adding an explicit type (indicated by the Fixit). See rdar://15263687 and rdar://16252090 for more rationale.
-
x.typehas been renamed tox.dynamicType, and you can usetypeas a regular identifier again.
2014-03-05
-
C macros that expand to a single constant string are now imported as global constants. Normal string literals are imported as
CString;NSStringliterals are imported asString. -
All values now have a
selfproperty, exactly equivalent to the value itself:let x = 0 let x2 = x.selfTypes also have a
selfproperty that is the type object for that type:let theClass = NSObject.self let theObj = theClass()References to type names are now disallowed outside of a constructor call or member reference; to get a type object as a value,
T.selfis required. This prevents the mistake of intending to construct an instance of a class but forgetting the parens and ending up with the class object instead:let x = MyObject // oops, I meant MyObject()... return x.description() // ...and I accidentally called +description // instead of -description -
Initializers are now classified as designated initializers, which are responsible for initializing the current class object and chaining via
super.init, and convenience initializers, which delegate to another initializer and can be inherited. For example:class A { var str: String init() -> Self { // convenience initializer self.init(withString: "hello") } init withString(str: String) { // designated initializer self.str = str } }When a subclass overrides all of its superclass's designated initializers, the convenience initializers are inherited:
class B { init withString(str: String) { // designated initializer super.init(withString: str) } // inherits A.init() }Objective-C classes that provide
NS_DESIGNATED_INITIALIZERannotations will have their init methods mapped to designated initializers or convenience initializers as appropriate; Objective-C classes withoutNS_DESIGNATED_INITIALIZERannotations have all of theirinitmethods imported as designated initializers, which is safe (but can be verbose for subclasses). Note that the syntax and terminology is still somewhat in flux. -
Initializers can now be marked as
requiredwith an attribute, meaning that every subclass is required to provide that initializer either directly or by inheriting it from a superclass. To constructclass View { @required init withFrame(frame: CGRect) { ... } } func buildView(subclassObj: View.Type, frame: CGRect) -> View { return subclassObj(withFrame: frame) } class MyView : View { @required init withFrame(frame: CGRect) { super.init(withFrame: frame) } } class MyOtherView : View { // error: must override init withFrame(CGRect). } -
Properties in Objective-C protocols are now correctly imported as properties. (Previously the getter and setter were imported as methods.)
-
Simple enums with no payloads, including
NS_ENUMs imported from Cocoa, now implicitly conform to the Equatable and Hashable protocols. This means they can be compared with the==and!=operators and can be used asDictionarykeys:enum Flavor { case Lemon, Banana, Cherry } assert(Flavor.Lemon == .Lemon) assert(Flavor.Banana != .Lemon) struct Profile { var sweet, sour: Bool } let flavorProfiles: Dictionary<Flavor, Profile> = [ .Lemon: Profile(sweet: false, sour: true ), .Banana: Profile(sweet: true, sour: false), .Cherry: Profile(sweet: true, sour: true ), ] assert(flavorProfiles[.Lemon].sour) -
valhas been removed. Long livelet! -
Values whose names clash with Swift keywords, such as Cocoa methods or properties named
class,protocol,type, etc., can now be defined and accessed by wrapping reserved keywords in backticks to suppress their builtin meaning:let `class` = 0 let `type` = 1 let `protocol` = 2 println(`class`) println(`type`) println(`protocol`) func foo(Int) `class`(Int) {} foo(0, `class`: 1)
2014-02-26
-
The
overrideattribute is now required when overriding a method, property, or subscript from a superclass. For example:class A { func foo() { } } class B : A { @override func foo() { } // 'override' is required here } -
We're renaming
valback tolet. The compiler accepts both for this week, next week it will just acceptlet. Please migrate your code this week, sorry for the back and forth on this. -
Swift now supports
#if,#elseand#endifblocks, along with target configuration expressions, to allow for conditional compilation within declaration and statement contexts.Target configurations represent certain static information about the compile-time build environment. They are implicit, hard-wired into the compiler, and can only be referenced within the conditional expression of an
#ifblock.Target configurations are tested against their values via a pseudo-function invocation expression, taking a single argument expressed as an identifier. The argument represents certain static build-time information.
There are currently two supported target configurations:
os, which can have the valuesOSXoriOSarch, which can have the valuesi386,x86_64,armandarm64Within the context of an
#ifblock's conditional expression, a target configuration expression can evaluate to eithertrueorfalse.For example:
#if arch(x86_64) println("Building for x86_64") #else println("Not building for x86_64") #endif class C { #if os(OSX) func foo() { // OSX stuff goes here } #else func foo() { // non-OSX stuff goes here } #endif }The conditional expression of an
#ifblock can be composed of one or more of the following expression types:- A unary expression, using
! - A binary expression, using
&&or|| - A parenthesized expression
- A target configuration expression
For example:
#if os(iOS) && !arch(I386) ... #endifNote that
#if/#else/#endifblocks do not constitute a preprocessor, and must form valid and complete expressions or statements. Hence, the following produces a parser error:class C { #if os(iOS) func foo() {} } #else func bar() {} func baz() {} } #endifAlso note that "active" code will be parsed, typechecked and emitted, while "inactive" code will only be parsed. This is why code in an inactive
#ifor#elseblock will produce parser errors for malformed code. This allows the compiler to detect basic errors in inactive regions.This is the first step to getting functionality parity with the important subset of the C preprocessor. Further refinements are planned for later.
- A unary expression, using
-
Swift now has both fully-closed ranges, which include their endpoint, and half-open ranges, which don't.
(swift) for x in 0...5 { print(x) } ; print('\n') // half-open range 01234 (swift) for x in 0..5 { print(x) } ; print('\n') // fully-closed range 012345 -
Property accessors have a new brace-based syntax, instead of using the former "label like" syntax. The new syntax is:
var computedProperty: Int { get { return _storage } set { _storage = value } } var implicitGet: Int { // This form still works. return 42 } var storedPropertyWithObservingAccessors: Int = 0 { willSet { ... } didSet { ... } } -
Properties and subscripts now work in protocols, allowing you to do things like:
protocol Subscriptable { subscript(idx1: Int, idx2: Int) -> Int { get set } var prop: Int { get } } func foo(s: Subscriptable) { return s.prop + s[42, 19] }These can be used for generic algorithms now as well.
-
The syntax for referring to the type of a type,
T.metatype, has been changed toT.Type. The syntax for getting the type of a value,typeof(x), has been changed tox.type. -
DynamicSelfis now calledSelf; the semantics are unchanged. -
destructorhas been replaced withdeinit, to emphasize that it is related toinit. We will refer to these asdeinitializers. We've also dropped the parentheses, i.e.:class MyClass { deinit { // release any resources we might have acquired, etc. } } -
Class methods defined within extensions of Objective-C classes can now refer to
self, including usinginstancetypemethods. As a result,NSMutableString,NSMutableArray, andNSMutableDictionaryobjects can now be created with their respective literals, i.e.,var dict: NSMutableDictionary = ["a" : 1, "b" : 2]
2014-02-19
-
The
Streamprotocol has been renamed back toGenerator,which is precedented in other languages and causes less confusion with I/O streaming. -
The
typekeyword was split into two:staticandclass. One can define static functions and static properties in structs and enums like this:struct S { static func foo() {} static var bar: Int = 0 } enum E { static func foo() {} }classkeyword allows one to define class properties and class methods in classes and protocols:class C { class func foo() {} class var bar: Int = 0 } protocol P { class func foo() {} class var bar: Int = 0 }When using
classandstaticin the extension, the choice of keyword depends on the type being extended:extension S { static func baz() {} } extension C { class func baz() {} } -
The
letkeyword is no longer recognized. Please move toval. -
The standard library has been renamed to
Swift(instead ofswift) to be more consistent with other modules on our platforms. -
NSIntegerand other types that are layout-compatible with Swift standard library types are now imported directly as those standard library types. -
Optional types now support a convenience method named "cache" to cache the result of a closure. For example:
class Foo { var _lazyProperty: Int? var property: Int { return _lazyProperty.cache { computeLazyProperty() } } }
2014-02-12
-
We are experimenting with a new message send syntax. For example:
SKAction.colorizeWithColor(SKColor.whiteColor()) colorBlendFactor(1.0) duration(0.0)When the message send is too long to fit on a single line, subsequent lines must be indented from the start of the statement or declaration. For example, this is a single message send:
SKAction.colorizeWithColor(SKColor.whiteColor()) colorBlendFactor(1.0) duration(0.0)while this is a message send to colorizeWithColor: followed by calls to
colorBlendFactorandduration(on self or to a global function):SKAction.colorizeWithColor(SKColor.whiteColor()) colorBlendFactor(1.0) // call to 'colorBlendFactor' duration(0.0) // call to 'duration' -
We are renaming the
letkeyword toval. Theletkeyword didn't work out primarily because it is not a noun, so "defining a let" never sounded right. We chosevaloverconstand other options becausevarandvalhave similar semantics (making syntactic similarity useful), becauseconsthas varied and sordid connotations in C that we don't want to bring over, and because we don't want to punish the "preferred" case with a longer keyword.For migration purposes, the compiler now accepts
letandvalas synonyms,letwill be removed next week. -
Selector arguments in function arguments with only a type are now implicitly named after the selector chunk that contains them. For example, instead of:
func addIntsWithFirst(first : Int) second(second : Int) -> Int { return first+second }you can now write:
func addIntsWithFirst(first : Int) second(Int) -> Int { return first+second }if you want to explicitly want to ignore an argument, it is recommended that you continue to use the
_to discard it, as in:func addIntsWithFirst(first : Int) second(_ : Int) -> Int {...} -
The
@inoutattribute in argument lists has been promoted to a context-sensitive keyword. Where before you might have written:func swap<T>(a : @inout T, b : @inout T) { (a, b) = (b, a) }You are now required to write:
func swap<T>(inout a : T, inout b : T) { (a, b) = (b, a) }We made this change because
inoutis a fundamental part of the type system, which attributes are a poor match for. The inout keyword is also orthogonal to thevarandletkeywords (which may be specified in the same place), so it fits naturally there. -
The
@mutatingattribute (which can be used on functions in structs, enums, and protocols) has been promoted to a context-sensitive keyword. Mutating struct methods are now written as:struct SomeStruct { mutating func f() {} } -
Half-open ranges (those that don't include their endpoint) are now spelled with three
.s instead of two, for consistency with Ruby.(swift) for x in 0...5 { print(x) } ; print('\n') // new syntax 01234Next week, we'll introduce a fully-closed range which does include its endpoint. This will provide:
(swift) for x in 0..5 { print(x) } ; print('\n') // coming soon 012345These changes are being released separately so that users have a chance to update their code before its semantics changes.
-
Objective-C properties with custom getters/setters are now imported into Swift as properties. For example, the Objective-C property
@property (getter=isEnabled) BOOL enabled;was previously imported as getter (
isEnabled) and setter (setEnabled) methods. Now, it is imported as a property (enabled). -
didSet/willSetproperties may now have an initial value specified:class MyAwesomeView : UIView { var enabled : Bool = false { // Initial value. didSet: self.needsDisplay = true } ... }they can also be used as non-member properties now, e.g. as a global variable or a local variable in a function.
-
Objective-C instancetype methods are now imported as methods that return Swift's
DynamicSelftype. WhileDynamicSelfis not generally useful for defining methods in Swift, importing to it eliminates the need for casting with the numerousinstancetypeAPIs, e.g.,let tileNode: SKSpriteNode = SKSpriteNode.spriteNodeWithTexture(tileAtlas.textureNamed("tile\(tileNumber).png"))!becomes
let tileNode = SKSpriteNode.spriteNodeWithTexture(tileAtlas.textureNamed("tile\(tileNumber).png"))DynamicSelfwill become more interesting in the coming weeks.
2014-02-05
-
ifandwhilestatements can now conditionally bind variables. If the condition of aniforwhilestatement is aletdeclaration, then the right-hand expression is evaluated as anOptionalvalue, and control flow proceeds by considering the binding to betrueif theOptionalcontains a value, orfalseif it is empty, and the variables are available in the true branch. This allows for elegant testing of dynamic types, methods, nullable pointers, and other Optional things:class B : NSObject {} class D : B { func foo() { println("we have a D") } } var b: B = D() if let d = b as D { d.foo() } var id: AnyObject = D() if let foo = id.foo { foo() } -
When referring to a member of an
AnyObject(orAnyClass) object and using it directly (such as calling it, subscripting, or accessing a property on it), one no longer has to write the?or!. The run-time check will be performed implicitly. For example:func doSomethingOnViews(views: NSArray) { for view in views { view.updateLayer() // no '!' needed } }Note that one can still test whether the member is available at runtime using
?, testing the optional result, or conditionally binding a variable to the resulting member. -
The
swiftcommand line tool can now create executables and libraries directly, just like Clang. Useswift main.swiftto create an executable andswift -emit-library -o foo.dylib foo.swiftto create a library. -
Object files emitted by Swift are not debuggable on their own, even if you compiled them with the
-goption. This was already true if you had multiple files in your project. To produce a debuggable Swift binary from the command line, you must compile and link in a single step withswift, or pass object files AND swiftmodule files back intoswiftafter compilation. (Or use Xcode.) -
importwill no longer import other source files, only built modules. -
The current directory is no longer implicitly an import path. Use
-I .if you have modules in your current directory.
2014-01-29
-
Properties in structs and classes may now have
willSet:anddidSet:observing accessors defined on them:For example, where before you may have written something like this in a class:
class MyAwesomeView : UIView { var _enabled : Bool // storage var enabled : Bool { // computed property get: return _enabled set: _enabled = value self.needDisplay = true } ... }you can now simply write:
class MyAwesomeView : UIView { var enabled : Bool { // Has storage & observing methods didSet: self.needDisplay = true } ... }Similarly, if you want notification before the value is stored, you can use
willSet, which gets the incoming value before it is stored:var x : Int { willSet(value): // value is the default and may be elided, as with set: println("changing from \(x) to \(value)") didSet: println("we've got a value of \(x) now.\n") }The
willSet/didSetobservers are triggered on any store to the property, except stores frominit(), destructors, or from within the observers themselves.Overall, a property now may either be "stored" (the default), "computed" (have a
get:and optionally aset:specifier), or an observed (willSet/didSet) property. It is not possible to have a custom getter or setter on an observed property, since they have storage.Two known-missing bits are:
- (rdar://problem/15920332) didSet/willSet variables need to allow initializers
- (rdar://problem/15922884) support non-member didset/willset properties
Because of the first one, for now, you need to explicitly store an initial value to the property in your
init()method. -
Objective-C properties with custom getter or setter names are (temporarily) not imported into Swift; the getter and setter will be imported individually as methods instead. Previously, they would appear as properties within the Objective-C class, but attempting to use the accessor with the customized name would result in a crash.
The long-term fix is tracked as (rdar://problem/15877160).
-
Computed 'type' properties (that is, properties of types, rather than of values of the type) are now permitted on classes, on generic structs and enums, and in extensions. Stored 'type' properties in these contexts remain unimplemented.
The implementation of stored 'type' properties is tracked as (rdar://problem/15915785) (for classes) and (rdar://problem/15915867) (for generic types).
-
The following command-line flags have been deprecated in favor of new spellings. The old spellings will be removed in the following week's build:
Old Spelling New Spelling -emit-llvm-emit-ir-triple-target-serialize-diagnostics-serialize-diagnostics-path -
Imported
NS_OPTIONStypes now have a default initializer which produces a value with no options set. They can also be initialized to the empty set withnil. These are equivalent:var x = NSMatchingOptions() var y: NSMatchingOptions = nil
2014-01-22
-
The swift binary no longer has an SDK set by default. Instead, you must do one of the following:
- pass an explicit
-sdk /path/to/sdk - set
SDKROOTin your environment - run
swiftthroughxcrun, which setsSDKROOTfor you
- pass an explicit
-
letdeclarations can now be used as struct/class properties. Aletproperty is mutable withininit(), and immutable everywhere else.class C { let x = 42 let y : Int init(y : Int) { self.y = y // ok, self.y is mutable in init() } func test() { y = 42 // error: 'y' isn't mutable } } -
The immutability model for structs and enums is complete, and arguments are immutable by default. This allows the compiler to reject mutations of temporary objects, catching common bugs. For example, this is rejected:
func setTo4(a : Double[]) { a[10] = 4.0 // error: 'a' isn't mutable } ... setTo4(someArray)since
ais semantically a copy of the array passed into the function. The proper fix in this case is to mark the argument is@inout, so the effect is visible in the caller:func setTo4(a : @inout Double[]) { a[10] = 4.0 // ok: 'a' is a mutable reference } ... setTo4(&someArray)Alternatively, if you really just want a local copy of the argument, you can mark it
var. The effects aren't visible in the caller, but this can be convenient in some cases:func doStringStuff(var s : String) { s += "foo" print(s) } -
Objective-C instance variables are no longer imported from headers written in Objective-C. Previously, they would appear as properties within the Objective-C class, but trying to access them would result in a crash. Additionally, their names can conflict with property names, which confuses the Swift compiler, and there are no patterns in our frameworks that expect you to access a parent or other class's instance variables directly. Use properties instead.
-
The
NSObjectprotocol is now imported under the nameNSObjectProtocol(rather thanNSObjectProto).
2014-01-15
-
Improved deallocation of Swift classes that inherit from Objective-C classes: Swift destructors are implemented as
-deallocmethods that automatically call the superclass's-dealloc. Stored properties are released right before the object is deallocated (using the same mechanism as ARC), allowing properties to be safely used in destructors. -
Subclasses of
NSManagedObjectare now required to provide initial values for each of their stored properties. This permits initialization of these stored properties directly after +alloc to provide memory safety with CoreData's dynamic subclassing scheme. -
letdeclarations are continuing to make slow progress. Curried and selector-style arguments are now immutable by default, andletdeclarations now get proper debug information.
2014-01-08
-
The
statickeyword changed totype. One can now define "type functions" and "type variables" which are functions and variables defined on a type (rather than on an instance of the type), e.g.,class X { type func factory() -> X { ... } type var version: Int }The use of
staticwas actively misleading, since type methods on classes are dynamically dispatched (the same as Objective-C+methods).Note that
typeis a context-sensitive keyword; it can still be used as an identifier. -
Strings have a new native UTF-16 representation that can be converted back and forth to
NSStringat minimal cost. String literals are emitted as UTF-16 for string types that support it (including Swift'sString). -
Initializers can now delegate to other initializers within the same class by calling
self.init. For example:class A { } class B : A { var title: String init() { // note: cannot access self before delegating self.init(withTitle: "My Title") } init withTitle(title: String) { self.title = title super.init() } } -
Objective-C protocols no longer have the
Protosuffix unless there is a collision with a class name. For example,UITableViewDelegateis now imported asUITableViewDelegaterather thanUITableViewDelegateProto. Where there is a conflict with a class, the protocol will be suffixed withProto, as inNSObject(the class) andNSObjectProto(the protocol).
2014-01-01
-
Happy New Year
-
Division and remainder arithmetic now trap on overflow. Like with the other operators, one can use the "masking" alternatives to get non-trapping behavior. The behavior of the non-trapping masking operators is defined:
x &/ 0 == 0 x &% 0 == 0 SIGNED_MIN_FOR_TYPE &/ -1 == -1 // i.e. Int8: -0x80 / -1 == -0x80 SIGNED_MIN_FOR_TYPE &% -1 == 0 -
Protocol conformance checking for
@mutatingmethods is now implemented: an@mutatingstruct method only fulfills a protocol requirement if the protocol method was itself marked@mutating:protocol P { func nonmutating() @mutating func mutating() } struct S : P { // Error, @mutating method cannot implement non-@mutating requirement. @mutating func nonmutating() {} // Ok, mutating allowed, but not required. func mutating() {} }As before, class methods never need to be marked
@mutating(and indeed, they aren't allowed to be marked as such).
2013-12-25
-
Merry Christmas
-
The setters of properties on value types (structs/enums) are now
@mutatingby default. To mark a setter non-mutating, use the@!mutatingattribute. -
Compiler inserts calls to
super.init()into the class initializers that do not call any initializers explicitly. -
A
mapmethod with the semantics of Haskell'sfmapwas added toArray<T>. Map applies a functionf: T->Uto the values stored in the array and returns an Array. So,(swift) func names(x: Int[]) -> String[] { return x.map { "<" + String($0) + ">" } } (swift) names(Array<Int>()) // r0 : String[] = [] (swift) names([3, 5, 7, 9]) // r1 : String[] = ["<3>", "<5>", "<7>", "<9>"]
2013-12-18
-
Global variables and static properties are now lazily initialized on first use. Where you would use
dispatch_onceto lazily initialize a singleton object in Objective-C, you can simply declare a global variable with an initializer in Swift. Likedispatch_once, this lazy initialization is thread safe.Unlike C++ global variable constructors, Swift global variables and static properties now never emit static constructors (and thereby don't raise build warnings). Also unlike C++, lazy initialization naturally follows dependency order, so global variable initializers that cross module boundaries don't have undefined behavior or fragile link order dependencies.
-
Swift has the start of an immutability model for value types. As part of this, you can now declare immutable value bindings with a new
letdeclaration, which is semantically similar to defining a get-only property:let x = foo() print(x) // ok x = bar() // error: cannot modify an immutable value swap(&x, &y) // error: cannot pass an immutable value as @inout parameter x.clear() // error: cannot call mutating method on immutable value getX().clear() // error: cannot mutate a temporaryIn the case of bindings of class type, the bound object itself is still mutable, but you cannot change the binding.
let r = Rocket() r.blastOff() // Ok, your rocket is mutable. r = Rocket() // error: cannot modify an immutable binding.In addition to the
letdeclaration itself,selfon classes, and a few other minor things have switched to immutable bindings.A pivotal part of this is that methods of value types (structs and enums) need to indicate whether they can mutate self - mutating methods need to be disallowed on let values (and get-only property results, temporaries, etc) but non-mutating methods need to be allowed. The default for a method is that it does not mutate
self, though you can opt into mutating behavior with a new@mutatingattribute:struct MyWeirdCounter { var count : Int func empty() -> Bool { return count == 0 } @mutating func reset() { count = 0 } ... } let x = MyWeirdCounter() x.empty() // ok x.reset() // error, cannot mutate immutable 'let' valueOne missing piece is that the compiler does not yet reject mutations of self in a method that isn't marked
@mutating. That will be coming soon. Related to methods are properties. Getters and setters can be marked mutating as well:extension MyWeirdCounter { var myproperty : Int { get: return 42 @mutating set: count = value*2 } }The intention is for setters to default to mutating, but this has not been implemented yet. There is more to come here.
-
A
mapmethod with the semantics of Haskell'sfmapwas added toOptional<T>. Map applies a functionf: T->Uto any value stored in anOptional<T>, and returns anOptional<U>. So,(swift) func nameOf(x: Int?) -> String? { return x.map { "<" + String($0) + ">" } } (swift) (swift) var no = nameOf(.None) // Empty optional in... // no : String? = <unprintable value> (swift) no ? "yes" : "no" // ...empty optional out // r0 : String = "no" (swift) (swift) nameOf(.Some(42)) // Non-empty in // r1 : String? = <unprintable value> (swift) nameOf(.Some(42))! // Non-empty out // r2 : String = "<42>" -
Cocoa types declared with the
NS_OPTIONSmacro are now available in Swift. LikeNS_ENUMtypes, their values are automatically shortened based on the common prefix of the value names in Objective-C, and the name can be elided when type context provides it. They can be used inifstatements using the&,|,^, and~operators as in C:var options: NSJSONWritingOptions = .PrettyPrinted if options & .PrettyPrinted { println("pretty-printing enabled") }We haven't yet designed a convenient way to author
NS_OPTIONS-like types in Swift.
2013-12-11
-
Objective-C
idis now imported asAnyObject(formerly known asDynamicLookup), Objective-CClassis imported asAnyClass. -
The casting syntax
x as Tnow permits both implicit conversions (in which case it produces a value of typeT) and for runtime-checked casts (in which case it produces a value of typeT?that will be.Some(casted x)on success and.Noneon failure). An example:func f(x: AnyObject, y: NSControl) { var view = y as NSView // has type 'NSView' var maybeView = x as NSView // has type NSView? } -
The precedence levels of binary operators has been redefined, with a much simpler model than C's. This is with a goal to define away classes of bugs such as those caught by Clang's
-Wparentheseswarnings, and to make it actually possible for normal humans to reason about the precedence relationships without having to look them up.We ended up with 6 levels, from tightest binding to loosest:
exponentiative: <<, >> multiplicative: *, /, %, & additive: +, -, |, ^ comparative: ==, !=, <, <=, >=, > conjunctive: && disjunctive: || -
The
Enumerableprotocol has been renamedSequence. -
The
Chartype has been renamedUnicodeScalar. The preferred unit of string fragments for users is calledCharacter. -
Initialization semantics for classes, structs and enums init methods are now properly diagnosed by the compiler. Instance variables now follow the same initialization rules as local variables: they must be defined before use. The initialization model requires that all properties with storage in the current class be initialized before
super.initis called (or, in a root class, before any method is called onself,and before the final return).For example, this will yield an error:
class SomeClass : SomeBase { var x : Int init() { // error: property 'self.x' not initialized at super.init call super.init() } }A simple fix for this is to change the property definition to
var x = 0, or to explicitly assign to it before callingsuper.init(). -
Relatedly, the compiler now diagnoses incorrect calls to
super.init(). It validates that any path through an initializer callssuper.init()exactly once, that all ivars are defined before the call to super.init, and that any uses which require the entire object to be initialized come after thesuper.initcall. -
Type checker performance has improved considerably (but we still have much work to do here).
2013-12-04
- The "slice" versus "array" subtlety is now dead.
Slice<T>has been folded intoArray<T>andT[]is just sugar forArray<T>.
2013-11-20
-
Unreachable code warning has been added:
var y: Int = 1 if y == 1 { // note: condition always evaluates to true return y } return 1 // warning: will never be executed -
Overflows on integer type conversions are now detected at runtime and, when dealing with constants, at compile time:
var i: Int = -129 var i8 = Int8(i) // error: integer overflows when converted from 'Int' to 'Int8' var si = Int8(-1) var ui = UInt8(si) // error: negative integer cannot be converted to unsigned type 'UInt8' -
defkeyword was changed back tofunc.
2013-11-13
-
Objective-C-compatible protocols can now contain optional requirements, indicated by the
@optionalattribute:@class_protocol @objc protocol NSWobbling { @optional def wobble() }A class that conforms to the
NSWobblingprotocol above can (but does not have to) implementwobble. When referring to thewobblemethod for a value of typeNSWobbling(or a value of generic type that is bounded byNSWobbling), the result is an optional value indicating whether the underlying object actually responds to the given selector, using the same mechanism as messagingid. One can use!to assume that the method is always there,?to chain the optional, or conditional branches to handle each case distinctly:def tryToWobble(w : NSWobbling) { w.wobble() // error: cannot call a value of optional type w.wobble!() // okay: calls -wobble, but fails at runtime if not there w.wobble?() // okay: calls -wobble only if it's there, otherwise no-op if w.wobble { // okay: we know -wobble is there } else { // okay: we know -wobble is not there } } -
Enums from Cocoa that are declared with the
NS_ENUMmacro are now imported into Swift as Swift enums. Like all Swift enums, the constants of the Cocoa enum are scoped as members of the enum type, so the importer strips off the common prefix of all of the constant names in the enum when forming the Swift interface. For example, this Objective-C declaration:typedef NS_ENUM(NSInteger, NSComparisonResult) { NSOrderedAscending, NSOrderedSame, NSOrderedDescending, };shows up in Swift as:
enum NSComparisonResult : Int { case Ascending, Same, Descending }The
enumcases can then take advantage of type inference from context. In Objective-C, you would write:NSNumber *foo = [NSNumber numberWithInt: 1]; NSNumber *bar = [NSNumber numberWithInt: 2]; switch ([foo compare: bar]) { case NSOrderedAscending: NSLog(@"ascending\n"); break; case NSOrderedSame: NSLog(@"same\n"); break; case NSOrderedDescending: NSLog(@"descending\n"); break; }In Swift, this becomes:
var foo: NSNumber = 1 var bar: NSNumber = 2 switch foo.compare(bar) { case .Ascending: println("ascending") case .Same: println("same") case .Descending: println("descending") } -
Work has begun on implementing static properties. Currently they are supported for nongeneric structs and enums.
struct Foo { static var foo: Int = 2 } enum Bar { static var bar: Int = 3 } println(Foo.foo) println(Bar.bar)
2013-11-06
-
funckeyword was changed todef. -
Implicit conversions are now allowed from an optional type
T?to another optional typeU?ifTis implicitly convertible toU. For example, optional subclasses convert to their optional base classes:class Base {} class Derived : Base {} var d: Derived? = Derived() var b: Base? = d
2013-10-30
-
Type inference for variables has been improved, allowing any variable to have its type inferred from its initializer, including global and instance variables:
class MyClass { var size = 0 // inferred to Int } var name = "Swift"Additionally, the arguments of a generic type can also be inferred from the initializer:
// infers Dictionary<String, Int> var dict: Dictionary = ["Hello": 1, "World": 2]
2013-10-23
-
Missing return statement from a non-
Voidfunction is diagnosed as an error. -
Vector<T>has been replaced withArray<T>. This is a complete rewrite to use value-semantics and copy-on-write behavior. The former means that you never need to defensively copy again (or remember to attribute a property as "copy") and the latter yields better performance than defensive copying.Dictionary<T>is next. -
switchcan now pattern-match into structs and classes, using the syntaxcase Type(property1: pattern1, property2: pattern2, ...):.struct Point { var x, y: Double } struct Size { var w, h: Double } struct Rect { var origin: Point; var size: Size } var square = Rect(Point(0, 0), Size(10, 10)) switch square { case Rect(size: Size(w: var w, h: var h)) where w == h: println("square") case Rect(size: Size(w: var w, h: var h)) where w > h: println("long rectangle") default: println("tall rectangle") }Currently only stored properties ("ivars" in ObjC terminology) are supported by the implementation.
-
Array and dictionary literals allow an optional trailing comma:
var a = [1, 2,] var d = ["a": 1, "b": 2,]
2013-10-16
-
Unlike in Objective-C, objects of type
idin Swift do not implicitly convert to any class type. For example, the following code is ill-formed:func getContentViewBounds(window : NSWindow) -> NSRect { var view : NSView = window.contentView() // error: 'id' doesn't implicitly convert to NSView return view.bounds() }because
contentView()returns anid. One can now use the postfix!operator to allow an object of typeidto convert to any class type, e.g.,func getContentViewBounds(window : NSWindow) -> NSRect { var view : NSView = window.contentView()! // ok: checked conversion to NSView return view.bounds() }The conversion is checked at run-time, and the program will fail if the object is not an NSView. This is shorthand for
var view : NSView = (window.contentView() as NSView)!which checks whether the content view is an
NSView(via theas NSView). That operation returns an optionalNSView(writtenNSView?) and the!operation assumes that the cast succeeded, i.e., that the optional has a value in it. -
The unconditional checked cast syntax
x as! Thas been removed. Many cases where conversion fromidis necessary can now be handled by postfix!(see above). Fully general unconditional casts can still be expressed usingasand postfix!together,(x as T)!. -
The old "square bracket" attribute syntax has been removed.
-
Overflows on construction of integer and floating point values from integer literals that are too large to fit the type are now reported by the compiler. Here are some examples:
var x = Int8(-129) // error: integer literal overflows when stored into 'Int8' var y : Int = 0xFFFF_FFFF_FFFF_FFFF_F // error: integer literal overflows when stored into 'Int'Overflows in constant integer expressions are also reported by the compiler.
var x : Int8 = 125 var y : Int8 = x + 125 // error: arithmetic operation '125 + 125' (on type 'Int8') results in // an overflow -
Division by zero in constant expressions is now detected by the compiler:
var z: Int = 0 var x = 5 / z // error: division by zero -
Generic structs with type parameters as field types are now fully supported.
struct Pair<T, U> { var first: T var second: U }
2013-10-09
-
Autorelease pools can now be created using the
autoreleasepoolfunction.autoreleasepool { // code }Note that the wrapped code is a closure, so constructs like
breakandcontinueandreturndo not behave as they would inside an Objective-C@autoreleasepoolstatement. -
Enums can now declare a "raw type", and cases can declare "raw values", similar to the integer underlying type of C enums:
// Declare the underlying type as in Objective-C or C++11, with // ': Type' enum AreaCode : Int { // Assign explicit values to cases with '=' case SanFrancisco = 415 case EastBay = 510 case Peninsula = 650 case SanJose = 408 // Values are also assignable by implicit auto-increment case Galveston // = 409 case Baltimore // = 410 }This introduces
fromRawandtoRawmethods on the enum to perform conversions from and to the raw type:/* As if declared: extension AreaCode { // Take a raw value, and produce the corresponding enum value, // or None if there is no corresponding enum value static func fromRaw(raw:Int) -> AreaCode? // Return the corresponding raw value for 'self' func toRaw() -> Int } */ AreaCode.fromRaw(415) // => .Some(.SanFrancisco) AreaCode.fromRaw(111) // => .None AreaCode.SanJose.toRaw() // => 408Raw types are not limited to integer types--they can additionally be character, floating-point, or string values:
enum State : String { case CA = "California" case OR = "Oregon" case WA = "Washington" } enum SquareRootOfInteger : Float { case One = 1.0 case Two = 1.414 case Three = 1.732 case Four = 2.0 }Raw types are currently limited to simple C-like enums with no payload cases. The raw values are currently restricted to simple literal values; expressions such as
1 + 1or references to other enum cases are not yet supported. Raw values are also currently required to be unique for each case in an enum.Enums with raw types implicitly conform to the
RawRepresentableprotocol, which exposes the fromRaw and toRaw methods to generics:protocol RawRepresentable { typealias RawType static func fromRaw(raw: RawType) -> Self? func toRaw() -> RawType } -
Attribute syntax has been redesigned (see (rdar://10700853) and (rdar://14462729)) so that attributes now precede the declaration and use the
@character to signify them. Where before you might have written:func [someattribute=42] foo(a : Int) {}you now write:
@someattribute=42 func foo(a : Int) {}This flows a lot better (attributes don't push the name for declarations away), and means that square brackets are only used for array types, collection literals, and subscripting operations.
-
The
forloop now uses the Generator protocol instead of theEnumeratorprotocol to iterate a sequence. This protocol looks like this:protocol Generator { typealias Element func next() -> Element? }The single method
next()advances the generator and returns an Optional, which is either.Some(value), wrapping the next value out of the underlying sequence, or.Noneto signal that there are no more elements. This is an improvement over the previous Enumerator protocol because it eliminates the separateisEmpty()query and better reflects the semantics of ephemeral sequences like un-buffered input streams.
2013-10-02
-
The
[byref]attribute has been renamed to[inout]. When applied to a logical property, the getter is invoked before a call and the setter is applied to write back the result.inoutconveys this better and aligns with existing Objective-C practice better. -
[inout]arguments can now be captured into closures. The semantics of a inout capture are that the captured variable is an independent local variable of the callee, and the inout is updated to contain the value of that local variable at function exit.In the common case, most closure arguments do not outlive the duration of their callee, and the observable behavior is unchanged. However, if the captured variable outlives the function, you can observe this. For example, this code:
func foo(x : [inout] Int) -> () -> Int { func bar() -> Int { x += 1 return x } // Call 'bar' once while the inout is active. bar() return bar } var x = 219 var f = foo(&x) // x is updated to the value of foo's local x at function exit. println("global x = \(x)") // These calls only update the captured local 'x', which is now independent // of the inout parameter. println("local x = \(f())") println("local x = \(f())") println("local x = \(f())") println("global x = \(x)")will print:
global x = 220 local x = 221 local x = 222 local x = 223 global x = 220In no case will you end up with a dangling pointer or other unsafe construct.
-
x as Tnow performs a checked cast toT?, producing.Some(t)if the cast succeeds, or.Noneif the cast fails. -
The ternary expression (
x ? y : z) now requires whitespace between the first expression and the question mark. This permits?to be used as a postfix operator. -
A significant new piece of syntactic sugar has been added to ease working with optional values. The
?postfix operator is analogous to!, but instead of asserting on None, it causes all the following postfix operators to get skipped and returnNone.In a sense, this generalizes (and makes explicit) the Objective-C behavior where message sends to
nilsilently produce the zero value of the result.For example, this code
object?.parent.notifyChildEvent?(object!, .didExplode)first checks whether
objecthas a value; if so, it drills to its parent and checks whether that object implements thenotifyChildEventmethod; if so, it calls that method. (Note that we do not yet have generalized optional methods.)This code:
var titleLength = object?.title.lengthchecks whether
objecthas a value and, if so, asks for the length of its title.titleLengthwill have typeInt?, and ifobjectwas missing, the variable will be initialized to None. -
Objects with type
idcan now be used as the receiver of property accesses and subscript operations to get (but not set) values. The result is of optional type. For example, for a variableobjof typeid, the expressionobj[0]will produce a value of type
id, which will either contain the result of the message send objectAtIndexedSubscript(0) (wrapped in an optional type) or, if the object does not respond toobjectAtIndexedSubscript:, an empty optional. The same approach applies to property accesses. -
_can now be used not only invarbindings, but in assignments as well, to ignore elements of a tuple assignment, or to explicitly ignore values.var a = (1, 2.0, 3) var x = 0, y = 0 _ = a // explicitly load and discard 'a' (x, _, y) = a // assign a.0 to x and a.2 to y
2013-09-24
-
The
unionkeyword has been replaced withenum. Unions and enums are semantically identical in swift (the former just has data associated with its discriminators) andenumis the vastly more common case. For more rationale, please see docs/proposals/Enums.rst -
The Optional type
T?is now represented as anenum:enum Optional<T> { case None case Some(T) }This means that, in addition to the existing Optional APIs, it can be pattern-matched with switch:
var x : X?, y : Y? switch (x, y) { // Both are present case (.Some(var a), .Some(var b)): println("both") // One is present case (.Some, .None): case (.None, .Some): println("one") // Neither is present case (.None, .None): println("neither") } -
Enums now allow multiple cases to be declared in a comma-separated list in a single
casedeclaration:enum Color { case Red, Green, Blue } -
The Objective-C
idandClasstypes now support referring to methods declared in any class or protocol without a downcast. For example, given a variablesenderof typeid, one can refer to-isEqual: with:sender.isEqualThe actual object may or may not respond to
-isEqual, so this expression returns result of optional type whose value is determined via a compiler-generated-respondsToSelectorsend. When it succeeds, the optional contains the method; when it fails, the optional is empty.To safely test the optional, one can use, e.g.,
var senderIsEqual = sender.isEqual if senderIsEqual { // this will never trigger an "unrecognized selector" failure var equal = senderIsEqual!(other) } else { // sender does not respond to -isEqual: }When you know that the method is there, you can use postfix
!to force unwrapping of the optional, e.g.,sender.isEqual!(other)This will fail at runtime if in fact sender does not respond to
-isEqual:. We have some additional syntactic optimizations planned for testing an optional value and handling both the success and failure cases concisely. Watch this space. -
Weak references now always have optional type. If a weak variable has an explicit type, it must be an optional type:
var [weak] x : NSObject?If the variable is not explicitly typed, its type will still be inferred to be an optional type.
-
There is now an implicit conversion from
TtoT?.
2013-09-17
-
Constructor syntax has been improved to align better with Objective-C's
initmethods. Theconstructorkeyword has been replaced withinit, and the selector style of declaration used for func declarations is now supported. For example:class Y : NSObject { init withInt(i : Int) string(s : String) { super.init() // call superclass initializer } }One can use this constructor to create a
Yobject with, e.g.,Y(withInt:17, string:"Hello")Additionally, the rules regarding the selector corresponding to such a declaration have been revised. The selector for the above initializer is
initWithInt:string:; the specific rules are described in the documentation.Finally, Swift initializers now introduce Objective-C entry points, so a declaration such as:
class X : NSObject { init() { super.init() } }Overrides
NSObject's-initmethod (which it calls first) as well as introducing the 'allocating' entry point so that one can create a newXinstance with the syntaxX(). -
Variables in top-level code (i.e. scripts, but not global variables in libraries) that lack an initializer now work just like local variables: they must be explicitly assigned-to sometime before any use, instead of being default constructed. Instance variables are still on the TODO list.
-
Generic unions with a single payload case and any number of empty cases are now implemented, for example:
union Maybe<T> { case Some(T) case None } union Tristate<T> { case Initialized(T) case Initializing case Uninitialized }Generic unions with multiple payload cases are still not yet implemented.
2013-09-11
-
The implementation now supports partial application of class and struct methods:
(swift) class B { func foo() { println("B") } } (swift) class D : B { func foo() { println("D") } } (swift) var foo = B().foo // foo : () -> () = <unprintable value> (swift) foo() B (swift) foo = D().foo (swift) foo() DSupport for partial application of Objective-C class methods and methods in generic contexts is still incomplete.
2013-09-04
-
Local variable declarations without an initializer are no longer implicitly constructed. The compiler now verifies that they are initialized on all paths leading to a use of the variable. This means that constructs like this are now allowed:
var p : SomeProtocol if whatever { p = foo() } else { p = bar() }where before, the compiler would reject the definition of
psaying that it needed an initializer expression.Since all local variables must be initialized before use, simple things like this are now rejected as well:
var x : Int print(x)The fix is to initialize the value on all paths, or to explicitly default initialize the value in the declaration, e.g. with
var x = 0or withvar x = Int()(which works for any default-constructible type). -
The implementation now supports unions containing protocol types and weak reference types.
-
The type annotation syntax,
x as T, has been removed from the language. The checked cast operationsx as! Tandx is Tstill remain.
2013-08-28
-
thishas been renamed toself. Similarly,Thishas been renamed toSelf. -
Swift now supports unions. Unlike C unions, Swift's
unionis type-safe and always knows what type it contains at runtime. Union members are labeled usingcasedeclarations; each case may have a different set of types or no type:union MaybeInt { case Some(Int) case None } union HTMLTag { case A(href:String) case IMG(src:String, alt:String) case BR }Each
casewith a type defines a static constructor function for the union type.casedeclarations without types become static members:var br = HTMLTag.BR var a = HTMLTag.A(href:"http://www.apple.com/") // 'HTMLTag' scope deduced for '.IMG' from context var img : HTMLTag = .IMG(src:"http://www.apple.com/mac-pro.png", alt:"The new Mac Pro")Cases can be pattern-matched using
switch:switch tag { case .BR: println("<br>") case .IMG(var src, var alt): println("<img src=\"\(escape(src))\" alt=\"\(escape(alt))\">") case .A(var href): println("<a href=\"\(escape(href))\">") }Due to implementation limitations, recursive unions are not yet supported.
-
Swift now supports autolinking, so importing frameworks or Swift libraries should no longer require adding linker flags or modifying your project file.
2013-08-14
-
Swift now supports weak references by applying the
[weak]attribute to a variable declaration.(swift) var x = NSObject() // x : NSObject = <NSObject: 0x7f95d5804690> (swift) var [weak] w = x // w : NSObject = <NSObject: 0x7f95d5804690> (swift) w == nil // r2 : Bool = false (swift) x = NSObject() (swift) w == nil // r3 : Bool = trueSwift also supports a special form of weak reference, called
[unowned], for references that should never benilbut are required to be weak to break cycles, such as parent or sibling references. Accessing an[unowned]reference asserts that the reference is still valid and implicitly promotes the loaded reference to a strong reference, so it does not need to be loaded and checked for nullness before use like a true[weak]reference.class Parent { var children : Array<Child> func addChild(c:Child) { c.parent = this children.append(c) } } class Child { var [unowned] parent : Parent }
2013-07-31
-
Numeric literals can now use underscores as separators. For example:
var billion = 1_000_000_000 var crore = 1_00_00_000 var MAXINT = 0x7FFF_FFFF_FFFF_FFFF var SMALLEST_DENORM = 0x0.0000_0000_0000_1p-1022 -
Types conforming to protocols now must always declare the conformance in their inheritance clause.
-
The build process now produces serialized modules for the standard library, greatly improving build times.
2013-07-24
-
Arithmetic operators
+,-,*, and/on integer types now do overflow checking and trap on overflow. A parallel set of masking operators,&+,&-,&*, and&/, are defined to perform two's complement wrapping arithmetic for all signed and unsigned integer types. -
Debugger support. Swift has a
-gcommand line switch that turns on debug info for the compiled output. Using the standard lldb debugger this will allow single-stepping through Swift programs, printing backtraces, and navigating through stack frames; all in sync with the corresponding Swift source code. An unmodified lldb cannot inspect any variables.Example session:
$ echo 'println("Hello World")' >hello.swift $ swift hello.swift -c -g -o hello.o $ ld hello.o "-dynamic" "-arch" "x86_64" "-macosx_version_min" "10.9.0" \ -framework Foundation lib/swift/libswift_stdlib_core.dylib \ lib/swift/libswift_stdlib_posix.dylib -lSystem -o hello $ lldb hello Current executable set to 'hello' (x86_64). (lldb) b top_level_code Breakpoint 1: where = hello`top_level_code + 26 at hello.swift:1, addre... (lldb) r Process 38592 launched: 'hello' (x86_64) Process 38592 stopped * thread #1: tid = 0x1599fb, 0x0000000100000f2a hello`top_level_code + ... frame #0: 0x0000000100000f2a hello`top_level_code + 26 at hello.shi... -> 1 println("Hello World") (lldb) bt * thread #1: tid = 0x1599fb, 0x0000000100000f2a hello`top_level_code + ... frame #0: 0x0000000100000f2a hello`top_level_code + 26 at hello.shi... frame #1: 0x0000000100000f5c hello`main + 28 frame #2: 0x00007fff918605fd libdyld.dylib`start + 1 frame #3: 0x00007fff918605fd libdyld.dylib`start + 1Also try
s,n,up,down.
2013-07-17
-
Swift now has a
switchstatement, supporting pattern matching of multiple values with variable bindings, guard expressions, and range comparisons. For example:func classifyPoint(point:(Int, Int)) { switch point { case (0, 0): println("origin") case (_, 0): println("on the x axis") case (0, _): println("on the y axis") case (var x, var y) where x == y: println("on the y = x diagonal") case (var x, var y) where -x == y: println("on the y = -x diagonal") case (-10..10, -10..10): println("close to the origin") case (var x, var y): println("length \(sqrt(x*x + y*y))") } }
2013-07-10
-
Swift has a new closure syntax. The new syntax eliminates the use of pipes. Instead, the closure signature is written the same way as a function type and is separated from the body by the
inkeyword. For example:sort(fruits) { (lhs : String, rhs : String) -> Bool in return lhs > rhs }When the types are omitted, one can also omit the parentheses, e.g.,
sort(fruits) { lhs, rhs in lhs > rhs }Closures with no parameters or that use the anonymous parameters (
$0,$1, etc.) don't need thein, e.g.,sort(fruits) { $0 > $1 } -
nilcan now be used without explicit casting. Previously,nilhad typeNSObject, so one would have to write (e.g.)nil as! NSArrayto create anilNSArray. Now,nilpicks up the type of its context. -
POSIX.EnvironmentVariablesandswift.CommandLineArgumentsglobal variables were merged into aswift.Processvariable. Now you can access command line arguments withProcess.arguments. In order to access environment variables addimport POSIXand useProcess.environmentVariables.