【发布时间】:2023-12-30 22:50:01
【问题描述】:
我刚刚在 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()
}
}
}
【问题讨论】:
-
你能显示错误信息吗?
-
在代码中被注释掉了。但这里有错误。 //错误:找不到接受类型为“(contentsOfURL:NSURL,错误:inout NSError?)”的参数列表的“AVAudioPlayer”类型的初始化程序//错误:调用中的额外参数“错误” //错误:方法确实不覆盖其超类中的任何方法
-
您是否尝试过使用 Swift 1.2 到 Swift 2 的迁移工具?
-
是的。但我没有解决这个问题:(
-
做 { 尝试 } 捕捉 { print("error = \(error)") }