gpt4 book ai didi

ios - 组合多个数组。快速调试 xcode

转载 作者:行者123 更新时间:2023-11-28 08:04:45 26 4
gpt4 key购买 nike

我正在构建抽认卡应用程序。我在下面附上了我的代码。当下面的代码运行时,用户应该能够左右滑动卡片数组(每张卡片包含一个图像和一个声音。有两个卡片数组称为 firstList 和 secondList)。当点击图像时,应该播放与之耦合的声音。我在播放声音或在单个列表中滑动时没有遇到问题。但是,当我尝试合并和滑动“firstList”和“secondList”时出现错误。我似乎无法弄清楚的错误之一是“表达式类型在没有更多上下文的情况下不明确”。请查看下面的代码和错误

import UIKit

class SecondViewController: UIViewController , UIGestureRecognizerDelegate {

var imageIndex: Int = 0
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}

@IBOutlet weak var imgPhoto: UIImageView!

struct List {
let words: [Card] /*Create array of cards*/
var active: Bool
}



let firstList:[Card] = [
Card(image: UIImage(named: "lake")!, soundUrl: "lake"),
Card(image: UIImage(named: "river")!, soundUrl: "river"),
Card(image: UIImage(named: "ocean")!, soundUrl: "ocean")
]

let secondList:[Card] = [
Card(image: UIImage(named: "alligator")!, soundUrl: "alligator"),
Card(image: UIImage(named: "apple")!, soundUrl: "apple"),
Card(image: UIImage(named: "grape")!, soundUrl: "grape")
]

override func viewDidLoad() {


var imageList: [String] {
let list1 = List(words:firstList, active: true)
let list2 = List(words:secondList, active: true)

let wordLists = [list1, list2]

let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words

} else {
return result
}
}

return active

}

super.viewDidLoad()

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(tapGestureRecognizer)


imgPhoto.image = (wordLists)[0]; ).image

// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false

let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false

leftSwipe.direction = .left
rightSwipe.direction = .right

view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)

}


func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{

itemList[imageIndex].playSound()
// Your action
}
func Swiped(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

switch swipeGesture.direction {

case UISwipeGestureRecognizerDirection.right :
print("User swiped right")

// decrease index first

imageIndex -= 1

// check if index is in range

if imageIndex < 0 {

imageIndex = itemList.count - 1

}

imgPhoto.image = itemList[imageIndex].image

case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")

// increase index first

imageIndex += 1

// check if index is in range

if imageIndex > itemList.count - 1 {

imageIndex = 0

}

imgPhoto.image = itemList[imageIndex].image
default:


break //stops the code/codes nothing.
}
}
}
}

enter image description here

包含每个“卡片”信息的类如下

import Foundation; import UIKit; import AVFoundation

var player: AVAudioPlayer?

class Card: NSObject
{
var image: UIImage
var soundUrl: String


init(image: UIImage, soundUrl: String) {
self.image = image
self.soundUrl = soundUrl
}
func playSound()

{ print("play")
guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
print("hhh")
} catch let error {
print(error.localizedDescription)
}
}
}

最佳答案

第一个问题:

结果 + list.words

result 是一个 [String]list.words 是一个 [Card]。你想用这条线完成什么?

第二个问题:

imgPhoto.image = (wordLists)[0]; ).image

wordLists 是一个[List],每个列表都包含[Card]作为属性words

看起来您正在尝试从其中一个列表中的一张卡片中获取图像,但不清楚您要获取的是哪张卡片。从第一个列表的第一张卡片中抓取图像的示例是:

imgPhoto.image = wordLists[0].words[0].image

第三个问题:

您正试图引用一个名为 itemList 的变量,但它似乎并不存在。它没有在您列出的代码中的任何地方声明,但我怀疑它是针对该类的属性。

关于ios - 组合多个数组。快速调试 xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45494195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com