Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

ios - Swift 2 migration problems

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


        }
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...