*****问题更新*****我以为我已经找到了所有这些东西,但最后遇到了另外一个问题。我的问题类(见下文)接受一个Int作为参数,以便定义一个"答案"财产到"问题"我正在创造的对象。
当我在视图控制器中时,我需要访问此answer属性才能进行检查。在我的checkAnswer函数中,(参见下面视图控制器中的代码)我正在订阅一个名为questionNumber的变量。我知道这是不正确的,我试图访问这个名为&#34的参数;正确答案"在我的"问题"的初始化类。
我是不是错了?
感谢上百万人的帮助。
******上一个问题********
我是使用Swift在我的第一个iOS应用上工作的新手。当我尝试让UIButton按下访问捆绑包中的随机声音文件数组时,我遇到了问题。我通过创建文件夹引用将文件添加到我的项目大纲中。
我设置了一个带有"问题的模型"需要一些参数的文件(不确定我是否正确...)
import Foundation
class Question {
var questionText: URL
var answer: Int
init(text: String, correctAnswer: Int){
questionText = Bundle.main.url(forResource: text, withExtension: "m4a", subdirectory: "Sounds")!
answer = correctAnswer
}
}
然后我做了一个"问题库"文件添加一些属性(即答案Int)
import AVFoundation
import Foundation
class QuestionBank {
var list = [Question]()
init(){
// creating a quiz item and appending it to the list
let item = Question(text: "13", correctAnswer: 0)
// add question to the list of items
list.append(item)
//creating the quiz item inside the append function
list.append(Question(text: "14", correctAnswer: 1))
list.append(Question(text: "15", correctAnswer: 2))
list.append(Question(text: "16", correctAnswer: 2))
list.append(Question(text: "17", correctAnswer: 1))
list.append(Question(text: "18", correctAnswer: 0))
list.append(Question(text: "19", correctAnswer: 0))
list.append(Question(text: "20", correctAnswer: 1))
list.append(Question(text: "21", correctAnswer: 2))
list.append(Question(text: "22", correctAnswer: 2))
list.append(Question(text: "23", correctAnswer: 0))
list.append(Question(text: "24", correctAnswer: 1))
}
}
我在View Controller中创建了一个处理按钮按下的功能。我在"尝试使用audioPlayer ..."表示"不能转换类型值的问题'问题'预期的论点' URL'
func playRandomSound() {
let sound1URL = allQuestions.list[0].questionText
let sound2URL = allQuestions.list[1].questionText
let sound3URL = allQuestions.list[2].questionText
let sound4URL = allQuestions.list[3].questionText
let sound5URL = allQuestions.list[4].questionText
let sound6URL = allQuestions.list[5].questionText
let sound7URL = allQuestions.list[6].questionText
let sound8URL = allQuestions.list[7].questionText
let sound9URL = allQuestions.list[8].questionText
let sound10URL = allQuestions.list[9].questionText
let sound11URL = allQuestions.list[10].questionText
let sound12URL = allQuestions.list[11].questionText
let soundURLArray = [sound1URL, sound2URL, sound3URL, sound4URL, sound5URL, sound6URL, sound7URL, sound8URL, sound9URL, sound10URL, sound11URL, sound12URL]
let randNo = Int(arc4random_uniform(UInt32(allQuestions.list.count)))
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOf: (soundURLArray[randNo]))
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
我还创建了一个响应"答案"我在"问题库中创建了#34;文件...
当我运行应用程序时,答案似乎正在起作用,但它们并不与从该捆绑包中播放的声音文件相关联。
不确定我到底搞砸了什么。
这里是我的View Controller文件中的所有代码供参考:
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var feedbackLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var progressBar: UIView!
let allQuestions = QuestionBank()
var pickedAnswer: Int?
var questionNumber: Int = 0
var score: Int = 0
var currentQuestion: Question?
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
feedbackLabel.alpha = 0
progressBar.alpha = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playButtonPressed(_ sender: UIButton) {
playRandomSound()
feedbackLabel.text = "Listen..."
feedbackLabel.alpha = 1
}
@IBAction func answerButtonPressed(_ sender: UIButton) {
if sender.tag == 1 {
pickedAnswer = 0
} else if sender.tag == 2 {
pickedAnswer = 1
} else if sender.tag == 3 {
pickedAnswer = 2
}
progressBar.alpha = 1
checkAnswer()
questionNumber += 1
nextQuestion()
}
func playRandomSound() {
let sound1URL = allQuestions.list[0].questionText
let sound2URL = allQuestions.list[1].questionText
let sound3URL = allQuestions.list[2].questionText
let sound4URL = allQuestions.list[3].questionText
let sound5URL = allQuestions.list[4].questionText
let sound6URL = allQuestions.list[5].questionText
let sound7URL = allQuestions.list[6].questionText
let sound8URL = allQuestions.list[7].questionText
let sound9URL = allQuestions.list[8].questionText
let sound10URL = allQuestions.list[9].questionText
let sound11URL = allQuestions.list[10].questionText
let sound12URL = allQuestions.list[11].questionText
let soundURLArray = [sound1URL, sound2URL, sound3URL, sound4URL, sound5URL, sound6URL, sound7URL, sound8URL, sound9URL, sound10URL, sound11URL, sound12URL]
let randNo = Int(arc4random_uniform(UInt32(allQuestions.list.count)))
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOf: (soundURLArray[randNo]))
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
func updateUI() {
scoreLabel.text = "Score: \(score)"
progressLabel.text = "Round \(questionNumber) of \(String(allQuestions.list.count))"
progressBar.frame.size.width = (view.frame.size.width / 12) * CGFloat(questionNumber) //make this dynamic (the 12)
}
func nextQuestion() {
do{
updateUI()
if questionNumber <= allQuestions.list.count - 1 {
let randNo = Int(arc4random_uniform(UInt32(allQuestions.list.count)))
currentQuestion = allQuestions.list[randNo]
try audioPlayer = AVAudioPlayer(contentsOf: (currentQuestion!.questionText))
}
else {
let alert = UIAlertController(title: "Game Over", message: "Start Over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startOver()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
} catch {
print (error)
}
}
func startOver() {
questionNumber = 0
feedbackLabel.alpha = 0
score = 0
progressBar.alpha = 0
scoreLabel.text = "0"
progressLabel.text = "0"
}
func checkAnswer() {
let correctAnswer = currentQuestion?.answer
if correctAnswer == pickedAnswer {
feedbackLabel.text = "You Got it! ✔️"
score += 1
} else {
feedbackLabel.text = "Wrong ✖️"
}
}
}
非常感谢任何帮助!!!!
答案 0 :(得分:2)
在playRandomSound函数中,您要在allQuestions.list中设置对象的URL,这是一个Question对象列表。您需要获取.questionText属性才能获取路径。像这样:
let sound1URL = allQuestions.list[0].questionText
let sound2URL = allQuestions.list[1].questionText
let sound3URL = allQuestions.list[2].questionText
也在您的问题类中,更改:
var questionText = Bundle.main.paths(forResourcesOfType: "mp3", inDirectory: "Sounds")
为:
var questionText: URL
并将以下内容添加到init():
Bundle.main.url(forResource: text, withExtension: "mp3", subdirectory: "Sounds")
目前您正在将questionText设置为包含传入init的文本的String数组。
新的问题类看起来像这样:
class Question {
var questionText: URL
let answer: Int
init(text: String, correctAnswer: Int){
questionText = Bundle.main.url(forResource: text, withExtension: "mp3", subdirectory: "Sounds")
answer = correctAnswer
}
}
关于您的新问题,请尝试通过将其设置为nextQuestion方法来跟踪当前问题:
if questionNumber <= allQuestions.list.count - 1 {
let randNo = Int(arc4random_uniform(UInt32(allQuestions.list.count)))
currentQuestion = allQuestions.list[randNo] //Add this line
try audioPlayer = AVAudioPlayer(contentsOf: currentQuestion.questionText) //You would no longer need the array of all URLs
}
在playRandomSound()方法中,用相同的方法替换soundArray:
let randNo = Int(arc4random_uniform(UInt32(allQuestions.list.count)))
currentQuestion = allQuestions.list[randNo]
用随机播放声音 尝试audioPlayer = AVAudioPlayer(contentsOf:currentQuestion.questionText)
代替soundArray中的条目
然后在checkAnswer中你可以:
let correctAnswer = currentQuestion.answer
而不是基于你的questionNumber变量。