Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何检查每两张翻转的卡片';他们在一个数组中按钮的当前标题在一个卡片匹配游戏中匹配_Ios_Swift - Fatal编程技术网

Ios 如何检查每两张翻转的卡片';他们在一个数组中按钮的当前标题在一个卡片匹配游戏中匹配

Ios 如何检查每两张翻转的卡片';他们在一个数组中按钮的当前标题在一个卡片匹配游戏中匹配,ios,swift,Ios,Swift,我正在为这个iOS swift匹配卡游戏添加一些功能,我需要检查前两个按钮是否在匹配上翻转。他们有四种表情符号来表示八张卡片,这意味着有四对匹配。我很难找到如何检查卡是否匹配,当它们匹配时,我需要按钮的背景色为不透明(不可见)。除了chooseCard函数中的concentration类中的空if语句外,其他所有语句都可以工作。那就是我需要帮助的地方。 以下是所有代码,您可以查看与以下内容相关的内容: class ViewController: UIViewController { l

我正在为这个iOS swift匹配卡游戏添加一些功能,我需要检查前两个按钮是否在匹配上翻转。他们有四种表情符号来表示八张卡片,这意味着有四对匹配。我很难找到如何检查卡是否匹配,当它们匹配时,我需要按钮的背景色为不透明(不可见)。除了chooseCard函数中的concentration类中的空if语句外,其他所有语句都可以工作。那就是我需要帮助的地方。 以下是所有代码,您可以查看与以下内容相关的内容:

class ViewController: UIViewController {

    lazy var game = Concentration(numberOfPairsOfCards: (cardButtons.count + 1) / 2)

    var flipCount = 0 {
        // Property Observer
        didSet { flipLabel.text = "Flips: \(flipCount)" }
    }

    @IBOutlet weak var flipLabel: UILabel!

    @IBOutlet var cardButtons: [UIButton]!

    @IBAction func touchCard(_ sender: UIButton) {
        flipCount+=1
        if let cardNumber = cardButtons.firstIndex(of: sender) {
            game.chooseCard(at: cardNumber)
            updateViewFromModel()
        } else {
            print ("chosen card was not in cardButtons")
        }

    }

