Section 1

Preview this deck

Bridging Headers

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

7 years ago

Date created

Mar 14, 2020

Cards (73)

Section 1

(50 cards)

Bridging Headers

Front

Back

Classes

Front

Back

What is the difference between the two property observers?

Front

willSet changes a property to newValue that is what a value will be, and didSet changes a property toan oldValue. This allows you to track changing values

Back

Closures

Front

Back

What are generics and what problem do they solve?

Front

Generics are used to make algorithms safely work with types. In Swift, generics can be used both in functions and data types, e.g. in classes, structures or enumerations. Generics solve the problem of code duplication. A common case is when you have a method that takes a type of parameter and you have to duplicate it just to accommodate a parameter of another type.

Back

What is the difference between a local and global variable?

Front

Global variables are defined outside of any function, method, or closure, and local variables are those defined within functions, methods, or closures.

Back

Generics (types, functions, and constraints)

Front

Back

What is a static method?

Front

A method usable by a class, not a specific instance (object) of that class.

Back

If you copy a struct and then alter the property of the copy, does the original change? Why or why not?

Front

The original doesnt change, because structs are value types. The copy us a copy of the values, not the reference.

Back

What is a type parameter?

Front

Type parameters specify and name a placeholder type, and are written immediately after the function's name between angle brackets. Example:

Back

There are a few cases when you can't avoid using implicitly unwrapped optionals. When? Why?

Front

The most common reasons to use implicitly unwrapped optionals are: When a property that is not nil by nature cannot be initialized at instantiation time. A typical example is an Interface Builder outlet, which always initializes after its owner has been initialized. In this specific case -- assuming it's properly configured in Interface Builder -- the outlet is guaranteed to be non-nil before it's used. To solve the strong reference cycle problem, which is when two instances refer to each other and require a non-nil reference to the other instance. In such a case, one side of the reference can be marked unowned, while the other uses an implicitly unwrapped optional.

Back

What is generic programming?

Front

A way to write functions and data types without being specific about the types they use. As an example, an Array can take any type of object.

Back

Under the hood, what type of data structure is an optional?

Front

An optional is an enum! enum Optional<T> { case None case Some(T) }

Back

What is an optional and what problem do optionals solve?

Front

An optional is used to let a variable of any type represent the lack of value. In Objective-C, the absence of value is available in reference types only, and it uses the nil special value. Value types, such as int or float, do not have such ability. Swift extends the lack of value concept to both reference and value types with optionals. An optional variable can hold either a value or nil any time.

Back

Extensions

Front

Back

How do you force unwrap an optional?

Front

With an ! This forces an optional to unwrap... even if it has no value!

Back

How do you optionally unwrap an optional?

Front

if let This essentially says, "if this optional returns a value, put that value into a new variable"

Back

Sets vs. Arrays (when and why?)

Front

Back

What is a tuple?

Front

A tuple is an on-the-fly data structure that resembles a struct or a mini-dictionary, designed to be discarded and only hold data as a small data structure. The best part about it is that it can return multiple pieces of data in one return from a method.

Back

What is an optional?

Front

A variable that might return a nil value.

Back

Is this correct? var optional1: String? = nil var optional2: String? = .None optional1 == optional2

Front

Back

What is the word for the instance of a class?

Front

An object

Back

Methods

Front

Back

Enums

Front

Back

What are the two types of property observers?

Front

didSet and willSet

Back

What is an important difference about Swift classes and structures regarding polymorphism?

Front

Classes can inherit from other classes, but structs cannot.

Back

What is a computed property?

Front

A property that is determined by code

Back

What is wrong with this code? public struct ThermometerStruct { private(set) var temperature: Double = 0.0 public mutating func registerTemperature(temperature: Double) { self.temperature = temperature } } let thermometerStruct = ThermometerStruct() thermometerStruct.registerTemperature(56.0)

Front

thermometerStruct is declared via let, but the .registerTemperature(temperature) method mutates the field. In structures, methods that change the internal state must be marked as mutating but invoking them from immutable variables is not allowed.

Back

What are static methods and properties in swift?

Front

"properties and methods that belong to a type, rather than to instances of a type." Excerpt From: Hudson, Paul. "Hacking with Swift." iBooks. This material may be protected by copyright.

Back

Optionals (regular, implicit, and unwrapping)

Front

Back

Swift Playgrounds

Front

Back

Error Handling

Front

Back

What type are structs?

Front

Value type

Back

Value types vs. reference types

Front

Value types are objects that point at values, while reference types just point to an address

Back

Swift API Design Guidelines

Front

Back

Structs

Front

