4

I have this code in swift: To explain the idea of polymorphism!

//Polymorphism


class Person {
var name:String="Guest"
var age:Int=0

init(name:String) {
    self.name=name
    self.age=0
}
init(name:String, age:Int) {
    self.name=name
    self.age=age
}

func Update(name:String) {
    self.name=name
}
func Upgrade() {
    self.age++
}
}



class Student:Person
{
var average:Float=100
func IsOk()->Bool {
    return average > 80
}
init(name:String, average:Float) {
    super.init(name: name)
    self.average=average
}
}


class Teacher:Person {
var Salary:Float=2000
init(name:String, age:Int, Salary:Float){
    super.init(name: name, age: age)
    self.Salary=Salary

}
func GetNetSalary()->Float {
    return 0.8*self.Salary
}

override func Upgrade() {
    super.Upgrade()
    Salary*=1.1 // add 10% to salary
}
}



var t1:Teacher=Teacher(name: "Ahmed", age: 28, Salary: 3000)
var st1=Student(name:"Test", average: 70)

var p1:Person=Person(name: "abc")
var p2:Person=Student(name: "Student1", average: 100) //up casting
var p3:Person=Teacher(name: "Teacher", age: 40, Salary: 3008)


var arr=[t1, st1, p1, p2, p3] //array of persons and teachers and students

for instance in arr {
if instance is Student {println("This is student")}
if instance is Teacher {println("This is teacehr")}  
}

In the end in the for loop How could I put such a condition to see if an element in the array is only a Person? Because when I type:

if instance is Person {println("This is a Person")}

This gives me an error because this condition is always true!

4
  • What sort of output does isKindOfClass give you? Commented Aug 22, 2015 at 14:07
  • possible duplicate of How do you find out the type of an object (in Swift)? Commented Aug 22, 2015 at 14:22
  • In the future, start by boiling your problem down to the absolute necessary example that duplicates your problem. This code is far from that. Commented Aug 22, 2015 at 14:22
  • What type has your array? [Person]? Commented Aug 22, 2015 at 15:45

4 Answers 4

2

EDITED

You are asking:

How could I put such a condition to see if an element in the array is only a Person?

In a strongly, statically, typed language in which you cannot access the run-time type of an object, you cannot do this. However, in Swift you can use the reflexive properties of the language, through the dynamicType method, to get such a type.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, what? If the object is a Person -- and not a subclass -- it's dynamicType will reflect that. And it doesn't matter what the variable's type is...
Example of such a language in which you cannot access the run time type of an object?
For instance OCaml, where type information is completely managed at compile time, and is not present at run-time.
1

I should say that @Renzo's answer is outdated and dynamicType is deprecated. you must use type(of:) method for that. one more advice: if you want to know the exact type of an optional object, unwrap it first, then pass it to type(of:)

class A { }
class B: A { }

let b: A? = B()
print("\(type(of: b))")    // you will get optional(A)

if let temp = b {
    print("\(type(of: temp))")    // you will get(B)
}

Comments

0

Try this:

for instance in arr {
    if instance.dynamicType == Person.self {print("This is a Person")}
    if instance.dynamicType == Student.self {print("This is student")}
    if instance.dynamicType == Teacher.self {print("This is teacehr")}
}

More information about this you can find in Metatype Type chapter of documentation.

Comments

-2

Maybe you can test if the element is aTeacher ? If not, it must be aPerson!

2 Comments

If it's not a Teacher, it could be a Student. And what happens if we add a Janitor, Parent, Principal, Advisor, etc. Now all of the sudden adding code elsewhere breaks the code here, where ever this test is... plus, as the question my comment linked demonstrates, we can just check for exact class...
Oh, sorry, it's my fault, maybe you can compare with the class name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.