Swift 2迁移问题

时间:2015-06-11 18:18:20

标签: ios xcode swift ios9

我刚刚在Xcode 7 beta中打开了我的旧项目。该代码在Xcode 6中运行得非常好,但现在它显示了很多错误。我不知道那些是什么。任何人都可以解释为什么会这样,以及如何解决它?谢谢!这是代码

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


        }
    }


}

1 个答案:

答案 0 :(得分:4)

这是使用Swift 2.0的do / try / catch实现的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)

    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)"
}