gpt4 book ai didi

ios - 没有更多上下文,表达式类型不明确。 xcode 快速

转载 作者:行者123 更新时间:2023-11-28 08:04:46 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(image: UIImage(named: "lake")!, soundUrl: "Lake"),
Card(image: UIImage(named: "lamb")!, soundUrl: "Lamb"),
Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"),
Card(image: UIImage(named: "lark")!, soundUrl: "Lark"),
Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"),
Card(image: UIImage(named: "leash")!, soundUrl: "Leash"),
Card(image: UIImage(named: "left")!, soundUrl: "Left"),
Card(image: UIImage(named: "leg")!, soundUrl: "Leg"),
Card(image: UIImage(named: "lime")!, soundUrl: "Lime"),
Card(image: UIImage(named: "lion")!, soundUrl: "Lion"),
Card(image: UIImage(named: "lips")!, soundUrl: "Lips"),
Card(image: UIImage(named: "list")!, soundUrl: "List"),
Card(image: UIImage(named: "lock")!, soundUrl: "Lock"),
Card(image: UIImage(named: "log")!, soundUrl: "Log"),
Card(image: UIImage(named: "look")!, soundUrl: "Look"),
Card(image: UIImage(named: "love")!, soundUrl: "Love"),
Card(image: UIImage(named: "lunch")!, soundUrl: "Lunch")
]

let secondList = [
Card(image: UIImage(named: "lake")!, soundUrl: "Lake"),
Card(image: UIImage(named: "lamb")!, soundUrl: "Lamb"),
Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"),
Card(image: UIImage(named: "lark")!, soundUrl: "Lark"),
Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"),

]




override func viewDidLoad() {
super.viewDidLoad()

let list1 = List(words:firstList, active: true)
let list2 = List(words:secondList, active: true)

var imageList: [String]{
let wordLists = [list1, list2]

print((wordLists[0] ).words[0].soundurl)


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

} else {
return result
}
}

return active

}


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

imgPhoto.image = (imageList[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)

}

@IBAction func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
print("hhh")
imageList[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 = words.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)
}
}
}

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