Swift is an application and systems programming language developed by Apple and distributed as open source. Swift interoperates with Objective-C and Cocoa/Cocoa touch APIs for Apple's macOS, iOS, tvOS, and watchOS operating systems. Swift currently supports macOS and Linux. Community efforts are in progress to support Android, Windows, and other platforms.
Swift development happens on GitHub; contributions are usually submitted via pull requests.
Bugs and other issues are tracked at bugs.swift.org.
Discussions about Swift development, evolution, and usage are held on the Swift mailing lists.
Supply a case for every possible value of your input. Use a default case
to cover remaining input values you don't want to specify.
The default case must be the last case.
By default Switches in Swift will not continue to check other cases after a case has been matched.
Like structs and unlike classes, enums are value types and are copied instead of referenced when passed around.
For more information about enums, see The Swift Programming Language.
A Swift protocol is a collection of requirements that conforming types must implement. The protocol may then be used in most places where a type is expected, for example Arrays and generic requirements.
Protocol members always share the same access qualifier as the whole protocol, and cannot be specified separately. Although a protocol could restrict access with getter or setter requirements, as per examples above.
For more information about protocols, see The Swift Programming Language.
Objective-C protocols are similar to Swift protocols.
Protocols are also comparable to Java interfaces.
For more information about optionals, see The Swift Programming Language.
For more information on Swift closures, see Apple's documentation.
For more information about errors, see The Swift Programming Language.
Arrays are an ordered collection of values. Values may repeat but must be of the same type.
Some examples in this topic might have a different order when used because dictionary order is not guaranteed.
A String
in Swift is a collection of characters, and by extension a collection of Unicode scalars. Because Swift Strings are based on Unicode, they may be any Unicode scalar value, including languages other than English and emojis.
Because two scalars could combine to form a single character, the number of scalars in a String is not necessarily always the same as the number of characters.
For more information about Strings, see The Swift Programming Language and the String Structure Reference.
For implementation details, see "Swift String Design"
You can read more about extensions in The Swift Programming Language.
For further information, see Apple's documentation on Using Swift with Cocoa and Objective-C.
The init()
is a special method in classes which is used to declare an initializer for the class. More information can be found here: Initializers
For more information about conditional statements, see The Swift Programming Language.
Properties: Associated with a type
Variables: Not associated with a type
See the Swift Programming Language iBook for more information.
Tuples are considered value types. More info on tuples could be found in the documentation: developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
weak
-keyword should be used, if a referenced object may be deallocated during the lifetime of the object holding the reference.
unowned
-keyword should be used, if a referenced object is not expected to be deallocated during the lifetime of the object holding the reference.
Example:
class A : CLLocationManagerDelegate
{
init()
{
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startLocationUpdates()
}
}
This example will not work properly, as the location manager is deallocated after the initializer returns. The proper solution is to create a strong reference as an instance variable:
class A : CLLocationManagerDelegate
{
let locationManager:CLLocationManager
init()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startLocationUpdates()
}
}
Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. For example, Swift's
Array
andDictionary
types are both generic collections. You can create an array that holdsInt
values, or an array that holdsString
values, or indeed an array for any other type that can be created in Swift. Similarly, you can create a dictionary to store values of any specified type, and there are no limitations on what that type can be.
Below are the three access levels from highest access (least-restrictive) to lowest access (most-restrictive)
Public access allows access to classes, structs, variables, etc from any file within the model, but more importantly outside the module if the external file imports the module containing the public access code. It is popular to use public access when you create a framework.
Internal access allows files only with the module of the entities to use the entities. All entities have internal acccess level by default (with a few exceptions).
Private access prevents the entity from being used outside of that file.
A subclass cannot have a higher access than its superclass.
If property’s setter is private the getter is internal (which is the default). Also you can assign access level for both the getter and the setter. These principles also apply to subscripts as well
Other entity types include: Initializers, Protocols, Extensions, Generics, and Type Aliases
A Mirror
is a struct
used in the introspection of an object in Swift. Its most prominent property is the children array. One possible use case is to serialize a struct for Core Data
. This is done by converting a struct
into a NSManagedObject
.
The children
property of a Mirror
is an array of child objects from the object the Mirror instance is reflecting. A child
object has two properties label
and value
. For example a child might be a property with the name title
and the value of Game of Thrones: A Song of Ice and Fire
.
When using method swizzling in Swift there are two requirements that your classes/methods must comply with:
NSObject
dynamic
attributeFor a complete explanation of why this is required, check out Using Swift with Cocoa and Objective-C:
Requiring Dynamic Dispatch
While the
@objc
attribute exposes your Swift API to the Objective-C runtime, it does not guarantee dynamic dispatch of a property, method, subscript, or initializer. The Swift compiler may still devirtualize or inline member access to optimize the performance of your code, bypassing the Objective-C runtime. When you mark a member declaration with thedynamic
modifier, access to that member is always dynamically dispatched. Because declarations marked with thedynamic
modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the@objc
attribute.Requiring dynamic dispatch is rarely necessary. However, you must use the
dynamic
modifier when you know that the implementation of an API is replaced at runtime. For example, you can use themethod_exchangeImplementations
function in the Objective-C runtime to swap out the implementation of a method while an app is running. If the Swift compiler inlined the implementation of the method or devirtualized access to it, the new implementation would not be used.
For more information on this topic, see the WWDC 2015 talk Protocol-Oriented Programming in Swift.
There is also a great written guide on the same: Introducing Protocol-Oriented Programming in Swift 2.
Swift has an official style guide: Swift.org API Design Guidelines. Another popular guide is The Official raywenderlich.com Swift Style Guide.
println
and debugPrintln
where removed in Swift 2.0.
Sources:
https://developer.apple.com/library/content/technotes/tn2347/_index.html http://ericasadun.com/2015/05/22/swift-logging/
Special Characters
*?+[(){}^$|\./
Closely aligned concepts required to complete one's understanding of (Unsafe) BufferPointers.
(Source, Swiftdoc.org)