SWIFT 单击按钮应用程序崩溃

SWIFT clicking the button app crashes

当我点击按钮(连接到 @IBAction fun Plus(..)..) 时,我的应用程序崩溃并显示:

Thread 1: signal SIGABRT in AppDelegate.Swift file. 

Xcode 是这样说的:

`2015-05-05 00:43:11.165 koka[14911:632850] -[koka.ViewController mainButton:]: unrecognized selector sent to instance 0x7fa4205786b0
2015-05-05 00:43:11.192 koka[14911:632850] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[koka.ViewController mainButton:]: unrecognized selector sent to instance 0x7fa4205786b0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108615a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a16dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010861cd1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001085749dc ___forwarding___ + 988
    4   CoreFoundation                      0x0000000108574578 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000108ea9a22 -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x0000000108fb0e50 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x0000000108fb021f -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x0000000108eefb68 -[UIWindow _sendTouchesForEvent:] + 735
    9   UIKit                               0x0000000108ef0493 -[UIWindow sendEvent:] + 683
    10  UIKit                               0x0000000108ebcfb1 -[UIApplication sendEvent:] + 246
    11  UIKit                               0x0000000108eca227 _UIApplicationHandleEventFromQueueEvent + 17700
    12  UIKit                               0x0000000108ea523c _UIApplicationHandleEventQueue + 2066
    13  CoreFoundation                      0x000000010854ac91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x0000000108540b5d __CFRunLoopDoSources0 + 269
    15  CoreFoundation                      0x0000000108540194 __CFRunLoopRun + 868
    16  CoreFoundation                      0x000000010853fbc6 CFRunLoopRunSpecific + 470
    17  GraphicsServices                    0x000000010c71ea58 GSEventRunModal + 161
    18  UIKit                               0x0000000108ea8580 UIApplicationMain + 1282
    19  koka                                0x000000010842fa6e top_level_code + 78
    20  koka                                0x000000010842faaa main + 42
    21  libdyld.dylib                       0x000000010a949145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

这是我的代码。有很多不必要的信息,但为了确保,我已经发布了所有代码。您可以看到 @IBAction func Enter(..).. 的主体类似于 @IBAction func Plus(..).. 的主体,但不是第一个 Plus 函数,它工作正常。

import UIKit

class ViewController: UIViewController {

    var num = ""
    var typedNum: [Int] = []
    var numArray: [[Int]] = []
    var numSumArray: [[Int]] = []
    var plusFlag = false
    var minusFlag = false
    var resultLabel = "Res: "
    var length: Int = Int()

    @IBOutlet weak var resultField: UILabel!

    @IBAction func Reset(sender: UIButton!) {
        resultLabel = "Res: "
        resultField.text = resultLabel
        num = ""
        plusFlag = false
        minusFlag = false
        numArray = []
        numSumArray = []
    }

    @IBAction func Enter(sender: UIButton!) {
        resultLabel += " = "
        numArray = getNum(num)
        length = numArray.count
        if (length == 1){
            resultLabel += num
        }
        resultField.text = resultLabel
        if (length > 1){
            if plusFlag{
                numSumArray[length-1] = SumOfTwo(numArray[length-2], second: numArray[length-1])
                for i in numSumArray{
                    resultLabel += "\(i)"
                }
            }
            if minusFlag{
            }
        }

        //reset
        resultLabel = "Res: "
        num = ""
        plusFlag = false
        minusFlag = false
        numArray = []
        numSumArray = []
    }

    @IBAction func Minus(sender: UIButton!) {

    }

    @IBAction func Plus(sender: UIButton!) {
        numArray = getNum(num)
        length = numArray.count
        if (length > 1){
            if plusFlag{
                numSumArray[length-1] = SumOfTwo(numArray[length-2], second: numArray[length-1])
                numArray[length-1] = numSumArray[length-1]
                plusFlag = false
            }
            if minusFlag{
            }
        }
        plusFlag = true
        resultLabel += " + "
        resultField.text = resultLabel
        num = ""
    }

    @IBAction func Null(sender: UIButton!) {
        num += "0"
        resultLabel += "0"
        resultField.text = resultLabel
    }

    @IBAction func One(sender: UIButton!) {
        num += "1"
        resultLabel += "1"
        resultField.text = resultLabel
    }

    @IBAction func Two(sender: UIButton!) {
        num += "2"
        resultLabel += "2"
        resultField.text = resultLabel
    }

    @IBAction func Three(sender: UIButton!) {
        num += "3"
        resultLabel += "3"
        resultField.text = resultLabel
    }

    @IBAction func Four(sender: UIButton!) {
        num += "4"
        resultLabel += "4"
        resultField.text = resultLabel
    }

    @IBAction func Five(sender: UIButton!) {
        num += "5"
        resultLabel += "5"
        resultField.text = resultLabel
    }

    @IBAction func Six(sender: UIButton!) {
        num += "6"
        resultLabel += "6"
        resultField.text = resultLabel
    }

    @IBAction func Seven(sender: UIButton!) {
        num += "7"
        resultLabel += "7"
        resultField.text = resultLabel
    }

    @IBAction func Eight(sender: UIButton!) {
        num += "8"
        resultLabel += "8"
        resultField.text = resultLabel
    }

    @IBAction func Nine(sender: UIButton!) {
        num += "9"
        resultLabel += "9"
        resultField.text = resultLabel
    }


    func getNum(num: String) -> [[Int]]{
        var i = 0
        for Character in num{
            let characterString = String(count: 1, repeatedValue: Character)
            typedNum.append(characterString.toInt()!)
            i++
        }
        numArray.append(typedNum)
        return numArray
    }

    func SumOfTwo(first: [Int], second: [Int]) -> [Int]{
        var perenos = 0
        var newValue: Int
        var newArray: [Int] = []
        for (index, value) in enumerate(first){
            newValue = value + second[index]
            perenos = newValue / 10
            newValue %= 10
            newArray[index] = newValue
        }
        return newArray
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        //let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        //button.addTarget(self, action: "Plus", forControlEvents: UIControlEvents.TouchUpInside)

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


}

P.S。我试图将按钮从 Main.storyboard 重新连接到 ViewController.Swift 但它没有帮助。我没改AppDelegate.Swift

"resultLabel"是标签控件吗?如果它只是一个变量(字符串)那么它就可以了(除非你使用了非常糟糕的命名技术),否则明智地查看你正在使用的程序中的每个地方 "resultLabel".

终于找到答案了!我删除了以前从 Main.storyboard 连接的 IBActions:View Controller Scene -> Show the Connections Inspector