Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in protocols

Tags:

ios

swift

I have a protocol A which has a static variabe x. B is a implementation of A. In Class C I pass an instance of B and assigned it to a. How can I access 2 (value of x in class B) from it ?

protocol A {
 static var x : Int { get }  
}

class B : A {
 static var x: Int {
  return 2 
}
}


class C {
// instance of B is assigned to a. 
let a: A


print(a.x)
}
like image 778
2ank3th Avatar asked Mar 13 '23 21:03

2ank3th


1 Answers

A static variable belongs to the class, not the instance. You can refer to the class by calling dynamicType:

print(a.dynamicType.x)
like image 113
Code Different Avatar answered Mar 15 '23 22:03

Code Different