我正在构建一个flashcard应用程序。我在下面附上了我的代码。当下面的代码运行时,用户应该能够向左和向右滑动一系列卡片(每张卡片包含一个与声音相结合的图像。有两个名为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.
}
}
}
}
包含每张“Card”信息的类位于
之下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)
}
}
}
答案 0 :(得分:0)
第一个问题:
result + list.words
result
是[String]
,list.words
是[Card]
。你想用这条线做什么?
第二个问题:
imgPhoto.image = (wordLists)[0]; ).image
wordLists
是[List]
,每个[Card]
都包含words
作为属性imgPhoto.image = wordLists[0].words[0].image
看起来你正试图从其中一张卡片中的一张卡片中抓取一张图片,但不清楚你要抓的是哪张卡片。从第一个列表中抓取第一张卡片中的图像的示例如下:
itemList
第三个问题:
您尝试引用一个似乎不存在的名为var pcapp = require('pcap-parser');
var parser = pcapp.parse('C:/Users/.../data.pcap');
parser.on('packet', function(packet) {
console.log(packet);
});
的变量。它未在您列出的代码中的任何位置声明,但我怀疑它是针对该类的属性。