在NSCoder中展开Optional值时,意外地发现了nil
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在NSCoder中展开Optional值时,意外地发现了nil相关的知识,希望对你有一定的参考价值。
我在isSelected(Bool)
得到零错误。请查看以下截图以获取更多详细信息
进口基金会
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)
}
}
答案
你要解码一个对象。就NSCoding
而言,Bool
不是一个对象。
原始类型有专用方法。
isSelected = aDecoder.decodeBool(forKey: "isSelected")
广告请遵守命名约定,即变量名称以小写字母开头并且是camelCased而不是snake_cased
以上是关于在NSCoder中展开Optional值时,意外地发现了nil的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中播放音乐:在展开 Optional 值时意外发现 nil
value 不是 nil,而是在展开 Optional 值时意外发现 nil
Swift 致命错误:在 segue 上展开 Optional 值时意外发现 nil
iOS/Xcode:Koloda 框架:在隐式展开 Optional 值时意外发现 nil
为啥我在尝试在 iOS Swift 中向服务器发出 post 请求时收到此错误:在展开 Optional 值时意外发现 nil?