jweinst1
6/2/2016 - 6:02 PM

storing objects in a capsule swift

storing objects in a capsule swift

//encapsulated instance to store any object
class Capsule {
    var object:Any
    var type:String
    
    init(object:Any, type:String){
        self.object = object
        self.type = type
    }
}

//class that stores capsules
class Storage {
    var map:[String:Capsule]
    
    init(){
        self.map = [String:Capsule]()
    }
    //inserts an entry into the map, creating a new capsule
    func insert(name:String, elem:Any, type:String){
        self.map[name] = Capsule(object:elem, type:type)
    }
    
    subscript (name:String) -> Capsule {
        get {
            return self.map[name]!
        }
    }
}