Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在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 - Fatal编程技术网

如何在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 c
NSMutableDictionary
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…
集合类型
  • userfollowerarray
    声明为Swift数组

    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>
    }
    
  • 字典
    作为
    var
    iable获取

    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
}