2015-07-11 58 views
0

我有一個自定義對象的快速字典,我用它來填充我的UITableview。但是,當我填寫字典時,我意識到當我向其添加新數據時,它會刪除以前的數據。像下面的相關的代碼塊:Swift dictionary在追加新數據時刪除以前的數據

var dict = Dictionary<ListItem, [ListItem]>() 

func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{ 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return self.dict.keys.array.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell: CategoryCell! = tableView.dequeueReusableCellWithIdentifier("categoryCell", forIndexPath: indexPath) as? CategoryCell 

    if(cell == nil) 
    { 
     cell = CategoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: "categoryCell") 
    } 
    else 
    { 
     cell.ClearCurrentCellContext(cell.image, labelTitle: cell.lblCategory, labelDescription: cell.lblDescription) 
    } 

    if let ip = indexPath as NSIndexPath? 
    { 
     let key = Array(self.dict.keys)[ip.row] as ListItem 
     cell.SetCell(key, image: cell.image, labelTitle: cell.lblCategory, labelDescription: cell.lblDescription) 
    } 

    return cell 
} 

我填補了字典這個區塊內:

Alamofire.request(.POST, WSConstants.WebServiceAddress, parameters: nil) 
    .responseJSON 
{(request, response, JSON, error) in 

if let jsonResult: NSDictionary = JSON as? NSDictionary 
{ 
    if let categories = jsonResult[WSConstants.CATEGORIES] as? NSDictionary 
    { 
     if let wsStatus = categories.valueForKey(WSConstants.WS_STATUS) as? String 
     { 
      if(wsStatus == "ok") 
      { 
       var mainObj: NSDictionary! 
       var mainItem: ListItem! 
       for(var i=0; i<mainJSONArray.count; i++) 
       { 
        mainObj = mainJSONArray.objectAtIndex(i) as! NSDictionary 
        mainItem = ListItem() 
        mainItem.itemId = mainObj.valueForKey(WSConstants.ID) as! Int 
        mainItem.itemName = mainObj.valueForKey(WSConstants.TITLE) as! String 
        mainItem.itemDescription = mainObj.valueForKey(WSConstants.DESCRIPTION) as! String 
        mainItem.itemIcon = mainObj.valueForKey(WSConstants.ICON) as! String 
        mainItem.parentId = mainObj.valueForKey(WSConstants.PARENT) as! Int 
        mainItem.postCount = mainObj.valueForKey(WSConstants.POST_COUNT) as! Int 
        if let subJSONArray = mainObj.valueForKey(WSConstants.SUB) as? NSArray 
        { 
         var midCategoryList: [ListItem]! = [ListItem]() 
         var subObj: NSDictionary! 
         var subItem: ListItem! 
         for(var i=0; i<subJSONArray.count; i++) 
         { 
          subObj = subJSONArray.objectAtIndex(i) as! NSDictionary 
          subItem = ListItem() 
          subItem.itemId = subObj.valueForKey(WSConstants.ID) as! Int 
          subItem.itemName = subObj.valueForKey(WSConstants.TITLE) as! String 
          subItem.itemDescription = subObj.valueForKey(WSConstants.DESCRIPTION) as! String 
          subItem.itemIcon = subObj.valueForKey(WSConstants.ICON) as! String 
          subItem.parentId = subObj.valueForKey(WSConstants.PARENT) as! Int 
          subItem.postCount = subObj.valueForKey(WSConstants.POST_COUNT) as! Int 
          midCategoryList.append(subItem) 
          subItem = nil 
          subObj = nil 
         } 
         // The code below line fills the dictionary with key and its values 
         self.dict[mainItem] = midCategoryList 
         midCategoryList = nil 
        } 
        mainItem = nil 
        mainObj = nil 
       } 
       Utility.ReloadTableViewDataWithAnimations(self.categoryTableView) 
      } 
     } 
} 
} 

的ListItem類:

class ListItem: Serializable, Hashable, NSCoding 
{ 
    var uniqueID: Int = 0 
    override var hashValue: Int { return uniqueID.hashValue } 
    var itemId: Int 
    var itemName: String! 
    var itemIcon: String! 
    var itemDescription: String! 
    var parentId: Int! 
    var postCount: Int! 
    var content: String! 

    override init() 
    { 
     self.itemId = 0 
     self.itemName = "" 
     self.itemIcon = "" 
     self.parentId = 0 
     self.postCount = 0 
     self.content = "" 
     self.itemDescription = "" 
    } 

    init(itemId: Int, itemName: String!, itemIcon: String!, itemDescription: String!, parentId: Int!, postCount: Int, content: String!, date: String!, url: String!) 
    { 
     self.itemId = itemId 
     self.itemName = itemName 
     self.itemIcon = itemIcon 
     self.parentId = parentId 
     self.postCount = postCount 
     self.content = content 
     self.itemDescription = itemDescription 
    } 

    deinit 
    { 

    } 

    required init(coder aDecoder: NSCoder) 
    { 
     self.itemId = aDecoder.decodeIntegerForKey("itemId") as Int 
     self.itemName = aDecoder.decodeObjectForKey("itemName") as! String 
     self.itemIcon = aDecoder.decodeObjectForKey("itemIcon") as! String 
     self.itemDescription = aDecoder.decodeObjectForKey("itemDescription") as! String 
    } 

    func encodeWithCoder(aCoder: NSCoder) 
    { 
     aCoder.encodeInteger(self.itemId, forKey: "itemId") 
     aCoder.encodeObject(self.itemName, forKey: "itemName") 
     aCoder.encodeObject(self.itemIcon, forKey: "itemIcon") 
     aCoder.encodeObject(self.itemDescription, forKey: "itemDescription") 
    } 
} 

func ==(lhs: ListItem, rhs: ListItem) -> Bool 
{ 
    return lhs.uniqueID == rhs.uniqueID 
} 

這是一個迅速的bug或者我犯了一些錯誤來填補它?

謝謝你的答案

王認爲

解決方案:

問題的原因是使用==,而不是在列表項類===。丹尼爾T.的解決方案是正確的答案。謝謝你們。

+0

您是否檢查每次您的密鑰是否相同? –

+0

我已經通過每次打印所有的鍵和值檢查了它,並沒有任何問題,所有的鍵都不同。 @DharmeshKheni – saksut

+0

ListItem是一個stuct還是一個類? –

回答

1

沒有看到更多的代碼,我的猜測是問題在於你的ListItem的==運算符。以下代碼不會出現您所描述的問題:

//: Playground - noun: a place where people can play 

class ListItem: Hashable { 

    var itemId: Int = 0 
    var itemName: String = "" 
    var itemDescription: String = "" 
    var itemIcon: String = "" 
    var parentId: Int = 0 
    var postCount: Int = 0 

    var hashValue: Int { 
     return itemId.hashValue 
    } 

} 

func ==(lhs: ListItem, rhs: ListItem) -> Bool { 
    return lhs === rhs // simple identity 
} 

var dict = Dictionary<ListItem, [ListItem]>() 

let arr = [ListItem(), ListItem()] 

dict[ListItem()] = arr 
dict[ListItem()] = arr 

dict.keys.array.count 
+0

我是對的。根據你發佈的新代碼,你的'=='運算符比較'ListItem.uniqueID's,但它們都被設置爲'0'並且永遠不會改變。 –

+0

'==='操作員真的會導致這個問題? @DanielT。 – saksut

+0

再一次,看看你的'func =='方法。你所有的對象都有一個uniqueID爲0的值,所以它們都是相同的== == –