简体   繁体   中英

SIGABRT error in swift 2

I recently updated my app to Swift 2, and had one error, which I solved thanks to you guys. And now, I have a second error, which is at runtime, when I press the one button to play a sound. It is a signal SIGABRT error.

Here is the error message I get in the debug console:

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

Also, this is the part of the code where it throws this error, in the second line, where the class is declared:

 import UIKit

@UIApplicationMain
-> class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

What is happening here? What am I missing / misnaming? What do I need to change in my code to get ot running again. This app is ridiculously simple, and worked for months under the last version of Swift. Why is it now giving me errors?

Thank you for your help.

Here is the code for my ViewController.swift file:

import UIKit

import AVFoundation import CoreMotion

class 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
// }

} }

"unrecognized selector sent to instance" means that there is a mismatch between the action-name in "addTarget()" and the name of the function you want to call. Probably something with the parameters of the function.. It's hard to say without seeing any code.

action: Selector("playMySound:")

would expect to find a function:

func playMySound(sender: AnyObject?) {};

To easier track what's happening, you might add a symbolic breakpoint for all exceptions. You do that in Xcode on the "exceptions" tab (left part of the window) and when an exception is thrown, Xcode stops like usual and you might look up the stack trace. If the call is synchronous, you should easily find and see your mistake.

EDIT: Oh well, you seem to have done the user interface using a XIB file. The mistake could be, you wired the button's action in the XIB file to the view controller. If you later change the method's signature (parameter, name, etc.), UIKit can't find the method. Open the XIB and fix your error. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM