使用未解析的标识符“self”

时间:2016-05-12 07:03:17

标签: xcode swift macos

我正在使用Swift开发Mac OS X应用程序。

这是我的第一次,我认为它与iOS上的相同。

所以我在这段代码中得到了这个错误(标题中):

import Cocoa
import AppKit

        class LoginViewController: NSViewController {


            @IBOutlet weak var emailTextField: NSTextField!
            @IBOutlet weak var passwordTextField: NSSecureTextField!
            override func viewDidLoad() {
                super.viewDidLoad()

                // Do any additional setup after loading the view.
            }

            override func viewDidAppear() {

            }

                super.viewDidAppear()

                if NSUserDefaults.standardUserDefaults().valueForKey("uid") != nil && CURRENT_USER!.authData != nil
                {
                    self.logoutButton.hidden = false
                }

            }
            func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
                // Dispose of any resources that can be recreated.
            }


            /*
            // MARK: - Navigation

            // In a storyboard-based application, you will often want to do a little preparation before navigation
            override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                // Get the new view controller using segue.destinationViewController.
                // Pass the selected object to the new view controller.
            }
            */


            func loginAction(sender: AnyObject)
            {
                let email = self.emailTextField.text
                let password = self.passwordTextField.text
                ...
            }
    }

我需要写什么而不是自己?

1 个答案:

答案 0 :(得分:0)

使用self是正确的。尝试使用属性stringValue代替text

以下代码对我有用:

@IBOutlet weak var emailTextField: NSTextField!

@IBAction func sendTapped(sender: AnyObject) {
  let email = self.emailTextField.stringValue
  print(email)
}