    var emojiChoices = ["In your concentration class you will check for faceUp card indexes 

Change your Concentration from class to Struct

struct Concentration { // instead of class Concentration

private var indexOfFaceUpCard: Int? {
        get {
            let faceUpCardIndices = cards.indices.filter { cards[$0].isFaceUp }
            return faceUpCardIndices.count == 1 ? faceUpCardIndices.first : nil
        } set {
            for index in cards.indices {
                cards[index].isFaceUp = (index == newValue)
            }
        }
    } 
类ViewController:UIViewController{
lazy var game=专注(卡片对数:(cardButtons.count+1)/2)
var flipCount=0{
//财产观察员
didSet{flipLabel.text=“Flips:\(flipCount)”}
}
@IBVAR flipLabel:UILabel!
@IBVAR卡按钮:[UIButton]!
@iAction func触摸卡(发送方:UIButton){
flipCount+=1
如果let cardname=cardButtons.firstIndex(of:sender){
游戏。选择卡片(地址:cardNumber)
updateViewFromModel()
}否则{
打印(“所选卡片不在cardButtons中”)
}
}

var emojiChoices=[“在您的专注类中,您将检查faceUp卡索引

将注意力从类转移到结构

mutating func chooseCard(at index: Int)
然后你有了变异的chooseCard方法

if !cards[index].isMatched {
            if let matchIndex = indexOfFaceUpCard, matchIndex != index {
                if cards[matchIndex] == cards[index] {
                    cards[matchIndex].isMatched = true
                    cards[index].isMatched = true
                }
                cards[index].isFaceUp = true

            } else {
                indexOfFaceUpCard = index
            }
        }
在chooseCard方法中,检查是否匹配

mutating func chooseCard(at index: Int) {

        if !cards[index].isMatched {
            if let matchIndex = indexOfFaceUpCard, matchIndex != index {
                if cards[matchIndex] == cards[index] {
                    cards[matchIndex].isMatched = true
                    cards[index].isMatched = true
                }
                cards[index].isFaceUp = true

            } else {
                indexOfFaceUpCard = index
            }
        }
}
所以你的方法是这样的

struct Card: Hashable {
    var isMatched = false
    var isFaceUp = false
    var identifier: Int

    static var identifierFactory = 0

    static func getUniqueIdentifier() -> Int {
        Card.identifierFactory += 1
        return Card.identifierFactory
    }
  static func ==(lhs: Card, rhs: Card) -> Bool {
      return lhs.identifier == rhs.identifier
  }

    init() {
        self.identifier = Card.getUniqueIdentifier()
    }
} 
更新您的卡结构


在你的专注课程中,你将检查faceUp卡索引

将注意力从类转移到结构

mutating func chooseCard(at index: Int)
然后你有了变异的chooseCard方法

if !cards[index].isMatched {
            if let matchIndex = indexOfFaceUpCard, matchIndex != index {
                if cards[matchIndex] == cards[index] {
                    cards[matchIndex].isMatched = true
                    cards[index].isMatched = true
                }
                cards[index].isFaceUp = true

            } else {
                indexOfFaceUpCard = index
            }
        }
在chooseCard方法中,检查是否匹配

mutating func chooseCard(at index: Int) {

        if !cards[index].isMatched {
            if let matchIndex = indexOfFaceUpCard, matchIndex != index {
                if cards[matchIndex] == cards[index] {
                    cards[matchIndex].isMatched = true
                    cards[index].isMatched = true
                }
                cards[index].isFaceUp = true

            } else {
                indexOfFaceUpCard = index
            }
        }
}
所以你的方法是这样的

struct Card: Hashable {
    var isMatched = false
    var isFaceUp = false
    var identifier: Int

    static var identifierFactory = 0

    static func getUniqueIdentifier() -> Int {
        Card.identifierFactory += 1
        return Card.identifierFactory
    }
  static func ==(lhs: Card, rhs: Card) -> Bool {
      return lhs.identifier == rhs.identifier
  }

    init() {
        self.identifier = Card.getUniqueIdentifier()
    }
} 
更新您的卡结构



请编辑您的问题,将相关代码作为文本包含。请使用toolbar@Paulw11这是我一直坚持使用的代码。我更新了问题。请发布代码而不是截图。您需要在
卡的
结构中找到可以匹配的内容。例如,您的两张匹配卡必须有一些共同的属性,并且恩,您只需使用此属性比较所选的两张卡。@Paulw11我不确定如何引用按钮标题,因为这是按钮的唯一区别。这是我的第一个swift项目,我的导师对此进行了编码,但只编写了几个部分。我是一个完全的初学者,因此我不确定如何引用swift中的特定内容,尤其是来自不同文件的y。请编辑您的问题,将相关代码作为文本包含在内。请使用toolbar@Paulw11这是我一直坚持的代码。我更新了问题。请发布代码而不是截图。你需要在你的
结构中找到可以匹配的东西。即你的两张匹配卡必须有一些pr属性,然后您只需使用此属性比较所选的两张卡。@Paulw11我不确定如何引用按钮标题,因为这是按钮的唯一区别。这是我的第一个swift项目,我的导师对此进行了编码,但只编写了几个部分。我是一个完全的初学者,所以我不确定如何引用特定的标题swift中的ngs,特别是来自不同的文件。每当我尝试使用mutating时,它都会给我一个错误:mutating“对类或类绑定协议中的方法无效。mutating适用于值类型…类是引用类型…所以将您的注意力从类更改为结构…更新我的答案谢谢!还有,在您有我()我得到了未解析标识符的错误用法。在下一行中,它说二进制运算符“==”不能应用于两个“卡”操作数删除计时器…这是我的错
哈希表
并不意味着
可等式
。你得到了
=
的工作,因为你自己实现了这个运算符。这样,你就可以删除
Hashable
一致性,它仍然可以工作。但是如果您符合
Equatable
,您可以在
中删除
=
函数,因为编译器将为
结构
生成此函数。每当我尝试使用mutating时,它都会给我一个错误:mutating'对类或类绑定协议中的方法无效olsmutating适用于值类型…类是引用类型…所以将您的注意力从类更改为结构…更新了我的答案谢谢!还有,您有stoppTime()的地方我得到了未解析标识符的错误用法。在下一行中,它说二进制运算符“==”不能应用于两个“卡”操作数删除计时器…这是我的错
哈希表
并不意味着
可等式
。你得到了
=
的工作,因为你自己实现了这个运算符。这样,你就可以删除
Hashable
一致性,它仍然可以工作。但是如果您符合
Equatable
,您可以在
卡中删除
=
函数,因为编译器将为
结构生成此函数。