0

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

Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30
Lightno19
  • 13
  • 4
  • 2
    You have a ! at the end of that line which will crash if the sound can't be found. I would suggest that you use ? to conditionally unwrap and handle the error more gracefully and check your file names carefully – Paulw11 Jun 23 '16 at 02:06
  • Thank you for learning iOS this way – esreli Jun 23 '16 at 03:01
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Kurt Revis Jun 23 '16 at 05:27

1 Answers1

0
//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.

Lazy
  • 670
  • 5
  • 14
  • I tried the code and it tells me "Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?" and if I add the ! then it tells me "Initializer for conditional binding must have Optional type, not 'NSURL' – Lightno19 Jun 23 '16 at 03:22
  • Nice! Ok the app works now but there's something going on that I'm investigating now. When I tap the sixth button the seventh sound starts playing; and when I tap the seventh button the eighth sound is played and so on. When the last button is tapped (#48) the app crashed, as expected. However I don't understand what's going on since the button tags are correctly placed from 0-47. – Lightno19 Jun 23 '16 at 05:54
  • Please check if right indices are being used. And an up vote is appreciated! – Lazy Jun 23 '16 at 05:57
  • Yep the array had a file name written wrong. Thanks a lot! I tried to give you an up vote but it says you need 15 rep for it to show up (http://i.imgur.com/pvJkurH.png). I did tick the green checkmark though. – Lightno19 Jun 23 '16 at 06:13
  • Thanks, happy swifting! – Lazy Jun 23 '16 at 06:14