constant property cannot be modified by a subclass

"For class instances, a constant property can only be modified during initialization by the class that introduces it. It cannot be modified by a subclass."-《The Swift Programming Language》


If I want to inherit a constant from super class and modified during subclass init.I konw it won't work.


So I changed to this:


class commonStyle
{
     private var insidevar:someclass
     var outsidelet:someclass {return insidevar}
     init(){
     insidevar = someclass()
     }
}
class iPhone6Style:commonStyle
{
     override init() {
     super.init()
     insidevar = someclass("subclass value")
     }
}


Is there a better solution?

Thanks a lot.

Pass the value in base class init and set a default value for it.


class Base {
    private(set) var value: Int // Setter is private
  
    init(value: Int = 1) { // Default value if value is not set
        self.value = value
    }
}
class Sub: Base {
    init() {
        super.init(value: 2)
    }
}
Base().value //1
Sub().value //2

Or you could do this:


class commonStyle
{
     private(set) var outsidelet:someclass
     init(){
     outsidelet = someclass()
     }
}
class iPhone6Style:commonStyle
{
     override init() {
     super.init()
     outsidelet = someclass("subclass value")
     }
}


It gets harder if your classes must be in different files. Swift does not have a good solution for that scenario yet.

constant property cannot be modified by a subclass
 
 
Q