DJ_ango25
DJ_ango25

Reputation: 33

Xcode 7 How to get Text Field inputs?

So I have been working with some swift code in xcode 7, and I am trying to make a basic login and registration page. I am trying to get the text inputs from the text fields, (Username, Password, etc), but I get an error (Value of type 'NSTextField' has no member 'text'. I have a note to mark where the error is in the code:

//  RegisterViewController.swift
//  SafeUtils
//
//  Created by DJtech on 7/26/16.
//  Copyright © 2016 DJtech. All rights reserved.
//

import Cocoa

class RegisterViewController: ViewController {

    @IBOutlet weak var UserNameTextF: NSTextField!
    @IBOutlet weak var UserPasswordTextF: NSSecureTextField!
    @IBOutlet weak var UserVerifyPassTextF: NSSecureTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

    // When Register Button Tapped
    @IBAction func RegisterBTNTPD(sender: AnyObject) {
        //These are the parts of code that have the error. Error: Value of type 'NSTextField' has no member 'text'
        let Username = UserNameTextF.text
        let Password = UserPasswordTextF.text
        let veriPass = UserVerifyPassTextF.text


        // Are there empty fields?


        // Store User Data

        // Confirm Registration Message

    }


}

All help is appreciated.

Upvotes: 0

Views: 1487

Answers (2)

Carlos Norena
Carlos Norena

Reputation: 115

here is this code that i have tried in play ground,, let me know how it goes

 var appUsers: [[String:String]] = [
[
    "userName": "Charly",
    "userPass": "123"
],
[
    "userName": "Betty",
    "userPass": "345"
],
[
    "userName": "Lenny",
    "userPass": "567"
],
[
    "userName": "Karl",
    "userPass": "789"
],
[
    "userName": "Homer",
    "userPass": "012"
]
]

    var loginName = "Betty"
    var loginPass = "345"

    var logginResult = ""

    for loggingUser in appUsers {

   if loginName == loggingUser["userName"] {

    if loginPass == loggingUser["userPass"] {

        let result = "ok"

        logginResult = result

    }
  }
}

if logginResult == "ok" {

  print("You are logged")

   // Do your logging stuff here

} else {

   print("Logging error")

   //Dou your return stuff here

}

Upvotes: -1

Christian
Christian

Reputation: 22343

As already mentioned in the comments, you can't use text to get the value of the NSTextField. Just check the NSControl documentation, which the NSTextField inherits from. Because of that you can use stringValue:

let Username = UserNameTextF.stringValue
let Password = UserPasswordTextF.stringValue
let veriPass = UserVerifyPassTextF.stringValue

Also you don't have to use ; at the end of each line in swift.

Upvotes: 2

Related Questions