0

Im extremely new to iOS development, and I'm trying to make a simple soundboard app.

i've watched various tutorials, and videos, but i can't seem to get my code to work like it does in the example. - maybe because the video is objective C or an older version of swift - I dont know.

https://www.youtube.com/watch?v=hzQCLdd2X-A - seems to be the easiest way i've found.

copying his code:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var sound01 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("filename", ofType: "mp3")!)
    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        audioPlayer = AVAudioPlayer(contentsOfURL: sound01, error: nil)

        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

in his version, he doesn't get any errors: - yet in mine, i get:

Incorrect argument label in call (have 'contentsOfURL:error:', expected 'contentsOfURL:fileTypeHint:')

when you use the fix it suggestion, it becomes:

audioPlayer = AVAudioPlayer(contentsOfURL: sound01, fileTypeHint: nil)

now i get this error:

Call can throw, but it is not marked with 'try' and the error is not handled

what am I doing wrong?

MatthewD
  • 6,719
  • 5
  • 22
  • 41
  • Duplicate of [Swift 2: Call can throw, but it is not marked with 'try' and the error is not handled](http://stackoverflow.com/questions/30737262/swift-2-call-can-throw-but-it-is-not-marked-with-try-and-the-error-is-not-ha). See also [the error handling documentation](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html). – Aaron Brager Sep 30 '15 at 13:41
  • Learn Swift 2's `do try catch` syntax: http://stackoverflow.com/questions/30720497/swift-do-try-catch-syntax – Eric Aya Sep 30 '15 at 13:42

1 Answers1

0

Try to replace that line:

audioPlayer = AVAudioPlayer(contentsOfURL: sound01, error: nil)

With this:

audioPlayer = try! AVAudioPlayer(contentsOfURL: sound01!)