self study Flashcards
What does Built-In mean?
Refers to functionalities, types, or features that are an integral part of the Swift language itself.
These are provided directly by the Swift standard library or are part of the Swift compiler, and they are readily available without the need for additional imports or external dependencies.
Functions and methods with no return type have an implicit return type of ______
Void
A struct is a ____ type
Value
A class is a ____ type
Reference
The language used by Apple before Swift:
Objective-C
Are variables always initialized before use?
Yes
Fundamental Data Types:
Integers, Double, Bool, String
Collection Types:
Array, Set, Dictionary
A tuple is an ____ type
Advanced
The language helps you to be clear about the types of values your code can work with.
For example, if your code requires a String, type safety prevents you from passing an Int by mistake
Type-Safe
Swift performs type-safe checks when ____ the code
Compiling
Must be declared before they’re used
Constants and Variables
Enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide
Type Inference
Write a ___________ by placing a colon after the constant of variable name, followed by a space, followed by the name of the type to use
Type Annotation
In the following code, the “String” word can be defined as:
var myWord: String
Type Annotation
It’s a value that appears directly in your source code. The number 42 is a ______
E.g let meaningOfLife = 42
Literal Value
If you need to give a constant or variable the same name as the reserved Swift keyword, surround the keyword with ____
backticks (`)
Include the name of a constant or variable as a placeholder in a longer string
String interpolation
How do you represent, single, multiple lines comments?
// Single line
/* Multiple lines */
/* This is the start of the first multiline comment. /* This is the second, nested multiline comment. */ This is the end of the first multiline comment. */
Whole numbers with no fractional component, Signed positive, zero, negative.
Integers
Represents a 64-bit floating-point number and it is preferred and inferred when you don’t specify a type
Double
Values are referred to as logical because they can only be true or false
Booleans (Bool)
The values within a ____ can be of any type and don’t have to be of the same type as each other
tuple
You can ____ a tuples contents into separate constants or variables
decompose
This is an example of a:
let http404Error = (404, “Not Found”)
Tuple
Define an alternative name for an existing type and use the keyword ____
Type Aliases
typealias
You use ____ in the situation where a value may be absent, it represents two possibilities:
Either there is a value of a specified type, and you can unwrap the ____ to access that value, or there isn’t a value at all
Optionals
If you define an optional variable without providing a default value, the variable is automatically set to ____
nil
Trailing question mark
?
You must ____ the value of an Optional instance before you can use it in many contexts
unwrap
Mention the ways to safely unwrap optional values
*Optional Binding
*Optional Chaining
*Nil-Coalescing Operator
*Force unwrapping / Unconditional Unwrapping
This is useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter.
Implicitly Unwrapped Optional
To conditionally ____ the wrapped value of an Optional instance to a new variable, use one of the optional ____ control sources
bind / binding
This safely unwrap optional value, includes:
if let, guard let, and switch
Optional binding
It’s a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil
Optional Chaining
Multiple queries can be chained together, and the entire chain ____ gracefully if any link in the chain is nil
falls
Postfix optional chaining operator
(postfix ?)
Use the ____ to supply a default value in case the Optional instance is ____
nil-coalescing operator / nil
nil-coalescing operator
??
When you are certain that an instance Optional contains a value, you can unconditionally unwrap the value by using the ____ operator
force unwrap
Forced unwrap operator
(postfix !)
Unconditionally unwrapping a nil instance with ! Triggers a ____
runtime error
This is an example of a ____
let assumedString: String! = “Hello”
Implicitly Unwrapped Optional
Assertions and Preconditions are checks that happen ____, you use them to make sure an essential condition is satisfied before executing any further code.
a runtime
If the boolean condition in the assertion or precondition is ____, code execution continues as usual, if it’s ____, the current state of the program is invalid, code execution ends and your app is terminated.
true
false
____ help you find mistakes and incorrect assumption during ____, and ____ help detect issues in ____
Assertions
development
preconditions
production
Which is the difference between assertions and preconditions ?
It’s when they are ____
Assertions are checked only in ____ builds.
Preconditions are checked in both ____ and ____ builds.
Checked
debug
debug
production
in production builds, the condition inside an assertion is evaluated.
No
You write an assertion by calling the ____ function
assert(::file:line:)
If the code already checks for the condition, you use ____ function to indicate that an assertion has failed
assertionFailure(::file:line:)
You write precondition by calling ____ function
precondition(::file:line:)
You can also call the ____ function to indicate that a failure has occurred
preconditionFailure(::file:line:)
Defines a blueprint of methods, properties, and any other requirements that suit a particular task or piece of functionality
Protocol
A protocol defines a ____ of ____ and ____
blueprint
methods
properties
A protocol can be adopted by a ___, ___ and ___
class
struct
enum
Any time that satisfies the requirements of a protocol is said to ____ to that protocol
conform
You can ____ a protocol
extend
Can multiple protocols be listed?
Yes, separated by commas
If a class has a superclass, list a superclass ____ the protocols
before
Why do protocols begin with a capital letter?
Because protocols are types
Protocol doesn’t specify whether the property should be stored or computed, it only specifies whether each property must be ____, or ____and ____.
gettable
gettable
settable
what is it
var mustBeSettable: Int { get set }
A property declared in a protocol
Default values, however, can’t be specified for method parameters within a protocol’s definition
True or False?
True
Protocols can require specific initializers to be implemented by conforming types.
protocol SomeProtocol {
init(someParameter: Int)
}
True or False?
True,
You write these initializers as part of the protocol’s definition in exactly the same way as for normal initializers, but without curly braces or an initializer body
Mutating keyword only applies to ____
structs
To force a protocol to be limited to class types only, you can add the ____ protocol to its inheritance list
AnyObject
protocol SecretClassFeature:AnyObject {
func secretClassFeature ()
}
What does the delegation design pattern do?
Enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type
delegates are declared as ____ references
weak
A protocol can be used as the type to be stored in a collection such as an array or a dictionary?
True or False
True
A protocol can ____ one or more protocols and can add further requirements on top of the requirements it inherits.
inherit
protocol InheritingProtocol: SomeProtocol, AnotherProtocol { }
what is protocol composition?
You can combine multiple protocols into a single requirement
You can also create a typealias using protocol composition
typealias RobotQ = Bipedal & PetrolPowered
What are Optional Protocol Requirements?
Which is the optional modifier?
You can define optional requirements of protocols. These requirements don’t have to be implemented by types that conform to the protocol.
Those are prefixed with the optional modifier, Both the protocol and the optional requirement must be marked with the @objc attribute,
What are extensions?
Allow you to add brand-new functionality to existing classes, structures, and protocols. You can use extensions to add computed properties, methods, or initializers, or make something conform to a protocol.
Extensions can add methods and computed properties, but not ____ properties
stored