ฉันเพิ่งอัปเดตแอปเป็น Swift 2 และมีข้อผิดพลาดหนึ่งข้อ ซึ่งฉันแก้ไขได้ต้องขอบคุณพวกคุณ และตอนนี้ ฉันมีข้อผิดพลาดประการที่สอง ซึ่งอยู่ที่รันไทม์ เมื่อฉันกดปุ่มเดียวเพื่อเล่นเสียง มันเป็นข้อผิดพลาดของสัญญาณ SIGABRT
นี่คือข้อความแสดงข้อผิดพลาดที่ฉันได้รับในคอนโซลดีบัก:
2016-01-25 09:16:09.019 WarningShot1.0.0[291:19030] -[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30
2016-01-25 09:16:09.021 WarningShot1.0.0[291:19030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30'
*** First throw call stack:
(0x182835900 0x181ea3f80 0x18283c61c 0x1828395b8 0x18273d68c 0x18755fe50 0x18755fdcc 0x187547a88 0x18755f6e4 0x18755f314 0x187557e30 0x1875284cc 0x187526794 0x1827ecefc 0x1827ec990 0x1827ea690 0x182719680 0x183c28088 0x187590d90 0x10005e2e0 0x1822ba8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
นอกจากนี้ นี่เป็นส่วนหนึ่งของโค้ดที่ส่งข้อผิดพลาดนี้ในบรรทัดที่สองซึ่งมีการประกาศคลาส:
import UIKit
@UIApplicationMain
-> class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
เกิดอะไรขึ้นที่นี่? ฉันขาดหายไป / ตั้งชื่อผิดอะไร? ฉันจะต้องเปลี่ยนแปลงอะไรบ้างในรหัสของฉันเพื่อให้ทำงานได้อีกครั้ง แอพนี้เรียบง่ายอย่างน่าขันและใช้งานได้นานหลายเดือนภายใต้ Swift เวอร์ชันล่าสุด ทำไมตอนนี้ถึงทำให้ฉันมีข้อผิดพลาด?
ขอขอบคุณสำหรับความช่วยเหลือของคุณ.
นี่คือรหัสสำหรับไฟล์ ViewController.swift ของฉัน:
import UIKit
นำเข้า AVFoundation นำเข้า CoreMotion
คลาส ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
var mySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
do {
try myPlayer = AVAudioPlayer(contentsOfURL: mySound, fileTypeHint: nil)
myPlayer.prepareToPlay()
// myPlayer.volume = 1.0 // < for setting initial volume, still not perfected.
} catch {
// handle error
}
var motionManager = CMMotionManager()
var currentMaxAccelY : Double = 0.0
func viewDidLoad() {
super.viewDidLoad()
initYourSound()
// Do any additional setup after loading the view, typically from a nib.
//set motion manager properties
motionManager.accelerometerUpdateInterval = 0.17
//start recording data
// motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {
// (accelerometerData: CMAccelerometerData!,error:NSError!) -> Void in
// self.outputAccelerationData(accelerometerData.acceleration)
// if(error != nil) {
// print("\(error)")
// }
// })
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: {
(accelerometerData,error) in outputAccelerationData(accelerometerData!.acceleration)
if(error != nil) {
print("\(error)", terminator: "")
}
})
}
//func outputAccelerationData(acceleration : CMAcceleration){
// accY?.text = "\(acceleration.y).2fg"
//if fabs(acceleration.y) > fabs(currentMaxAccelY)
//{
// currentMaxAccelY = acceleration.y
//}
// maxAccY?.text = "\(currentMaxAccelY) .2f"
//}
func outputAccelerationData(acceleration : CMAcceleration){
if fabs(acceleration.y) >= 1.25 {
myPlayer.play()
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playMySound(sender: AnyObject) {
myPlayer.play()
}
// self.resetMaxValues()
// @IBOutlet var accY: UILabel!
// @IBOutlet var maxAccY: UILabel!
// @IBAction func resetMaxValues() {
// currentMaxAccelY = 0
// }
} }
ViewController
เมธอดplayMySound:
หายไป - person vadian   schedule 25.01.2016initYourSound()
- person vadian   schedule 25.01.2016