Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Ios ';(@lvalue Int)——>$T7&x27;与';字典<;Int,Int>';_Ios_Swift_Dictionary - Fatal编程技术网

Ios ';(@lvalue Int)——>$T7&x27;与';字典<;Int,Int>';

Ios ';(@lvalue Int)——>$T7&x27;与';字典<;Int,Int>';,ios,swift,dictionary,Ios,Swift,Dictionary,我对编程相当陌生,所以要友善。我正在使用最新版本的Xcode构建一个tic-tac-toe游戏,只是为了好玩(单视图应用程序) var plays=Dictionary() var done=false var-aiDeciding=false @iAction func UIButton已选中(发件人:UIButton){ userMessage.hidden=true 如果!播放(sender.tag)&&&!辅助决策&&!完成{ setImageForSpot(sender.tag,播放器

我对编程相当陌生,所以要友善。我正在使用最新版本的Xcode构建一个tic-tac-toe游戏,只是为了好玩(单视图应用程序)

var plays=Dictionary()
var done=false
var-aiDeciding=false
@iAction func UIButton已选中(发件人:UIButton){
userMessage.hidden=true
如果!播放(sender.tag)&&&!辅助决策&&!完成{
setImageForSpot(sender.tag,播放器:1)
}
在条件if语句(
if!plays(sender.tag)
)中,我得到一个错误,它说:

“(@lvalue Int)->$T7”与“Dictionary”不同


使用
plays
字典时会遇到一些问题

最简单的方法是使用
()
而不是
[]
进行下标访问(即
播放[sender.tag]

接下来,您不能在整数上使用
——与某些基于C语言的语言不同,您不能在Swift中将整数用作布尔值。因此您必须将其与特定值进行比较。您可能正在检查它是否等于0

最后,字典返回Swift中的可选值,您可能需要将其展开。但好消息是,如果您只关心它是否等于0,那么您可以将其与值进行比较,而无需展开它

因此,这应该是可行的:

if plays[sender.tag] == 0 && !aiDeciding && !done {
    // etc…
}

字典中的第二个Int代表什么?if语句将其视为布尔值,但它不是,它是一个Int。此外,
plays(sender.tag)
将返回可选项,因为字典中可能没有与
sender.tag
匹配的项,因此需要将其展开。
if plays[sender.tag] == 0 && !aiDeciding && !done {
    // etc…
}