swift无法访问IBAction中的变量内容。错误:"预期声明"

时间:2015-01-23 16:01:25

标签: swift

这个程序编译好没有错误,但是当我点击附加到IBAction的按钮检查答案时崩溃。

在applicationWillResignActive(_ :)中的

我看到“线程1:信号SIGABRT” 在我的调试区域,我看到: 'NSInvalidArgumentException',原因:' - [Fingerz.ViewController checkAnswerButton:]:无法识别的选择器发送到实例0x7f89e254a180'

有人能指出我正朝着我所缺少的方向前进吗?

import UIKit
var num = 0
class ViewController: UIViewController

{

@IBAction func fingerGenerator(sender: AnyObject)  //request random number
{
    var randomNum = Int(arc4random_uniform(6))
    num = randomNum
}


@IBOutlet weak var userGuess: UITextField! //user enters Guess here

@IBOutlet weak var userResult: UILabel!    //display if user is right or wrong


@IBAction func checkAnswer(sender: AnyObject)
{
    var userInput = userGuess.text.toInt()
    println(userInput)

  if num == userInput
    {
        userResult.text = "Great Job"
    }
  else
        {
         userResult.text = "Try again"
    }
}

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


}

1 个答案:

答案 0 :(得分:0)

您发布的代码需要一个结束括号来关闭该类。

import UIKit

// You requested it to make this variable a global variable
var num = 0

class ViewController: UIViewController {

@IBAction func fingerGenerator(sendor: AnyObject)
{
    var randomNum = Int(arc4random_uniform(6))
    num = randomNum
    println(num)
}
}