1

I just opened my old project in Xcode 7 beta. The code works perfectly fine in Xcode 6, but now it's showing many error. I don"t know what those are. Can anybody explain why this happened, and how to fix it? Thank you! Here is the Code

import UIKit
import AVFoundation
class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()
    @IBOutlet weak var firstCardImageView: UIImageView!
    @IBOutlet weak var secondCardImageView: UIImageView!
    @IBOutlet weak var label: UILabel!


    var cardNamesArray:[String] = ["dice1","dice2","dice3","dice4","dice5","dice6"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

    @IBAction func rollaction(sender: AnyObject) {
        updateAction()
    }

    func updateAction(){
        var firstRandomNumber = Int(arc4random_uniform(5))
        var firstCardString:String = String(self.cardNamesArray[firstRandomNumber])
        var secondRandomNumber = Int(arc4random_uniform(5))
        var secondCardString:String = String(self.cardNamesArray[secondRandomNumber])

        self.firstCardImageView.image = UIImage(named: firstCardString)
        self.secondCardImageView.image = UIImage(named: secondCardString)



        var fileLocation = NSBundle.mainBundle().pathForResource("sound", ofType: ".mp3")

        var error: NSError? = nil

        player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: fileLocation!), error: &error) //Error: Cannot find an initializer for type 'AVAudioPlayer' that accepts an argument list of type '(contentsOfURL: NSURL, error: inout NSError?)'

        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil) //Error:Extra argument 'error' in call
        AVAudioSession.sharedInstance().setActive(true, error: nil) //Error:Extra argument 'error' in call
        player.play()


        let num = firstRandomNumber + secondRandomNumber + 2
        self.label.text = "The sum is \(num)"
    }


       override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {

        if event.subtype == UIEventSubtype.MotionShake { //Error:Method does not override any method from its superclass


            updateAction()


        }
    }


}
chiwangc
  • 3,566
  • 16
  • 26
  • 32
Jerry Xu
  • 295
  • 2
  • 19
  • 2
    Can you show the error messages? – joel goldstick Jun 11 '15 at 18:21
  • It's commented out in the code. but here are the errors. //Error: Cannot find an initializer for type 'AVAudioPlayer' that accepts an argument list of type '(contentsOfURL: NSURL, error: inout NSError?)' //Error:Extra argument 'error' in call //Error:Method does not override any method from its superclass – Jerry Xu Jun 11 '15 at 18:29
  • Have you tried using the Swift 1.2 to Swift 2 migration tool? – JAL Jun 11 '15 at 18:29
  • Yeah. But i doesn't solve this:( – Jerry Xu Jun 11 '15 at 18:30
  • do { try } catch { print("error = \\(error)") } – Leo Dabus Jun 11 '15 at 18:34
  • Just by reading the errors, it seems that `AVAudioSession.sharedInstance().setCategory` is not passing the correct arguments (either too many, or incorrect type). And here _"Method does not override any method from its superclass"_, try to remove the `override` from the function. From my experience, migrating code from Swift 1.x to Swift 1.2 was a headache and took us a whole day to sort out. Did I mention that the migration tool did not fix everything? But it did migrate most. I recall over 70 red issue to fix manually. – bauerMusic Jun 11 '15 at 18:37
  • do { player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: fileLocation!)) } catch { print(error) } – Leo Dabus Jun 11 '15 at 18:40
  • I tried to remove the override. Then it gives me a new error: Method 'motionEnded(_:withEvent:)' with Objective-C selector 'motionEnded:withEvent:' conflicts with method 'motionEnded(_:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector – Jerry Xu Jun 11 '15 at 18:55

1 Answers1

4

Here's your updateAction() function with Swift 2.0's do/try/catch implementation:

func updateAction(){
    var firstRandomNumber = Int(arc4random_uniform(5))
    var firstCardString:String = String(self.cardNamesArray[firstRandomNumber])
    var secondRandomNumber = Int(arc4random_uniform(5))
    var secondCardString:String = String(self.cardNamesArray[secondRandomNumber])

    self.firstCardImageView.image = UIImage(named: firstCardString)
    self.secondCardImageView.image = UIImage(named: secondCardString)

    let fileLocation = NSBundle.mainBundle().pathForResource("sound", ofType: ".mp3")

    do {
        player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: fileLocation!))

        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch {
        print("Something bad happened. Try catching specific errors to narrow things down")
    }

    player.play()

    let num = firstRandomNumber + secondRandomNumber + 2
    self.label.text = "The sum is \(num)"
}
Sidetalker
  • 698
  • 4
  • 14