如果没有类函数,我无法访问“price”变量,我可以知道为什么吗?
这是代码。
class var price : Int {
return 25000
}
class func display()
{
println("The \(name) with \(model) was released in \(year) with rs.\(price)")
}
最佳答案
类变量属于类,而不是实例。您可以通过以下方式访问它:
- 类名称:
MyClass.price
self.dynamicType.price
(对于类内的函数)myVar.dynamicType.price
(假设myVar
是该类的实例)。
关于swift - swift中的类变量只能通过类函数访问吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110189/