jweinst1
6/2/2016 - 8:24 PM

using multiple types in a struct in swift via optionals

using multiple types in a struct in swift via optionals

//symbol class that can interface across multiple types
//4 seperate initializers for each possible type
struct Symbol {
    var type:String
    var int:Int?
    var string:String?
    var bool:Bool?
    var char:Character?
    
    init(value:Int){
        self.int = value
        self.type = "int"
    }
    
    init(value:String) {
        self.string = value
        self.type = "string"
    }
    
    init(value:Bool) {
        self.bool = value
        self.type = "bool"
    }
    
    init(value:Character) {
        self.char = value
        self.type = "char"
    }
    
}

let a = Symbol(value:4)
let b = Symbol(value:true)
a.type
b.type