A struct is a complex data type that contains properties. When you create a struct, xcode creates an init method and allows you to construct instances. Properties are accessed via dot notation.

Back

What is the nil coalescing operator?

Front

An operator (??) that helps unwrap optionals or set them to a default. So if you have var brian = Person; var name = brian.name ?? "First Name Unknown"

Back

When should you use a structure, and when should you use a class?

Front

There's an ongoing debate about whether using classes over structures is a good or bad practice. Functional programming tends to favor value types, whereas object-oriented programming prefers classes. In Swift, classes and structures differ by a number of features. You can sum up the differences as follows: Classes support inheritance, structures don't Classes are reference types, structures are value types There's no universal rule that determines what's best to use. The general recommendation is to use the most minimal tool required to accomplish your goal, but a good rule of thumb is to use structures unless you need inheritance or reference semantics.

Back

Swift.org

Front

Back

What is a closure's capture list?

Front

A capture list is the list of variables captured by a variable on declaration. So let closure = { [thing] in print("I love \(thing)") } The capture list is [thing]. Because this closure is being declared with thing, it captures it when this declaration happens.

Back

Do Swift classes get automatic memberwise initializers?

Front

Nope, you gotta write your own init method.

Back

Protocols

Front

Back

Why can force unwrapping be bad?

Front

Because you can unwrap an optional that will return nil.

Back

If you copy a class object and then alter the property of the copy, does the original change? Why or why not?

Front

Yep it changes! Because the copy is a reference to the values, as classes are reference types.

Back

What is different about Swift and ObjC Strings?

Front

Objective-C Strings are encoded in UTF-16, 16bit strings where accents and characters are saved as separate characters.

Back

What type are classes?

Front

Reference types

Back

Initializers(single and multiple)

Front

Back

What is the difference between as? and as! In typecasting?

Front

as? Is optional typecasting, meaning you think a cast can occur, while as! forces a cast

Back

Variables vs. Constants

Front

You set variables as "var" and the value can change. You set constants as "let" and the value will never change.

Back

What does this mean? var optional2: String? = .None

Front

This is the correct way to intialize an optional as nil. Optionals are just enums behind the scenes.

Back

Section 2

(23 cards)

What is the value of (a...b)

Front

All numbers between a and b, inclusive

Back

What is the value of (a..<b)

Front

All numbers from a to b, not including b

Back

How do you prefix a number to be a binary number?

Front

0b

Back

Which of the following features are included in Swift? Generic classes Generic structs Generic protocols

Front

1 and 2 are included. Generics can be used in classes, structs, enumerations, global functions and methods. 3 is partially implemented via typealias. It's not a generic type per se, it's a placeholder name. It's often referred to as an associated type and it's defined when a protocol is adopted by a type.

Back

Are strings value types or reference types?

Front

Value types

Back

When can you use the abbreviated reference form for enums?

Front

When the value type is already known

Back

What structures can adopt protocols?

Front

Classes, enums, and structs

Back

What is an assert?

Front

A statement that asserts a condition is true; if it's not, the program crashes.

Back

Is Swift an object-oriented language or a functional language?

Front

Swift is a hybrid language that supports both paradigms. It implements the three fundamental principles of OOP: Encapsulation Inheritance Polymorphism As for Swift being a functional language, there are different but equivalent ways to define it. One of the more common is on Wikipedia: "...a programming paradigm [...] that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." It's hard to argue that Swift is a full-fledged functional language, but it has the basics.

Back

Are arrays value Types or reference types?

Front

Value Types

Back

What is an implicitly unwrapped optional?

Front

An optional that definitely has a value (because of earlier checks or whatever) that you suffix with !

Back

How are generics referenced?

Front

In <> brackets <T>

Back

What is a special feature of protocols?

Front

They can be used like any named type (for instance, collections of named protocols)

Back

Can enums contain functions?

Front

Yes! It's a good way to add simple descriptions

Back

What are the two swift range operators?

Front

Closed range operator and half-open range operator

Back

What are associated values vs raw values?

Front

Associated values are different and attached to an instance, raw values is the same for all of its instances

Back

What does mutating do?

Front

Allows a method to change its struct

Back

How do you prefix a number to be a hexadecimal number?

Front

0x

Back

Does Swift feature implicit casting between similar data types?

Front

No, you must explicitly cast them.

Back

How do you access argument parameters of a closure?

Front

$ Eg $0 is the first argument, $1 is the second

Back

What kinds of behaviors do structs support?

Front

Same as classes, including properties and inits and methods.

Back

How do you ensure a generic matches a requirement?

Front

Use where. <T where T: Comparable> Alternatively, <T: Comparable>

Back

What keyword allows a method to change its struct?

Front

Mutating

Back