iOS 编程菜鸟,需要帮助修复错误消息

标签 ios xcode swift

我最近开始自己进行 iOS 开发,并正在构建我的第一个应用程序:Donald Trump 音板。但是,我收到一条错误消息:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

这是 View Controller 文本:

import UIKit
import iAd
import AVFoundation

class SecondViewController: UIViewController {

// Array of every sound file
let soundFilenames = ["china", "drugs_killers_rapists", "america_great_again", "are_you_gay", "Mexico", "i_just_want_them_to_suffer", "are_you_homosexual", "are_you_surprised", "hes_a_loser", "isis_trump", "fantastic", "the_american_dream_is_dead", "wait_dummies", "special_guy", "I'm_really_rich", "speak_english", "so_probably_i'll_sue_her", "ladies", "ill_build_a_wall", "political_bullshit", "ima_bomb_em", "back_to_univision", "hes_a_pussy", "piece_of_garbage", "i_love_mexicans", "i_love_china", "i_love_saudis", "sit_down", "small_loan", "youre_fired", "lets_see_what_happens", "enough", "congratulations", "why", "are_you_anti_semite", "youre_the_boss", "1mm", "tell_it_like_it_is", "100b", "is_that_right", "hes_insecure", "beaten_up", "I_beat_China_all_the_time", "nonono", "ive_been_watching_you", "motivate_you", "okay_okay", "meatloaf"]

// Array of AudioPlayers for each file
var audioPlayers = [AVAudioPlayer]()

// Outlet for the ScrollView
@IBOutlet weak var ScrollView: UIScrollView!

var bannerView: ADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Set up scroll view to hold 48 buttons
    ScrollView.contentSize.height = 1900

    //Set up audio players
    for sound in soundFilenames {

    do {

            // Try to do something
            //THIS NEXT LINE IS WHERE THE BREAKPOINT 
            let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)

            let audioPlayer = try AVAudioPlayer(contentsOfURL: 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 tapped button
    let audioPlayer = audioPlayers[sender.tag]
    audioPlayer.play()

}

}

任何帮助将不胜感激。

编辑: fatal error 显示在底部状态:

unexpectedly found nil while unwrapping an Optional value

最佳答案

//Set up audio players
        for sound in soundFilenames {
            do {
                // Try to do something
                if let pathOfResource = NSBundle.mainBundle().pathForResource(sound, ofType: "mp3") {
                    let url = NSURL(fileURLWithPath: pathOfResource)
                    let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
                    audioPlayers.append(audioPlayer)
                }
            }
            catch {
                // Catch the error that is thrown
                audioPlayers.append(AVAudioPlayer())

            }
        }

尝试这个更新的代码。

关于iOS 编程菜鸟,需要帮助修复错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37981101/

相关文章:

ios - 检测应用程序是否是临时的

ios - 基于 nil 变量不返回任何单元格

objective-c - 错误 : unrecognized selector sent to instance

c - 链接 C 代码时出现重复符号错误

xcode - 带有原型(prototype)单元格的 tableView.insertRowsAtIndexPaths

swift - UICollectionViewCell 在横向模式下并排放置单元格

ios - 是否可以在 Swift 中重载 += 运算符?

ios - 具有可变 contentSize 的 UIScrollView

ios - 当前位置跨度在 map 套件 View 上更改为随机数

android - iOS Android - 如何为应用程序创建下载链接?