Lightno19 Lightno19 - 5 months ago 9
Swift Question

iOS programming noob, need help fixing error message

I've recently gotten into iOS development on my own and am building my first app: a Donald Trump soundboard. However, I got an error saying:


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


This is the viewcontroller text:

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()

}

}


Any help will be extremely appreciated.

Edit: fatal error shown at the bottom states:


unexpectedly found nil while unwrapping an Optional value

Answer
//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())

            }
        }

Try this updated code.

Comments