Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created August 26, 2014 18:55
Show Gist options
  • Save alexito4/2dbab8d961c4c060e5c3 to your computer and use it in GitHub Desktop.
Save alexito4/2dbab8d961c4c060e5c3 to your computer and use it in GitHub Desktop.
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
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