Get child class from parent

user2724028

I have a big issue in Swift programming. I am trying to get the name of child class from the parent. This is a sample example, what i want to do:

class Parent {
  class func sayHello() {
      let nameChildClass = //
      println("hi \(nameChildClass)")
  }
}

class Mother: Parent {

}

class Father: Parent {

}

Mother.sayHello()
Father.sayHello()

I know there is some other way do to that, but i really need to it like that.

Antonio

You can use a function like this:

func getRawClassName(object: AnyClass) -> String {
    let name = NSStringFromClass(object)
    let components = name.componentsSeparatedByString(".")
    return components.last ?? "Unknown"
}

which takes an instance of a class and obtain the type name using NSStringFromClass.

But the type name includes the namespace, so to get rid of that it's split into an array, using the dot as separator - the actual class name is the last item of the returned array.

You can use it as follows:

class Parent {
    class func sayHello() {
        println("hi \(getRawClassName(self))")
    }
}

and that will print the name of the actual inherited class

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get parent with certain class from child element

From Dev

Get variable from parent class for use in method of child class

From Dev

PHP Get child class name from parent class differences

From Dev

How to get the value of a variable in child class from parent class

From Dev

Get atributes from a parent class from child in Python 3

From Dev

Get parent class name from child with ES6?

From Dev

Flutter: Get data back from StatefulWidget child class to parent

From Dev

how to get callback from child to parent class in react-redux?

From Dev

Get the calling parent class in a static child class

From Dev

Get the Child Class Propert in Parent class

From Dev

Appending values from a child class to parent class

From Dev

Return child class from parent class

From Dev

Accessing child class prototype from parent class

From Dev

lxml - get attribute of child based on parent class

From Dev

Get index of parent div based on child class

From Dev

JavaScript: Use parent methods from child class?

From Dev

updating parent class UI from a child

From Dev

Typescript - call child method from parent class

From Dev

C++ Polymorphism: from parent class to child

From Dev

Typecasting an object from parent class to child

From Dev

Change parent variable from child class

From Dev

Access Parent Class Method from Child in ReactJS

From Dev

Accessing child variables from parent class?

From Dev

How to access child variables from a parent class?

From Dev

Python construct child class from parent

From Dev

Call parent class method from child in ReactJS

From Dev

Add specific class to child from parent

From Dev

Move class name from child to parent

From Dev

Access the child class properties from a parent

Related Related

  1. 1

    Get parent with certain class from child element

  2. 2

    Get variable from parent class for use in method of child class

  3. 3

    PHP Get child class name from parent class differences

  4. 4

    How to get the value of a variable in child class from parent class

  5. 5

    Get atributes from a parent class from child in Python 3

  6. 6

    Get parent class name from child with ES6?

  7. 7

    Flutter: Get data back from StatefulWidget child class to parent

  8. 8

    how to get callback from child to parent class in react-redux?

  9. 9

    Get the calling parent class in a static child class

  10. 10

    Get the Child Class Propert in Parent class

  11. 11

    Appending values from a child class to parent class

  12. 12

    Return child class from parent class

  13. 13

    Accessing child class prototype from parent class

  14. 14

    lxml - get attribute of child based on parent class

  15. 15

    Get index of parent div based on child class

  16. 16

    JavaScript: Use parent methods from child class?

  17. 17

    updating parent class UI from a child

  18. 18

    Typescript - call child method from parent class

  19. 19

    C++ Polymorphism: from parent class to child

  20. 20

    Typecasting an object from parent class to child

  21. 21

    Change parent variable from child class

  22. 22

    Access Parent Class Method from Child in ReactJS

  23. 23

    Accessing child variables from parent class?

  24. 24

    How to access child variables from a parent class?

  25. 25

    Python construct child class from parent

  26. 26

    Call parent class method from child in ReactJS

  27. 27

    Add specific class to child from parent

  28. 28

    Move class name from child to parent

  29. 29

    Access the child class properties from a parent

HotTag

Archive