I have this error:" fatal error: unexpectedly found nil while unwrapping an Optional value" in Xcode 8 swift 3
I have this error:" fatal error: unexpectedly found nil while unwrapping an Optional value" in Xcode 8 swift 3
这是我的代码
//
// ViewController.swift
// morpher app soundboard
//
// Created by Jared Evan Miller on 7/24/17.
// Copyright © 2017 Jared Evan Miller. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["5","8","7","4","6","Sound3forbutton3","sound2forbutton1","sound2forbutton2"]
var audioPlayers = [AVAudioPlayer]()
var lastAudioPlayer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set up audio players
for sound in soundFilenames {
do {
// Try to do something
let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!)
let audioPlayer = try AVAudioPlayer (contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
// Catch the error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play()
}
@IBAction func tbuttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play()
}
}
我该如何解决这个问题?
此外,我收到此错误。这是代码中的部分。看看我改变了什么。当我 运行 它出现在我的 iPhone se.
时它有错误
![我的代码][2]
[2]: https://i.stack.imgur.com/V5WC3.png
请帮忙!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
您正在强制展开 Budle.main.path... 的结果,因为这是失败的,这意味着您的资源未正确加载。
确保您的声音文件与您的应用捆绑在一起。您可以通过转到项目设置来检查这一点,然后在 "Build Phases" 下将文件添加到 "Copy Bundle Resources" 部分(如果它们还不存在)。
for sound in soundFilenames {
guard let urlString = Bundle.main.path(forResource: sound, ofType: "wav") else {
print("Sound file not found: \(sound)")
continue
}
let url = URL(fileURLWithPath:urlString)
do {
// Try to do something
let audioPlayer = try AVAudioPlayer (contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
// Catch the error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}
这是我的代码
//
// ViewController.swift
// morpher app soundboard
//
// Created by Jared Evan Miller on 7/24/17.
// Copyright © 2017 Jared Evan Miller. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["5","8","7","4","6","Sound3forbutton3","sound2forbutton1","sound2forbutton2"]
var audioPlayers = [AVAudioPlayer]()
var lastAudioPlayer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set up audio players
for sound in soundFilenames {
do {
// Try to do something
let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!)
let audioPlayer = try AVAudioPlayer (contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
// Catch the error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play()
}
@IBAction func tbuttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play()
}
}
我该如何解决这个问题?
此外,我收到此错误。这是代码中的部分。看看我改变了什么。当我 运行 它出现在我的 iPhone se.
时它有错误![我的代码][2]
[2]: https://i.stack.imgur.com/V5WC3.png 请帮忙!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
您正在强制展开 Budle.main.path... 的结果,因为这是失败的,这意味着您的资源未正确加载。
确保您的声音文件与您的应用捆绑在一起。您可以通过转到项目设置来检查这一点,然后在 "Build Phases" 下将文件添加到 "Copy Bundle Resources" 部分(如果它们还不存在)。
for sound in soundFilenames {
guard let urlString = Bundle.main.path(forResource: sound, ofType: "wav") else {
print("Sound file not found: \(sound)")
continue
}
let url = URL(fileURLWithPath:urlString)
do {
// Try to do something
let audioPlayer = try AVAudioPlayer (contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
// Catch the error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}