Swift, derived classes with same named static function - swift

I want to have the same named static function for multiple classes which are derived from each other. I really don't feel like naming them G_something, H_something, etc.
Is there any way in swift to do this:
class G {
static func dosomething()
{
print("G")
}
}
class H : G {
class func dosomething()
{
G.dosomething()
print("H")
}
}
class I : H {
class func dosomething()
{
H.dosomething()
print("I")
}
}
I.dosomething()

To be short :
class G {
class func dosomething()
{
print("G")
}
}
class H : G {
override class func dosomething()
{
super.dosomething()
print("H")
}
}
class I : H {
override class func dosomething()
{
super.dosomething()
print("I")
}
}
I.dosomething()
G
H
I

Simple overriding should works:
class G {
class func dosomething()
{
print("G")
}
}
class H : G {
override class func dosomething()
{
super.dosomething()
print("H")
}
}
class I : H {
override class func dosomething()
{
super.dosomething()
print("I")
}
}
I.dosomething()

Related

Apple way in Swift instead of an override?

what approach does Apple use in Swift instead of override, how can I write this without using the #objc marker
import Foundation
class A {
init() {}
}
extension A {
#objc func foo() {
print("foo")
}
}
class B: A {
override func foo() {
print("yes2")
}
}
A().foo()
B().foo()
maybe protocols? but how?
You can define a protocol and provide a default method implementation. Then all you need is to comform to that protocol and provide its own foo method if necessary:
protocol Fooable {
func foo()
}
extension Fooable {
func foo() {
print("default implementation")
}
}
class A: Fooable { }
class B: A {
func foo() {
print("B implementationn")
}
}
let a = A()
let b = B()
a.foo()
b.foo()
This will print
default implementation
B implementationn

Overriding the method - unexpected behaviour

I have code like this:
class A {
func method() { print("method from A") }
}
class B: A {
override func method() { print("method from B") }
}
class C: A {
override func method() { print("method from C") }
}
func run (_ obj: A) {
doIt(obj)
}
func doIt(_ obj: A) {
print("specific logic when object of A class")
obj.method()
}
func doIt(_ obj: B) {
print("specific logic when object of B class")
obj.method()
}
func doIt(_ obj: C) {
print("specific logic when object of C class")
obj.method()
}
let obj = C()
run(obj)
I am getting output:
specific logic when object of A class method from C
but i would expect:
specific logic when object of C class method from C
How should I change the code to get this behavior?
The problem is merely that you have made doIt three loose functions. Instead, make it three methods of the three classes:
class A {
func method() { print("method from A") }
func doIt() {
print("specific logic when object of A class")
self.method()
}
}
class B: A {
override func method() { print("method from B") }
override func doIt() {
print("specific logic when object of B class")
self.method()
}
}
class C: A {
override func method() { print("method from C") }
override func doIt() {
print("specific logic when object of C class")
self.method()
}
}
func run (_ obj: A) {
obj.doIt()
}
Now polymorphism does the work for you.
Although polymorphism is the best approach, here is another way you can do it
class A {
func method() { print("method from A") }
}
class B: A {
override func method() { print("method from B") }
}
class C: A {
override func method() { print("method from C") }
}
func run<T: A>(_ obj: T) {
doIt(obj)
}
func doIt<T: A>(_ obj: T) {
print("specific logic when object of \(T.self) class")
obj.method()
}
let obj = C()
run(obj)

Swift Inheritance: Super's Super

Supposing to have this three classes with this simply hierarchy:
class A {
func foo() {
print("A")
}
}
class B: A {
override func foo() {
super.foo()
print("B")
}
}
class C: B {
override func foo() {
// *******
print("C")
}
}
In class C, in overrided method foo I want to call a method foo: is it possible?
In C++ this can be achieved with C->A::foo(), but how do I do this in Swift?
super.foo() should be sufficient, since B prints "B" and calls super to print "A".
class C: B {
override func foo() {
super.foo()
print("C")
}
}
let c = C()
c.foo()
Output:
A
B
C
If you want to intentionally expose A's foo() from B, you need to create a new accessor:
class B: A {
override func foo() {
super.foo()
print("B")
}
func exposeFoo() {
super.foo()
}
}
class C: B {
override func foo() {
super.exposeFoo()
print("C")
}
}
Or, use NSObject and the power of the Objective-C runtime:
class A: NSObject { // make sure your base class inherits from NSObject
func foo() {
print("A")
}
}
// ...
class C: B {
override func foo() {
guard let superSuper = self.superclass?.superclass() else {
return; // no super super
}
let fooImp = class_getMethodImplementation(superSuper, "foo")
typealias MyCFunction = #convention(c) (AnyObject, Selector) -> Void
let curriedImplementation = unsafeBitCast(fooImp, MyCFunction.self)
curriedImplementation(self, selector) // prints A
}
}

How to make static function calls for all subclasses in an array loop?

All of the subclasses has a static function update and I have to call them for each subclass:
class subClass1 {
static func update() { ... }
}
subClass1.update()
subClass2.update()
subClass3.update()
I hope I can construct an array to store them and call them like this:
let allSubClasses: [baseClass.Type] = [
subClass1.self,
subClass2.self,
subClass3.self,
]
for var i=0; i<allSubClasses.count; ++i {
allSubClasses[i].update() // something like this, how to achieve it?
}
How to do it correctly?
Given a base class
class Animal {
class func update() { }
}
and 2 subclasses
class Dog: Animal { }
class Cat: Animal { }
This is how you create an array of class types
let animals: [Animal.Type] = [Dog.self, Cat.self]
And this is how you invoke the update static class function
for animal in animals {
animal.update()
}
How do you do it. Declare it as a class method,
class BaseClass {
class func update() {
}
}
class Subclass1: BaseClass {
override class func update() {
print("Subclass1")
}
}
class Subclass2: BaseClass {
override class func update() {
print("Subclass2")
}
}
class Subclass3: BaseClass {
override class func update() {
print("Subclass3")
}
}
let allSubclasses: [BaseClass.Type] = [
Subclass1.self,
Subclass2.self,
Subclass3.self
]
for i in 0 ..< allSubclasses.count {
allSubclasses[i].update()
}
I would guess that you had it as static which would make your method a final and so you cannot override that in your base classes.

Subclassing Nested Class in Swift

I'm trying to subclass a nested class as follows:
import Foundation
class Blah {
class BlahNested {
func name() -> String {
return "Blah"
}
}
var blah_ : BlahNested
init() {
blah_ = Blah.BlahNested()
}
func name() -> String {
return blah_.name()
}
}
class SubBlah : Blah {
class BlahNested : Blah.BlahNested {
override func name() -> String {
return "SubBlah"
}
}
init() {
super.init()
blah_ = SubBlah.BlahNested() // THIS LINE IS HAVING ISSUES
}
}
It seems that that nested subclass having the same name as the parent (i.e., "BlahNested" being the same in both the parent and the child) is causing issues. Is it required to have different names in such a case? Thanks!