Get text from an object of NSTextField ?

From the following code, I want to determine if the text is empty (contains nothing), but I can't access its value. The Validator class is to receive various objects, determine the object's type, and then check it it contains valid text. If not, return a message about the problem. The code follows:


import Foundation

import Cocoa

class Validator {

let alertTitle = "Entry Error"

let alertMsgA = " is a REQUIRED FIELD, please supply it!"

let alertMsgB = " is a an invalid date, please correct it."

let alertMsgC = " is a an invalid phone number, please correct it."

func isPresent(sender: Any) -> Bool {

if sender is NSTextField {

if sender.isEmpty { /

return false

}

}

return true

}

}

Answered by Claude31 in 212046022

your text field probably has an IBOutlet such as :


@IBOutlet var myTextField : UITextField!


then, you can test through all possible fields with :


if sender === myTextField { ....


or you can set a tag in IB to the textField and test

switch sender.tag {

case 1 : doForFirstField

case 2 : ...

}

What do you mean by " I can't access its value" ?


Is it a compiler error ? an execution error ? or you dont get the correct return value ? or something else ?


In your func isPresent, if the sender is not a textField you return true. Is that what you want ?:


func isPresent(sender: Any) -> Bool {
    if sender is NSTextField {
        if sender.isEmpty {  
            return false
        }
    }
        return true
    }
}


You must test the stringValue of the sender, not the sender itself which is not empty


if sender.stringValue == nil || sender.stringValue == "" {


or


if sender.stringValue.isEmpty {

Claude, thanks for your response! The problems was a compiler error which was fixed with the following change, but this is NOT what I need. This function MUST first determine the sender's Type, and then test it for empty. (This function is to test any object represented by sender, and look at its stringVslue.


func isPresent(sender: NSTextField) -> Bool {

if sender.stringValue.isEmpty {

return false

}

return true

}

}

Regarding your other questions: The purpose of this function is to test if the the sender (unknown object) contains input. To do this it needs to know the sender's Type first. MY NEXT QUESTION IS: How can I find the IB name of the sender, so I can tell the user what input field contains the error? With this knowledge, I want to return or keep focus on the field in error; how do I do this?

Accepted Answer

your text field probably has an IBOutlet such as :


@IBOutlet var myTextField : UITextField!


then, you can test through all possible fields with :


if sender === myTextField { ....


or you can set a tag in IB to the textField and test

switch sender.tag {

case 1 : doForFirstField

case 2 : ...

}

Claude, thanks! Without even testing this, I'm sure it answers my questions. I keep forgetting about @IBOutlets/Actions. About a year ago, I switched from Microsoft to Apple, because I think Apple is the best product out there. However, I will give Microsoft credit for a much better programming environment: Visual Studio versus xCode. It is just as flexible and function loaded, but much simpler and direct in anything you want to accomplish!

Get text from an object of NSTextField ?
 
 
Q