Types Flashcards
(36 cards)
What two kinds of types are there in Swift?
In Swift, there are two kinds of types: named types and compound types.
A named type is a type that can be given a particular name when it is defined.
A compound type is a type without a name, defined in the Swift language itself.
What varieties of named types are there?
Named types include classes, structures, enumerations, and protocols.
In addition to user-defined named types, the Swift standard library defines many commonly used named types, including those that represent arrays, dictionaries, and optional values.
Data types that are normally considered basic or primitive in other languages—such as types that represent numbers, characters, and strings—are actually named types, defined and implemented in the Swift standard library using structures.
What varieties of compound types are there?
There are two compound types: function types and tuple types.
A compound type may contain named types and other compound types.
For instance, the tuple type (Int, (Int, Int)) contains two elements: The first is the named type Int, and the second is another compound type (Int, Int).
In the Swift grammar, type → …
type → array-type | function-type | type-identifier | tuple-type | optional-type | implicitly-unwrapped-optional-type | protocol-composition-type | metatype-type
What is a type annotation?
A type annotation explicitly specifies the type of a variable or expression. Type annotations begin with a colon (:) and end with a type.
Type annotations can contain an optional list of type attributes before the type, such as @auto_closure or @noreturn.
What is a type identifier
A type identifier refers to either a named type or a type alias of a named or compound type.
Most of the time, a type identifier directly refers to a named type with the same name as the identifier. For example, Int is a type identifier that directly refers to the named type Int, and the type identifier Dictionary directly refers to the named type Dictionary.
What are the two cases in which a type identifier does not refer to a type with the same name?
In the first case, a type identifier refers to a type alias of a named or compound type.
In the second case, a type identifier uses dot (.) syntax to refer to named types declared in other modules or nested within other types.
What is a tuple type?
A tuple type is a comma-separated list of zero or more types, enclosed in parentheses.
You can use a tuple type as the return type of a function to enable the function to return a single tuple containing multiple values. You can also name the elements of a tuple type and use those names to refer to the values of the individual elements. An element name consists of an identifier followed immediately by a colon (:).
What is the Void type?
Void is a typealias for the the empty tuple type, ().
What is a function type?
A function type represents the type of a function, method, or closure and consists of a parameter and return type separated by an arrow (->):
parameter type -> return type
Because the parameter type and the return type can be a tuple type, function types support functions and methods that take multiple paramaters and return multiple values.
When can you apply an @auto_closure attribute to a function type?
You can apply the @auto_closure attribute to a function type that has a parameter type of () and that returns the type of an expression.
What does an autoclosure function do?
An autoclosure function captures an implicit closure over the specified expression, instead of the expression itself. The following example uses the auto_closure attribute in defining a very simple assert function:
func simpleAssert(condition: @auto_closure () -> Bool, message: String) { if !condition() { println(message) } }
let testNumber = 5 simpleAssert(testNumber % 2 == 0, "testNumber isn't an even number.") // prints "testNumber isn't an even number."
What are the rules for variadic parameter types?
A function type can have a variadic parameter as the last parameter in its parameter type.
Syntactically, a variadic parameter consists of a base type name followed immediately by three dots (…), as in Int…
A variadic parameter is treated as an array that contains elements of the base type name. For instance, the variadic parameter Int… is treated as Int[].
How do you specify an in-out parameter in a function type?
To specify an in-out parameter, prefix the parameter type with the inout keyword.
You cannot mark a variadic parameter or a return type with the inout keyword.
What is a curried function type?
The type of a curried function is equivalent to a nested function type. For example, the type of the curried function addTwoNumbers()() below is Int -> Int -> Int:
func addTwoNumbers(a: Int)(b: Int) -> Int { return a + b } addTwoNumbers(4)(5) // Returns 9
The function types of a curried function are grouped from right to left. For instance, the function type Int -> Int -> Int is understood as Int -> (Int -> Int)
What is an array type?
The Swift language uses square brackets ([]) immediately after the name of a type as syntactic sugar for the named type Array, which is defined in the Swift standard library.
In other words, the following two declarations are equivalent:
let someArray: String[] = [“Alex”, “Brian”, “Dave”]
let someArray: Array = [“Alex”, “Brian”, “Dave”]
How do you access multidimensional arrays?
When accessing the elements in a multidimensional array, the left-most subscript index refers to the element at that index in the outermost array. The next subscript index to the right refers to the element at that index in the array that’s nested one level in. And so on.
What is an optional type?
The Swift language defines the postfix ? as syntactic sugar for the named type Optional, which is defined in the Swift standard library. In other words, the following two declarations are equivalent:
var optionalInteger: Int?
var optionalInteger: Optional
The type Optional is an enumeration with two cases, None and Some(T), which are used to represent values that may or may not be present.
Note that no whitespace may appear between the type and the ?.
Why can optional types appear in a boolean context?
Optionals conform to the LogicValue protocol and therefore may occur in a Boolean context.
In that context, if an instance of an optional type T? contains Some(T) the optional type evaluates to true. Otherwise, it evaluates to false.
How do you access the value of an optional type?
If an instance of an optional type contains a value, you can access that value using the postfix operator !, as shown below:
optionalInteger = 42
optionalInteger! // 42
Using the ! operator to unwrap an optional that has a value of nil results in a runtime error.
What is an implicitly unwrapped optional type?
The Swift language defines the postfix ! as syntactic sugar for the named type ImplicitlyUnwrappedOptional, which is defined in the Swift standard library. In other words, the following two declarations are equivalent:
var implicitlyUnwrappedString: String!
var implicitlyUnwrappedString: ImplicitlyUnwrappedOptional
Note that no whitespace may appear between the type and the !.
Where can you use implicitly unwrapped optionals?
You can use implicitly unwrapped optionals in all the same places in your code that you can use optionals. For instance, you can assign values of implicitly unwrapped optionals to variables, constants, and properties of optionals, and vice versa.
What are the rules for initializing optionals?
If you don’t provide an initial value when you declare an optional variable or property, its value automatically defaults to nil.
The same is true for implicitly unwrapped optionals.
What are the rules for accessing implicitly unwrapped optionals?
Because the value of an implicitly unwrapped optional is automatically unwrapped when you use it, there’s no need to use the ! operator to unwrap it. That said, if you try to use an implicitly unwrapped optional that has a value of nil, you’ll get a runtime error.