2017-09-25 123 views
0

我在isSelected(Bool)處得到零錯誤。請查看以下屏幕截圖瞭解更多詳情。在NSCoder中展開一個可選值時意外地發現爲零

enter image description here

進口基金會

class StatusEntity: `NSObject`, `NSCoding` { 

    var Value: String 
    var Native_Description: String 
    var isSelected: Bool 

    override init() { 
     Value = "" 
     Native_Description = "" 
     isSelected = false 
    } 

    @objc required init(coder aDecoder: NSCoder) { 
     Value = aDecoder.decodeObject(forKey: "Value") as! String 
     Native_Description = aDecoder.decodeObject(forKey: "Native_Description") as! String 
     isSelected = aDecoder.decodeObject(forKey: "isSelected") as! Bool 
    } 

    @objc func encode(with aCoder: NSCoder) { 
     aCoder.encode(Value, forKey: "Value") 
     aCoder.encode(Native_Description, forKey: "Native_Description") 
     aCoder.encode(isSelected, forKey: "isSelected") 
    } 
} 

這是CollectionView代碼

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->UICollectionViewCell { 

     let cell: StatusCheckCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "statusCollectionCell", for: indexPath) as! StatusCheckCollectionViewCell 

     let entity: StatusEntity = arrayList[indexPath.item] 
     cell.txtName.text = entity.Native_Description 

     // Making CollectionView Cell Right Align 
     var scalingTransform : CGAffineTransform! 
     scalingTransform = CGAffineTransform(scaleX: -1, y: 1); 
     cell.transform = scalingTransform 
     cell.btnItem.tag = indexPath.row 
     cell.btnItem.addTarget(self, action: #selector(StatusTableViewCell.onItemClicker(_:)), for: UIControlEvents.touchUpInside) 

     if(entity.isSelected){ 
      cell.imgCheckBox.image = UIImage(named: "CheckBoxSelected") 
     } else { 
      cell.imgCheckBox.image = UIImage(named: "CheckBox") 
     } 
     return cell 
    } 

    func onItemClicker(_ sender: UIButton){ 
     let entity: StatusEntity = arrayList[sender.tag] 
     entity.isSelected = !entity.isSelected 
     let indexPath: IndexPath = IndexPath(row: sender.tag, section: 0) 
     collectionView.reloadItems(at: [indexPath]) 

     // Saving values to DB 
     var submitEntity: SubmitDocumentEntity = SubmitDocumentEntity() 
     let insertEntity = dataStorage.getSubmitDocumentEntity() 
     if(!insertEntity.isKind(of: NSNull.self)){ 
      submitEntity = dataStorage.getSubmitDocumentEntity() as! SubmitDocumentEntity 
     } 
     if(entity.isSelected) { 
      if(!submitEntity.TransactionTypes.contains(entity.Value)){ 
       submitEntity.TransactionTypes.append(entity.Value) 
      } 
     } else { 
      if(submitEntity.TransactionTypes.contains(entity.Value)){ 
       let index = submitEntity.TransactionTypes.index(of: entity.Value) 
       submitEntity.TransactionTypes.remove(at: index!) 
      } 
     } 
     dataStorage.putSubmitDocumentEntity(submitEntity) 
    } 
} 
+0

@vadian感謝。 –

回答

0

你要解碼的對象。根據NSCoding a Bool而不是的一個對象。

原始類型的專用方法。

isSelected = aDecoder.decodeBool(forKey: "isSelected") 

廣告,請符合命名約定的變量名稱以小寫字母,並駝峯格式而不是snake_cased用於更新和答案

相關問題