向数组追加添加延迟后出现致命错误 [英] Fatal Error After Adding Delay to Array Append

查看:26
本文介绍了向数组追加添加延迟后出现致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的二十一点应用程序,目前正在努力添加延迟,以便在点击交易按钮后显示卡片.因此,当您单击交易时,卡片不会同时出现,而是一次一张.为此,我尝试使用 DispatchQueue 来添加延迟.我的交易按钮如下:

I am trying to make a simple blackjack app and am currently working on adding a delay to make the cards appear after clicking the deal button. So when you click deal, the cards don't all appear at once, they go one at a time. To do this I am trying to use DispatchQueue to add the delay. My deal button is as follows:

func deal(deck: inout [Card]){
        for dealRound in 1...2{
            var dealCard = deck.last
            deck.removeLast()
            DispatchQueue.main.asyncAfter(deadline: .now() + 2){
                player1.hand.append(dealCard!)
            }
            //player1.hand.append(dealCard!)
            switch dealCard!.rank {
            case 1:
                player1.cardScore += 1
                player1.hasAce = true
            case 11,12,13:
                player1.cardScore += 10
            default:
                player1.cardScore += dealCard!.rank
            }
            dealCard = deck.last
            deck.removeLast()
            dealer.hand.append(dealCard!)
            //If dealRound == 2, dealing upcard to dealer so include that in score
            //Otherwise the down card is not included in the score until after the downcard is revealed
            if dealRound == 2{
                switch dealCard!.rank {
                case 1:
                    dealer.cardScore += 1
                    dealer.hasAce = true
                case 11,12,13:
                    dealer.cardScore += 10
                default:
                    dealer.cardScore += dealCard!.rank
                }
            }
        }
        isDealt = true
    }

而 player1 是玩家类的 @state 变量.显示卡片的图像是:

And player1 is a @state variable of a player class. The images to display the cards is:

if isDealt{
                            Image(player1.hand[0].suit.rawValue + String(player1.hand[0].rank) ).resizable().frame(width:120, height:160)
                            Image(player1.hand[1].suit.rawValue + String(player1.hand[1].rank) ).resizable().frame(width:120, height:160).offset(x: 40, y: -40)
                        }

当我以 DispatchQueue 延迟运行程序时,我得到致命错误:索引超出范围";尝试访问图像的 player1.hand[0] 时.是否有不同的方式我应该尝试添加延迟,如描述的那样?我不确定它是如何索引超出范围的,因为如果交易函数返回,应该将卡片附加到手牌上.我看过其他帖子,他们使用 DispatchQueue 来更新状态变量,但它们通常是布尔值,所以这不适用于我想要做的事情吗?

When I run the program with the DispatchQueue delay, I get "Fatal Error: Index out of range" when trying to access player1.hand[0] for the image. Is there a different way I should be trying to add a delay as described? I am not sure how it is indexing out of range because the card should be appended to the hand if the deal function returns. I have looked at other posts and they use DispatchQueue to update state variables but they are usually booleans, so does this not work with what I am trying to do?

推荐答案

等待:

        DispatchQueue.main.asyncAfter(deadline: .now() + 2){
            player1.hand.append(dealCard!)
        }

您可以将其替换为:

Thread.sleep(forTimeInterval: 2)  // <-- everything will wait here for 2 sec
player1.hand.append(dealCard!) 

这篇关于向数组追加添加延迟后出现致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