无法对不可变值使用变异成员:函数调用返回不可变值-不知道为什么值不可变 [英] Cannot use mutating member on immutable value: function call returns immutable value - not sure why value is immutable

查看:204
本文介绍了无法对不可变值使用变异成员:函数调用返回不可变值-不知道为什么值不可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift开发相对较新。试图修改其他人编写的几个函数。目的是在用户登录后(尚未设置该部分)在第一次调用时保留原始路径,并在随后的调用中使用另一条路径。

Relatively new to Swift development. Trying to modify a couple functions someone else wrote. The goal is to leave the original pathway on the first time calling this after user login (that part hasn't been setup yet) and on subsequent calls, use another pathway.

因此,在编写用于定向首次或非第一次通过的逻辑之前,我正在测试非第一次通过逻辑。

So before writing the logic that will direct to first or non-first pass through, I am testing the non-first pass through logic.

这里是allOfferCards():

Here's allOfferCards():

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
}

这是我尝试进行更改的地方。原始逻辑反转了allOfferCards()的返回值。我想使用一个称为随机播放的自定义函数,它将数组随机化。

And here is where I am trying to make the changes. The original logic reverses the return from allOfferCards(). I want to use a custom function called "shuffle" that will randomize the array.

func displayOfferCards() -> Void {
    // What was here originally
    //let offerCards = allOfferCards().reversed()
    var offerCards = allOfferCards().shuffle()

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

这里是随机播放功能

extension Array
{
    /** Randomizes the order of an array's elements. */
    mutating func shuffle()
    {
        for _ in 0..<10
        {
            sort { (_,_) in arc4random() < arc4random() }
        }
    }
}

但是何时我尝试运行此命令,但标题出现错误-无法在不可变值上使用变异成员。但是我不确定为什么allOfferCards()会生成不可变的值-var offerCards是使用var关键字而不是let关键字定义的-这应该意味着它是可变的正确的吗?

However when I try to run this I get the error in the title - can't use a mutating member on an immutable value. However I'm not sure why allOfferCards() is generating an immutable value - var offerCards is defined using a var keyword, not a let keyword - this should mean it is mutable correct?

我在这里做什么错了?

推荐答案

问题是在分配函数的返回值之前,该值本身是不可变的,因此无法直接完成对其的调用。您应该先将返回值分配给可变变量,然后在其上调用mutating函数。

The issue is that before assigning the return value of a function, the value itself is immutable, so calling a mutating function on it cannot be directly done. You should assign the return value first to a mutable variable, then call the mutating function on it.

func displayOfferCards() -> Void {
    var offerCards = allOfferCards()
    offerCards.shuffle()

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

这篇关于无法对不可变值使用变异成员:函数调用返回不可变值-不知道为什么值不可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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