Explore core concepts of Swift generics with these foundational questions designed to reinforce flexible, reusable, and type-safe coding principles. This quiz covers generic syntax, use cases, benefits, and common pitfalls, helping you solidify your understanding of generic types and functions in Swift programming.
Which is the correct way to define a generic function in Swift that swaps two values of any type?
Explanation: The correct syntax is func swapValuesu003CTu003E(a: inout T, b: inout T) which declares generic type T and uses inout to allow modification of the parameters. Option two uses incorrect brackets and misses inout. Option three omits inout, so it cannot swap values. Option four uses an unnecessary constraint and places inout incorrectly on the parameter declaration.
In the generic struct definition struct Boxu003CTu003E { var item: T }, what does T represent?
Explanation: T is a type parameter that acts as a placeholder for any type, making the struct usable with different types. It is not a built-in integer type; option two is incorrect. Option three incorrectly describes a function. Option four refers to protocols, not what T represents in generic code.
When would using generics in Swift be most appropriate?
Explanation: Generics are best for reusable code that works for any type, promoting flexibility and type safety. Handling only integers does not require generics, making option two inappropriate. Using predefined collections is unrelated to defining generic code. String formatting is unrelated to generics.
What is a key benefit of using generics in Swift functions?
Explanation: The well-known benefit is that generics help you write code that works for multiple types without duplicating logic. They do not inherently increase runtime speed, so option two is wrong. Generics do not create new primitive types, invalidating option three. Increasing code unreadability is not a benefit.
How can you restrict a generic function to work only with types conforming to the Equatable protocol?
Explanation: The correct way is to declare a constraint with T: Equatable in the function signature. Option two misuses the generic type. Option three uses incorrect where clause syntax. Option four places the colon in an invalid position.
Which statement about type safety with Swift generics is true?
Explanation: Swift generics are type-safe, so the compiler shows errors for mismatched types at compile time. They do not eliminate all run-time errors, so option two is incorrect. Option three falsely claims automatic conversion. Access control is still enforced, so option four is invalid.
Which is an example of a generic collection in Swift?
Explanation: Arrayu003CIntu003E is a generic collection that can store any type, such as Int in this case. NSInteger is a specific integer type, so option two is incorrect. StringLiteral is not a generic collection. DictionaryOfInt is not a standard Swift type.
How do you declare a struct with two generic type parameters in Swift?
Explanation: The standard syntax is struct Pairu003CT, Uu003E using a comma to separate multiple type parameters. Option two uses an invalid dash. Option three declares only one type but uses two, causing an error. Option four incorrectly mimics a function declaration.
Which statement distinguishes a generic function from type erasure in Swift?
Explanation: Generic functions preserve specific type information for compatibility and safety, while type erasure hides this for flexibility. Option two is inaccurate as performance depends on context. Option three falsely claims all types become unrelated. Option four is incorrect since generics often use protocols.
Can you provide a default type for a generic parameter in a Swift generic type?
Explanation: Swift allows you to specify default generic types in angle brackets when declaring a type. Option two is inaccurate, as generic types can have defaults. Option three is incorrect since structs and enums also support it. Option four unnecessarily restricts it to Decodable conformance.