Skip to content

Instantly share code, notes, and snippets.

@alexito4 alexito4/playground
Created Aug 26, 2014

Embed
What would you like to do?
Swift extensions and inheritance
import UIKit
protocol Thing {
func name() -> String
}
protocol God {
func talk() -> String
}
class Base : Thing {
func a() -> String {
return "Base A"
}
func name() -> String {
return "base name"
}
}
extension Base : God {
func talk() -> String {
return "base talk"
}
}
class Child : Base {
override func a() -> String {
return "child A"
}
override func name() -> String {
return "child name"
}
override func talk() -> String {
return "child talk"
}
}
let base = Base()
base.a()
base.name()
base.talk()
let child = Child()
child.a()
child.name()
child.talk()
@ivanboychuk

This comment has been minimized.

Copy link

ivanboychuk commented Aug 31, 2018

Nice example. I see you are strong in this. Could you please help me with question below?
I have 2 classes and 1 extension. Like following:

class A // Scene of sprite kit.
{
  var sprite: SKSpriteNode!
  addSprite() // this method invokes and adds sprite normally
}
extension A
{
  func addSprite() {
    sprite = SKSprite(image)
    // sets all positions and size, z position as well
   addChild(sprite)
  }
}
class B: A // B is a simple class
{
  if condition {
     super.addSprite() // this method also invokes, does the things but on the scene of class A no sprite. 
  }
}

I believe here is something wrong with inheritance but I don't know what.
If you have some thoughts could you please share it with me.
Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.