变量'offerCardsShuffled'推断为类型'()',这可能是意外的 [英] Variable 'offerCardsShuffled' inferred to have type '()', which may be unexpected

查看:65
本文介绍了变量'offerCardsShuffled'推断为类型'()',这可能是意外的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与该问题有关,但有一个不同的错误:

Related to this question, but a different error: Other Question

我从这个问题中尝试了Swift 3和Swift 4代码:

I have tried both the Swift 3 as well as the Swift 4 code from this question:

随机链接

我正在使用Swift 3.2,并且在使用Swift 3代码时,因为不使用SwapAt而对我大吼大叫,所以我包含了Swift 4代码,但是我尝试了两者,结果相同.

I am using Swift 3.2, and when using the Swift 3 code it yelled at me for not using SwapAt, so I've included the Swift 4 code, but I tried both, with the same result.

当使用shuffle()方法时,我得到错误变量'offerCardsShuffled'推断为类型为'()',可能是意外的,随后出现了明显的错误,即 Value元组类型()"的成员没有枚举"成员

When using the shuffle() method, I get the error Variable 'offerCardsShuffled' inferred to have type '()', which may be unexpected, and the obvious error afterwards, Value of tuple type '()' has no member 'enumerated'

我不确定是什么原因造成的,因为其他使用此代码的人似乎没有这个问题.为什么我打乱的数组会变成空的元组?

I'm not sure what would be causing this, as other people using this code don't seem to be having that problem. Why are my shuffled arrays coming up as empty tuples?

请注意,我是Swift开发的新手,因此进行详尽的解释绝对有帮助.

Note I am fairly new to Swift development so a thorough explanation is definitely helpful.

    func displayOfferCards() -> Void {
        //let offerCardsr = allOfferCards().reversed()
        var offerCards = allOfferCards()
        var offerCardsShuffled = offerCards.shuffle()

        for (index, offerCard) in offerCardsShuffled.enumerated() {
            let delay = Double(index) * 0.2
            offerCard.display(delay: delay)
        }
    }
}

func allOfferCards() -> [OfferCard]{

    guard dataSource != nil else {
        return []
    }

    let numberOfCards = self.dataSource!.kolodaNumberOfCards(self)

    var offerCards = [OfferCard]()

    for i in 0..<numberOfCards {
        let offerCard = viewForCard(at: i)

        if let offerCard = offerCard {
            offerCards.append(offerCard as! OfferCard)
        }

    }

    return offerCards
}

extension MutableCollection {
    /// Shuffles the contents of this collection.
    mutating func shuffle() {
        let c = count
        guard c > 1 else { return }

        for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
            let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
            let i = index(firstUnshuffled, offsetBy: d)
            swapAt(firstUnshuffled, i)
        }
    }
}

extension Sequence {
    /// Returns an array with the contents of this sequence, shuffled.
    func shuffled() -> [Element] {
        var result = Array(self)
        result.shuffle()
        return result
    }
}

推荐答案

您的 shuffle 方法会修改原始集合,但不会返回新集合.

Your shuffle method modifies the original collection, it does not return a new collection.

更改:

var offerCardsShuffled = offerCards.shuffle()

收件人:

offerCards.shuffle()

,并在其余代码中将 offerCardsShuffled 替换为 offerCards .

and replace the use of offerCardsShuffled with offerCards in the remaining code.

这篇关于变量'offerCardsShuffled'推断为类型'()',这可能是意外的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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