Types Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What two kinds of types are there in Swift?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What varieties of named types are there?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What varieties of compound types are there?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In the Swift grammar, type → …

A

type → array-type­ | function-type­ | type-identifier­ | tuple-type­ | optional-type­ | implicitly-unwrapped-optional-type­ | protocol-composition-type­ | metatype-type­

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a type annotation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a type identifier

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the two cases in which a type identifier does not refer to a type with the same name?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a tuple type?

A

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 (:).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Void type?

A

Void is a typealias for the the empty tuple type, ().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a function type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When can you apply an @auto_closure attribute to a function type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does an autoclosure function do?

A

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."
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the rules for variadic parameter types?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you specify an in-out parameter in a function type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a curried function type?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an array type?

A

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”]

17
Q

How do you access multidimensional arrays?

A

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.

18
Q

What is an optional type?

A

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 ?.

19
Q

Why can optional types appear in a boolean context?

A

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.

20
Q

How do you access the value of an optional type?

A

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.

21
Q

What is an implicitly unwrapped optional type?

A

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 !.

22
Q

Where can you use implicitly unwrapped optionals?

A

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.

23
Q

What are the rules for initializing optionals?

A

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.

24
Q

What are the rules for accessing implicitly unwrapped optionals?

A

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.

25
Q

What is a Protocol Composition Type?

A

A protocol composition type describes a type that conforms to each protocol in a list of specified protocols. Protocol composition types may be used in type annotations and in generic parameters.

A protocol composition type allows you to specify a value whose type conforms to the requirements of multiple protocols without having to explicitly define a new, named protocol that inherits from each protocol you want the type to conform to.

26
Q

What are the rules for Protocol Composition Type declarations?

A

Protocol composition types have the following form:

protocol

Each item in a protocol composition list must be either the name of protocol or a type alias of a protocol composition type. If the list is empty, it specifies the empty protocol composition type, which every type conforms to.

27
Q

What is a Metatype Type?

A

A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types.

28
Q

What is the metatype of a class, structure, or enumeration?

A

The metatype of a class, structure, or enumeration type is the name of that type followed by .Type

For example, the metatype of the class type SomeClass is SomeClass.Type.

29
Q

What is the metatype of a protocol?

A

The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the name of that protocol followed by .Protocol.

For example, the metatype of the protocol SomeProtocol is SomeProtocol.Protocol.

30
Q

How do you access a type as a value?

A

You can use the postfix self expression to access a type as a value.

For example, SomeClass.self returns SomeClass itself, not an instance of SomeClass.

And SomeProtocol.self returns SomeProtocol itself, not an instance of a type that conforms to SomeProtocol at runtime.

31
Q

How do you obtain the runtime type of an instance as a value?

A

You can use a dynamicType expression with an instance of a type to access that instance’s runtime type as a value, as the following example shows:

someInstance.dynamicType.printClassName()

32
Q

What is a Type Inheritance Clause?

A

A type inheritance clause is used to specify which class a named type inherits from and which protocols a named type conforms to.

A type inheritance clause begins with a colon (:), followed by a comma-separated list of type identifiers.

33
Q

What are the rules for the type inheritance clause in a class declaration?

A

Class types may inherit from a single superclass and conform to any number of protocols. When defining a class, the name of the superclass must appear first in the list of type identifiers, followed by any number of protocols the class must conform to. If the class does not inherit from another class, the list may begin with a protocol instead.

34
Q

What are the rules for the type inheritance clauses of protocols?

A

Other named types may only inherit from or conform to a list of protocols. Protocol types may inherit from any number of other protocols.

When a protocol type inherits from other protocols, the set of requirements from those other protocols are aggregated together, and any type that inherits from the current protocol must conform to all of those requirements.

35
Q

What are the rules for a type inheritance clause of an enumeration?

A

A type inheritance clause in an enumeration definition may be either a list of protocols, or in the case of an enumeration that assigns raw values to its cases, a single, named type that specifies the type of those raw values.

36
Q

What are the rules for Type Inference?

A

Type inference in Swift operates at the level of a single expression or statement. This means that all of the information needed to infer an omitted type or part of a type in an expression must be accessible from type-checking the expression or one of its subexpressions.