Complete the code to declare a strong reference to an instance.
var person: Person? = [1] Person()By default, variables hold strong references to instances in Swift.
Complete the code to declare a weak reference to avoid retain cycles.
class Person { weak var friend: [1]? }
Weak references must be optional and refer to class types to avoid retain cycles.
Fix the error in the code to prevent a strong reference cycle.
class Customer { var card: [1]? } class CreditCard { var owner: Customer? }
Using 'unowned' breaks the strong reference cycle when the referenced instance is expected to always exist.
Fill both blanks to create a dictionary with keys as names and values as their lengths, filtering names longer than 3 characters.
let names = ["Anna", "Bob", "Catherine", "Dave"] let nameLengths = [[1]: [2] for name in names if name.count > 3]
Use 'name' as key and 'name.count' as value to create the dictionary comprehension.
Fill all three blanks to create a closure that captures self weakly and prints a message.
class Printer { func printMessage() { let closure = { [[1] self] in print("Message from [2]") } closure() } } let printer = Printer() printer.printMessage()
Use '[weak self]' to avoid retain cycles, refer to 'self' inside closure, and print class name 'Printer'.