I want to fill Dictionary via a method of which dictionary is one of the parameter. When I add a value to key, say 15, it always return 0 count when I try to access it second time with same key i.e. 15. Here's the code.
private static var showWidgetMap = Dictionary<Int, [BaseWidget]>() private static var hideWidgetMap = Dictionary<Int, [BaseWidget]>()
static func initHandler() { let cloudWidget = CloudWidget() cloudWidget.setType(CreatorConstants.CLOUD) let property1 = [CreatorConstants.IMG_SRC: "cloud1", CreatorConstants.X_COORD: "100", CreatorConstants.Y_COORD: "450"] cloudWidget.setPropery(property1) addWidgetInLocalTimeList(15, widget: cloudWidget, delete: false) let emojiWidget = CloudWidget() emojiWidget.setType(CreatorConstants.EMOTICON) let property2 = [CreatorConstants.IMG_SRC: "1", CreatorConstants.X_COORD: "100", CreatorConstants.Y_COORD: "550"] emojiWidget.setPropery(property2) addWidgetInLocalTimeList(15, widget: emojiWidget, delete: false)} static func addWidgetInLocalTimeList(time_milisec: Int, widget: BaseWidget, delete: Bool) { if(delete) { checkAndAdd(hideWidgetMap, key: time_milisec, widget: widget); } else { checkAndAdd(showWidgetMap, key: time_milisec, widget: widget); } }
private static func checkAndAdd(var map: Dictionary<Int, [BaseWidget]>, key: Int, widget: BaseWidget) { print("map count is") print(map.count) if var val = map[key] { val.append(widget); } else { var temp: [BaseWidget] = []; temp.append(widget); map[key] = temp print(map.count) } } print(map.count) always returns 0.