2015-05-04 19 views
1

При нажатии на кнопку (который подключен к @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(..).., но вместо функции Плюс первый работает нормально.

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

+0

Какая линия вашего IBAction рушится? –

+0

Инициализация класса AppDelegate в AppDelegate.Swift – Koka

+0

Это не имеет смысла. У делегата приложения не должно быть методов IBAction. У делегата приложения нет связанных с ним представлений. Если у вас возникли сбои в делетете приложения, почему вы отправили кучу кода из класса 'ViewController'? –

ответ

3

Наконец-то я нашел ответ! Я удалил IBActions, которые ранее были подключены с Main.storyboard в: View Controller Scene -> Показать инспектор подключений

0

Является ли «resultLabel» контрольным надписью? Если это просто переменная (String), то это нормально (за исключением того, что вы используете чрезвычайно плохой метод именования), иначе разумный взгляд на каждое место в вашей программе, которое вы используете «resultLabel».

+0

«resultLabel» - это строковая переменная, и я использую ее для редактирования метки («resultField»); Я не уверен, что проблема, связанная с «resultLabel» – Koka

+0

, попробуйте это: length = "\\ (numArray.count)" - вы пытаетесь назначить Int строке. – GLefavi

+0

"length" is Int и как его можно назначить String: "\ (numArray.count)" ..? – Koka

Смежные вопросы