I want to use switch in extension - swift

protocol Sound { func makeSound() }
extension Sound {
func makeSound() {
print("Wow")
}
}
protocol Flyable {
func fly()
}
extension Flyable {
func fly() {
print("✈️")
}
}
class Airplane: Flyable { }
class Pigeon: Sound, Flyable { }
class Penguin: Sound { }
let pigeon = Pigeon()
pigeon.fly() // prints ✈️
pigeon.makeSound() // prints Wow
the above code works fine but, I need to print different types of sound (I.E). if I call the airplane.fly() it should print me ("something different") . and same for penguin

Provide fly() for the Airplane class:
class Airplane: Flyable {
func fly() {
print("something different")
}
}
let airBus: Airplane = Airplane()
airBus.fly()
//prints "something different"
You can do the same for the Penguin class:
class Penguin: Sound {
func makeSound() {
print("squawk")
}
}
let 🐧 = Penguin()
🐧.makeSound()
//prints "squawk"
The functions you are providing are default implementations of the protocol. If a type doesn't override the function, it will adopt the default implementation. You can find more in the docs:
You can use protocol extensions to provide a default implementation to any method or computed property requirement of that protocol. If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension.

Related

Swift Generic Classes and Extensions with Conditional Generics

I have the following class:
class MyClass<T: BaseClass> {
let aThing = T()
func someMethod() {
configure()
}
}
whereas
class SubTypeAOfBaseClass: BaseClass { ... }
class SubTypeBOfBaseClass: BaseClass { ... }
In configure I want to configure aThing depending on its type. Therefore I have created a protocol Configurable and an extension:
protocol Configurable {
func configure()
}
extension MyClass: Configurable where T == SubTypeAOfBaseClass {
func configure() {
print("Configuring SubTypeAOfBaseClass")
aThing.doSomethingA()
}
}
The error that I get is in MyClass.someMethod where I call configure(): "Referencing instance method configure() requires types T and SubTypeAOfBaseClass be equivalent.
Another error when I add the following extension:
extension MyClass: Configurable where T == SubTypeBOfBaseClass {
func configure() {
print("Configuring SubTypeBOfBaseClass")
aThing.doSomethingB()
}
}
The error changes to "No exact matches in call to instance method configure" and in the line
extension MyClass: Configurable where T == SubTypeBOfBaseClass {
I get the error "Conflicting conformance of MyClass to protocol configurable, there cannot more than one conformance, even with different conditional bounds."
It should work, but apparently I am missing something or my understanding of how to achieve what I want is wrong.
Consider the case when T is BaseClass, or when T is AnotherSubclass that I defined as
class AnotherSubclass : BaseClass {
}
What would happen? You haven't declared a conformance to Configure when T is AnotherSubclass!
There's really only two (not bad) choices here.
You want configure to do nothing when T is neither SubTypeAOfBaseClass nor SubTypeBOfBaseClass
you only want MyClass<SubTypeAOfBaseClass> and MyClass<SubTypeBOfBaseClass> to be valid types - MyClass<BaseClass> and MyClass<AnotherSubclass> would give compiler errors.
Choice 2 is not possible in Swift. That would require something similar to the sealed types in Java or Kotlin.
Choice 1 can be done like this:
class BaseClass {
...
func configure() {
}
}
class SubTypeAOfBaseClass: BaseClass {
...
override func configure() {
print("Configuring SubTypeAOfBaseClass")
doSomethingA()
}
}
class SubTypeBOfBaseClass: BaseClass {
...
override func configure() {
print("Configuring SubTypeAOfBaseClass")
doSomethingB()
}
}
class MyClass<T: BaseClass> {
let aThing = T()
func someMethod() {
aThing.configure()
}
}
You might notice that the each implementation of configure has been moved to the base classes. If you want to implement them all in MyClass, you must check the type by hand:
class MyClass<T: BaseClass> {
let aThing = T()
func someMethod() {
if let selfA = self as? MyClass<SubTypeAOfBaseClass> {
selfA.configure()
} else if let selfB = self as? MyClass<SubTypeBOfBaseClass> {
selfB.configure()
}
}
}
extension MyClass where T == SubTypeAOfBaseClass {
func configure() {
print("Configuring SubTypeAOfBaseClass")
aThing.doSomethingA()
}
}
extension MyClass where T == SubTypeBOfBaseClass {
func configure() {
print("Configuring SubTypeBOfBaseClass")
aThing.doSomethingB()
}
}
This is because of the second problem in your code - different parameterisations of a generic type, MyClass<SubTypeAOfBaseClass> and MyClass<SubTypeBOfBaseClass>, can't conform to a protocol differently. This is a limitation of Swift, unfortunately. See here for more info.

overriding declarations in extensions is not supported - swift

I made a minimal reproducible code of my issue.
enum Animal {
case cat
case dog
}
protocol AdoptPet {
func petIs(pet: Animal)
}
class Delegate {
}
extension Delegate: AdoptPet {
func petIs(pet: Animal) {
print("not implemeted")
}
}
class Girl: Delegate {
}
extension Girl {
override func petIs(pet: Animal) { // overriding declarations in extensions is not supported
print(pet)
}
}
class PetCenter {
init () {
}
func setup(adpoter: Delegate){
let pet: Animal = .cat
adpoter.petIs(pet: pet)
}
}
let petCenter = PetCenter()
let adpoter: Girl = Girl()
petCenter.setup(adpoter: adpoter)
What should I change in the code to make this compile ?
You need to move both the declaration of the function and the override into the type declarations from extensions. So Delegate needs to contain the petIs implementation in its declaration and Girl needs to override the petIs function in its body as well (using the override keyword) rather than in an extension.
class Delegate: AdoptPet {
func petIs(pet: Animal) {
print("not implemeted")
}
}
class Girl: Delegate {
override func petIs(pet: Animal) {
print(pet)
}
}
class PetCenter {
init () {
}
func setup(adopter: Delegate){
let pet: Animal = .cat
adopter.petIs(pet: pet)
}
}
let petCenter = PetCenter()
let adopter = Girl()
petCenter.setup(adopter: adopter) // cat

Unit testing protocol extensions through structs in Swift 3

Here's the protocol oriented code i have for which i am stuck with for writing unit tests.
protocol TestProtocol {
func testOne()
func testTwo()
}
extension TestProtocol {
var testHelper: TestHelper {
return TestHelper()
}
fileprivate func _one() {
testHelper.one()
}
func one() {
_one()
}
fileprivate func _two() {
testHelper.two()
}
func two() {
_two()
}
}
struct TestHelper {
var userDefaults: UserDefaults {
return UserDefaults.standard
}
func one() {
userDefaults.set("12223334444", forKey: "PhoneNumberWithCountryCode")
}
func two() {
userDefaults.set("Mark", forKey: "UserName")
}
}
class TestAboveCode { }
extension TestAboveCode: TestProtocol {
func testOne() {
_one()
}
func testTwo() {
_two()
}
}
class UseAboveCode {
func use() {
TestAboveCode().testOne()
}
}
here's the problem,
To write unit test for UseAboveCode class, i need to mock the TestHelper struct. Though i can create a mock struct and override the existing functions, i cannot set the testHelper property in the TestProtocol extension. Is there a work around for this?
I do know the other approach which is making the TestHelper struct implement a protocol and ultimately injecting that as a property or through a constructor. This code above looks ideal for readability and usability but someone i am not able to wrap my head around unit test.
Am i over thinking with this approach? Please let me know what the best approach is.

Is swift extension default implementation solved on compile or runtime?

I'm wondering is there a way to work with protocol default implementations in polymorphic style.
Example
protocol RockInterface {
}
extension RockInterface {
func foo() {
print("You Rock")
}
}
extension RockInterface where Self: Metal {
func foo() {
print("Metal")
}
}
extension RockInterface where Self: Grunge {
func foo() {
print("Grunge")
}
}
class Rock: RockInterface {
init() {
foo()
}
}
class Metal: Rock {
}
class Grunge: Rock {
}
let rock = Rock() //prints "You Rock"
let metal = Metal() //prints "You Rock"
let grunge = Grunge() //prints "You Rock"
I expected Metal() to print "Metal", and Grunge to print "Grunge". But it seems that default implementations are solved at compile time instead of runtime. Is my assumption right of wrong? How could I get expected behavior?
The are at least two factors that contribute to the behaviour you see, some within your control, and some not.
functions that are not part of the protocol requirements are statically dispatched. If you want dynamic dispatch, then you'll need to add the method to the protocol declaration:
protocol RockInterface {
func foo()
}
however, the above won't solve your problem, since subclasses inherit the protocol witness table of the parent class. See this excellent answer for more details about this.
I'd also argue you design is a good one, since you tightly coupled the protocol and the classes that conform to that protocol. If you really need the behaviour you described, then one solution would be to drop the protocol extensions, and implement the foo method within each class:
protocol RockInterface {
func foo()
}
class Rock: RockInterface {
init() {
foo()
}
func foo() {
print("You Rock")
}
}
class Metal: Rock {
override func foo() {
print("Metal")
}
}
class Grunge: Rock {
override func foo() {
print("Grunge")
}
}
let rock = Rock() //prints "You Rock"
let metal = Metal() //prints "Metal"
let grunge = Grunge() //prints "Grunge"

Ambiguous functions in multiple protocol extensions?

I have multiple protocols that have the same function name. Some protocols have associated types, where I can't figure out how to call the functions as I do in non-generic protocols. I get the error: Protocol 'MyProtocol1' can only be used as a generic contraint because it has Self or associated type requirements
Here's what I'm trying to do:
protocol Serviceable {
associatedtype DataType
func get(handler: ([DataType] -> Void)?)
}
struct PostService: Serviceable {
func get(handler: ([String] -> Void)? = nil) {
print("Do something...")
}
}
protocol MyProtocol1: class {
associatedtype ServiceType: Serviceable
var service: ServiceType { get }
}
extension MyProtocol1 {
func didLoad(delegate: Self) {
print("MyProtocol1.didLoad()")
}
}
protocol MyProtocol2: class {
}
extension MyProtocol2 {
func didLoad(delegate: MyProtocol2) {
print("MyProtocol2.didLoad()")
}
}
class MyViewController: UIViewController, MyProtocol1, MyProtocol2 {
let service = PostService()
override func viewDidLoad() {
super.viewDidLoad()
didLoad(self as MyProtocol1) // Error here: Protocol 'MyProtocol1' can only be used as a generic contraint because it has Self or associated type requirements
didLoad(self as MyProtocol2)
}
}
How can I specifically call the function from a generic protocol extension?
It's simple to achieve by turning the protocol into a generic (see below), or by creating a type eraser for these protocols, but this very strongly suggests that you have a design problem and you should redesign your classes and/or extensions. A collision like this suggests strongly that MyStruct is doing too many things itself because it's being pulled in multiple directions by MyProtocol1 and MyProtocol2. There should likely be two objects here instead. (Composition rather than inheritance.)
class MyStruct: MyProtocol1, MyProtocol2 {
let service = PostService()
func prot1Load<T: MyProtocol1>(t: T) {
t.didLoad()
}
func prot2Load<T: MyProtocol2>(t: T) {
t.didLoad()
}
init() {
prot1Load(self)
prot2Load(self)
}
}
To your particular example in the comments, I would use composition rather than inheritance. You're treating protocols like multiple-inheritance, which is almost never right. Instead compose out of things that conform to a protocol.
protocol LoadProviding {
func load()
}
struct MyLoader1: LoadProviding {
func load() {
print("MyLoader1.didLoad()")
}
}
struct MyLoader2: LoadProviding {
func load() {
print("MyLoader2.didLoad()")
}
}
protocol Loader {
var loaders: [LoadProviding] { get }
}
extension Loader {
func loadAll() {
for loader in loaders {
loader.load()
}
}
}
class MyStruct: Loader {
let service = PostService()
let loaders: [LoadProviding] = [MyLoader1(), MyLoader2()]
init() {
loadAll()
}
}
Of course you don't really have to have LoadProviding be a full struct. It could just be a function if that's all you need:
typealias LoadProviding = () -> Void
func myLoader1() {
print("MyLoader1.didLoad()")
}
func myLoader2() {
print("MyLoader2.didLoad()")
}
protocol Loader {
var loaders: [LoadProviding] { get }
}
extension Loader {
func loadAll() {
for loader in loaders {
loader()
}
}
}
class MyStruct: Loader {
let service = PostService()
let loaders: [LoadProviding] = [myLoader1, myLoader2]
init() {
loadAll()
}
}
If you have time to wade through a video on the subject, you may be interested in the Beyond Crusty: Real World Protocols talk from dotSwift. It's about this and similar problems.