swiftadvanced
Swift Advanced
Protocols, generics and ARC
7 questions
By EZ4Code Team
1. What is the purpose of Swift protocols?
Define method/property blueprints; classes/structs/enums can all conform, with multi-conformance support
Can only be conformed to by classes
Can only declare methods and cannot have default implementations
Equivalent to abstract classes
Explanation: A protocol defines an interface blueprint; classes, structs, and enums can all conform, with multi-conformance; protocol extensions can also provide default implementations, similar to traits.
2. What are the characteristics of protocol extensions?
Can provide default method implementations for protocols, and support conditional conformance (where constraints)
Cannot provide default implementations
Only for classes
Cannot add constraints
Explanation: Protocol extensions can provide default implementations for protocol methods, and combined with where clauses enable conditional conformance; this is the core of Swift's protocol-oriented programming.
3. What is the syntax for a generic function?
func identity<T>(_ x: T) -> T { return x }func foo<T>(_ x: T) -> T
func foo<T>(_ x: T) -> Any
func foo(_ x: T) -> T
func foo<T>(x: T) -> T
Explanation: Use <T> to declare a type parameter, used in parameters and return values; _ means the parameter label is omitted when called.
4. How does ARC (Automatic Reference Counting) work?
Inserts retain/release at compile time; automatically releases when the reference count reaches 0
Runtime garbage collection
Manual memory management
Does not manage memory
Explanation: ARC automatically inserts reference counting operations at compile time; strong references increment by 1, releases decrement by 1, and the object is destroyed when the count reaches 0; unlike GC, there are no runtime pauses.
5. What is the common way to resolve a retain cycle?
Use weak or unowned for references that may form a cycle
Use strong
Cannot be resolved
Manual release
Explanation: Retain cycles cause the reference count to never reach 0, resulting in memory leaks; use weak (can be nil, optional) or unowned (non-nil, guaranteed non-empty) to break the cycle.
6. What is the difference between weak and unowned?
weak becomes nil after the referenced object is released (must be optional); unowned assumes the object always exists (non-optional), and accessing a released object crashes
They are exactly the same
unowned becomes nil
weak cannot be used with optionals
Explanation: weak is a weak reference; the object is automatically set to nil after release, and must be optional; unowned is an unowned reference, non-optional, assuming the object's lifetime is not shorter than its own; accessing a released object crashes.
7. What are the representatives of value types and reference types in Swift?
struct/enum are value types; class is a reference type
struct is a reference type
class is a value type
enum is a reference type
Explanation: struct and enum are value types (copied on assignment/passing); class is a reference type (reference counted, shared). Swift recommends preferring value types for safety.