Consider the following code
var array1 = [1, 2, 3, 4, 5]
var array2 = array1
array2.append(6)
var len = array1.count
What’s the value of the len
variable, and why?
Consider the following code
let op1: Int = 1
let op2: UInt = 2
let op3: Double = 3.34
var result = op1 + op2 + op3
Where is the error and why? How can it be fixed?
Consider the following code
var defaults = UserDefaults.standard
var userPref = defaults.string(forKey: "userPref")
func printString(string: String) {
print(string)
}
printString(string: userPref)
Where is the bug? What does this bug cause? What’s the proper way to fix it?
In Swift enumerations, what’s the difference between raw values and associated values?
Consider the following code
struct IntStack {
var items = [Int]()
func add(x: Int) {
items.append(x) // Compile time error here.
}
}
Explain why a compile time error occurs. How can you fix it?
What's the differnce on these access control keywords
open
public
internal
fileprivate
private
What will this code print out and why?
import UIKit
var thing = "cars"
let closure = { [thing] in
print("I love \(thing)")
}
thing = "airplanes"
closure()
The following code has a compile time error. Can you spot where and why it happens?
struct Kitten {
}
func showKitten(kitten: Kitten?) {
guard let k = kitten else {
print("There is no kitten")
}
print(k)
}
What are the various ways to unwrap an optional?
What's the extension in Swift? Explain.
The following code has a compile time error. Can you spot where and why it happens?
protocol Movable {
var x: Int { set }
var y: Int { get set }
func move(x: Int, y: Int)
}
Consider the following code
func areEqual(x: Int, y: Int) -> Bool {
return x == y
}
func areEqual(x: String, y: String) -> Bool {
return x == y
}
Please refactory these two functions with generics into one function
Please explain these Higher-order functions in Swift
map
filter
reduce
flatmap