如何在Swift 3中更新字典密钥 if(userfollowArray[indepath.row][“watcher\u following”]as!Bool)=false{ 让dict:NSMutableDictionary=userfollowArray[indexPath.row]作为!NSMutableDictionary 打印(dict) dict[“观察者”]=1 self.userfollowerArray[indexPath.row]=dict as!Dictionary }
我想用如何在Swift 3中更新字典密钥 if(userfollowArray[indepath.row][“watcher\u following”]as!Bool)=false{ 让dict:NSMutableDictionary=userfollowArray[indexPath.row]作为!NSMutableDictionary 打印(dict) dict[“观察者”]=1 self.userfollowerArray[indexPath.row]=dict as!Dictionary },swift,dictionary,Swift,Dictionary,我想用true/false更新watcher\u following键,但它会引发异常 [\u TtGCs26\u迅捷延迟词典sp__ _swift_setObject:forKeyedSubscript::发送到实例的选择器无法识别 声明var对于可变类型,let仅适用于不可变/常量类型。不需要使用objective cNSMutableDictionarytype if (userFollowingArray[indexPath.row]["watcher_following"] as! B
true/false
更新watcher\u following
键,但它会引发异常
[\u TtGCs26\u迅捷延迟词典sp__
_swift_setObject:forKeyedSubscript::发送到实例的选择器无法识别
声明
var
对于可变类型,let
仅适用于不可变/常量类型。不需要使用objective cNSMutableDictionary
type
if (userFollowingArray[indexPath.row]["watcher_following"] as! Bool) == false {
let dict : NSMutableDictionary = userFollowingArray[indexPath.row] as! NSMutableDictionary
print(dict)
dict["watcher_following"] = 1
self.userFollowingArray[indexPath.row] = dict as! Dictionary<String, Any>
}
if dict[“watcher\u following”]as!Bool==false{
var dict:Dictionary=userfollowArray[indexPath.row]as!Dictionary
dict[“观察者”]=1
self.userfollowerArray[indexPath.row]=dict as!Dictionary
}
不要在Swift中使用NSMutable…
集合类型
- 将
声明为Swift数组userfollowerarray
if dict["watcher_following"] as! Bool == false { var dict : Dictionary<String, Any> = userFollowingArray[indexPath.row] as! Dictionary<String, Any> dict["watcher_following"] = 1 self.userFollowingArray[indexPath.row] = dict as! Dictionary<String, Any> }
- 将
作为字典
iable获取var
var userFollowingArray = [[String:Any]]()
- 检查值
var dict = userFollowingArray[indexPath.row]
- 如有必要,请进行更新,并将字典分配回数组
由于值语义,最后一步是必需的if dict["watcher_following"] as! Bool == false {
使用自定义结构或类比使用字典方便得多。将
let dict
更改为var dict
,然后尝试使用它?为什么在var中使用NSMutableDictionary而不是Swift dictionary?为什么要强制铸造一切?这是Swift,但它看起来更像Objective-C而不是Swift 3…现在它可以工作了,谢谢
dict["watcher_following"] = true
userFollowingArray[indexPath.row] = dict
}