It isn't clear what you want the line main2.Test(number1:15,number2:20)
to do. Is that supposed to create a variable of type Test? (Or rather, SubMain.test)? Or is it supposed to replace your main2's .test
property with a new value? Both are possible, but require slightly different syntax.
If you want your SubMain class to have an additional instance variable of type Test, you need to refactor your code slightly, as shown in Joakim's answer.
Take a look at this code:
class Main {
var name:String
init(name:String){
self.name = name
}
}
class SubMain: Main {
// Declare a struct "Test", and have it conform to the `CustomStringConvertible` class so we can log `Test` values`
struct Test: CustomStringConvertible {
var number1:Int
var number2:Int
var description: String {
return "Test(number1: \(self.number1), number2: \(self.number2))"
}
}
var test: Test
init(name:String, test: Test){
self.test = test
super.init(name: name)
}
}
// Also Make our SubMain class conform to CustomStringConvertible
extension SubMain: CustomStringConvertible {
var description: String {
return "SubMain(name: \(self.name), test = \(self.test))"
}
}
var aSubMain = SubMain(name: "Fred", test: SubMain.Test(number1: 1, number2: 2))
print("aSubMain before changing it's .test = \(aSubMain)")
aSubMain.test = SubMain.Test(number1: 7, number2: 4)
print("aSubMain after changing its .test property = \(aSubMain)")
let aSubMainTest = SubMain.Test(number1: 12, number2: 37)
print("aSubMainTest = \(aSubMainTest)")
(With a nod to Joakim, who posted a similar answer before I did.)
That outputs:
aSubMain before changing it's .test = SubMain(name: Fred, test = Test(number1: 1, number2: 2))
aSubMain after changing its .test property = SubMain(name: Fred, test = Test(number1: 7, number2: 4))
aSubMainTest = Test(number1: 12, number2: 37)