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!

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Mariah
  • 85
  • 1
  • 4
  • 10
  • What sort of output does `isKindOfClass` give you? – Cole Aug 22 '15 at 14:07
  • possible duplicate of [How do you find out the type of an object (in Swift)?](http://stackoverflow.com/questions/24101450/how-do-you-find-out-the-type-of-an-object-in-swift) – nhgrif Aug 22 '15 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. – nhgrif Aug 22 '15 at 14:22
  • What type has your array? `[Person]`? – Qbyte Aug 22 '15 at 15:45

4 Answers4

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.

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • 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... – Dan Rosenstark Apr 14 '16 at 00:31
  • Example of such a language in which you cannot access the run time type of an object? – Dan Rosenstark Apr 14 '16 at 18:19
  • For instance OCaml, where type information is completely managed at compile time, and is not present at run-time. – Renzo Apr 14 '16 at 21:25
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)
}
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.

pacification
  • 5,838
  • 4
  • 29
  • 51
-2

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

Siam
  • 162
  • 2
  • 9
  • 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... – nhgrif Aug 22 '15 at 14:29
  • Oh, sorry, it's my fault, maybe you can compare with the class name – Siam Aug 22 '15 at 15:05