Checking if textfields are empty Swift - swift

I know there are tons of stack overflow pages out there that explain how to do this but everytime I take the code from here and put it in i get the same error and that error is value of "string?" has no member "text" Any ideas of a solid way that will work for checking if a textfield is empty in swift?
let userEmail = userEmailTextField.text;
// Check for empty fields
if (userEmail.text.isEmpty) {
// Display alert message
return;
}

This post is given a good answer (it's a pity it has no "accepted" mark). Use (self.field.text?.isEmpty ?? true).
Assume your textField is declared as:
#IBOutlet weak var textField: UITextField!
You can check its emptiness with:
if textField.text?.isEmpty ?? true {
print("textField is empty")
} else {
print("textField has some text")
}
To use the variables in your edited post:
let userEmail = userEmailTextField.text;
// Check for empty fields
if userEmail?.isEmpty ?? true {
// Display alert message
return;
}
or:
// Check for empty fields
if userEmailTextField.text?.isEmpty ?? true {
// Display alert message
return;
}

The text property is an optional. So it can contains a String or nil.
If you want to treat nil as an empty String then just write
let isEmpty = (textField.text ?? "").isEmpty

Alternatively you can also use:
Swift 3:
if (textField.text.characters.count > 0) {
print("text field not empty")
} else {
print("text field empty")
}
Swift 4.x and above:
if (textField.text.count > 0) {
print("text field not empty")
} else {
print("text field empty")
}

Give you an example picture and cover code.
#IBAction func save(_ sender: Any) {
print("Saving...")
//CHECK MANDATORY FIELDS
checkMandatoryFields()
}
private func checkMandatoryFields(){
//CHECK EMPTY FIELDS
if let type = typeOutle.text, let name = nameOutlet.text, let address = addressOutlet.text, type.isEmpty || name.isEmpty || address.isEmpty {
print("Mandatory fields are: ")
errorDisplay(error: "Mandatory fields are: Type, Name, Address.")
return
}
//CHECK SPACE ONLY FIELDS
}

Here's the correct answer for this.
textField.text = ""
if (textField.text.isEmpty) {
print("Ooops, it's empty")
}

It was this check that helped me since it was necessary for me to send a request to the API, and it was necessary to send nill instead of "" if the textField is without text.
textField.text!.count > 0 ? textField.text : nil
Alternatively, you can check this way (but this option did not fit me):
if textField.text != nil {
} else {
}

Related

How to revert the "clearsOnBeginEditing(true) effect" (remove older value at beging editing) on textField if it is left blank?

I have a function firing on two observers - keyboardWillShowNotification and KeyboardWillHideNotification, and what I basically do is resetting a text field while editing, emulating the effect of clearsOnBeginEditing. However, if the user left the field blank I want it to return the original value, before editing. Here's my experimental code:
#objc func keyboardWillChange(notification: Notification) {
if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillHideNotification {
if textField.isEditing {
let originalValue: String!
originalValue = textField.text!
textField.text = ""
print("content while editing is \(originalValue)")
if notification.name == UIResponder.keyboardWillHideNotification {
print("this is content on end editing \(originalValue)")
if textField!.isEmpty == true {
print("this is nill")
textField.text! = originalValue
} else {
textField.text = textField.text
print("found a value")
}
}
}
}
What's the right way to do it?
You do not need to implement keyboard delegates for this. This is easily acheivable using the UITextFieldDelegate methods textFieldDidBeginEditing and textFieldDidEndEditing.
var orignalValue: String = ""
func textFieldDidBeginEditing(_ textField: UITextField) {
originalValue = textField.text
textField.text = ""
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
if textField.text?.isEmpty {
textField.text = orignalValue
}
}
Note: Your view controller would need to conform to UITextFieldDelegate and your text fields should have their delegates set to self.

Checking if a string contains a text in a textfield

How can I check if a String (for example "apple") contains text that I typed in a UITextField (for example "p" or "pp").
If the String contains the UITextField's text, I want to print a message - for example: "apple contains pp".
You can achieve that like so
class youClass: NSObject {
var yourTextFieldName = UITextField()
func someMethod() {
var apple = "apple"
if apple.containsString(self.yourTextfieldName.text!) {
print("apple contains \(self.yourTextfieldName.text!)")
}
}
}
You could extend String:
extension String {
#discardableResult
func containsText(of textField: UITextField) -> Bool {
// Precondition
guard let text = textField.text else { return false }
let isContained = self.contains(text)
if isContained { print("\(self) contains \(text)") }
return isContained
}
}
Instead of just printing a result, it also returns a Bool indicating whether or not the textField's text was contained in the String. The #discardableResult attribute allows you to ignore the return value if you want to though, without generating a compiler warning.
You could also take a reversed approach, by extending UITextField:
extension UITextField {
#discardableResult
func textIsContained(in target: String) -> Bool {
// Precondition
guard let text = self.text else { return false }
let isContained = target.contains(text)
if isContained { print("\(target) contains \(text)") }
return isContained
}
}
You would use these methods as follows:
// Your `UITextField`
let textField = UITextField()
textField.text = "pp"
// String extension:
"apple".containsText(of: textField) // returns `true` and prints "apple contains pp"
// UITextField extension:
textField.textIsContained(in: "apple") // returns `true` and prints "apple contains pp"

Login Item - cocoa

Is there a way to check if the login item already exists (with bundleIdentifier of the app?) I want to be able to see if there is a login item and if it is enable.
I was trying to check my checkbox in applicationDidFinishLuanching when the login item is enabled using this:
if (SMLoginItemSetEnabled(("bundleIDOfMyApp" as CFStringRef), true)) {
self.startAtLoginButton.state = 1
} else {
self.startAtLoginButton.state = 0
}
It does its thing, but it also launches my helper application.
Another thing is this:
#IBAction func startAtLoginButtonChecked(sender: NSButton) {
var enabled = false
if sender.state == 0 { enabled = false }
if sender.state == 1 { enabled = true }
if !SMLoginItemSetEnabled(("bundleIDOfMyApp" as CFStringRef), enabled) {
print("Login was not successful")
}
}
As far as I am concerned this is the way you implement checkbox to enable/disable login item.
What it does in my app is every time I check the box it launches the helper app (which launches my app again).
Although the method SMCopyAllJobDictionaries() is deprecated this is the usual way to check if the job is enabled, SMLoginItemSetEnabled is only be used to set the value
import ServiceManagement
let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as NSArray as! [[String:AnyObject]]
let label = "bundleIDOfMyApp"
let jobEnabled = jobDicts.filter { $0["Label"] as! String == label }.isEmpty == false
The double casting is needed to cast CFArray to NSArray and then to Array<String,AnyObject>
Also usually the checkbox is bound to a property via KVC. The lines above are the getter and SMLoginItemSetEnabled is the setter for example
let helperBundleIdentifier = "bundleIDOfMyApp"
#available(OSX, deprecated=10.10) // this line suppresses the 'deprecated' warning
dynamic var startAtLogin : Bool {
get {
guard let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as NSArray as? [[String:AnyObject]] else { return false }
return jobDicts.filter { $0["Label"] as! String == helperBundleIdentifier }.isEmpty == false
} set {
if !SMLoginItemSetEnabled(helperBundleIdentifier, newValue) {
print("SMLoginItemSetEnabled failed.")
}
}
}
Swift 3:
#available(OSX, deprecated: 10.10)
dynamic var startAtLogin : Bool {
get {
guard let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as? [[String:Any]] else { return false }
return jobDicts.first(where: { $0["Label"] as! String == helperBundleIdentifier }) != nil
} set {
if !SMLoginItemSetEnabled(helperBundleIdentifier as CFString, newValue) {
print("SMLoginItemSetEnabled failed.")
}
}
}
Side note: A launchd job requires the key Label so it's 100% safe to unwrap the optional in the filter function.

Check if Swift text field contains non-whitespace

This is an extension, not a duplicate, of How to check if a text field is empty or not in swift
The given answer,
#IBAction func Button(sender: AnyObject) {
if textField1.text != "" {
// textfield 1
}
}
does not work for me, i.e., the if-loop is triggered even when nothing is entered in the text field. (I have modified it from the original because I'm looking to trigger the code only when the field contains text).
The second answer
#IBAction func Button(sender: AnyObject) {
if !textField1.text.isEmpty{
}
}
comes much closer, but it accepts strings like " " as not empty. I could build something myself, but is there a function that will check if a string contains something other than whitespace?
This answer was last revised for Swift 5.2 and iOS 13.5 SDK.
You can trim whitespace characters from your string and check if it's empty:
if !textField1.text.trimmingCharacters(in: .whitespaces).isEmpty {
// string contains non-whitespace characters
}
You can also use .whitespacesAndNewlines to remove newline characters too.
Below is the extension I wrote that works nicely, especially for those that come from a .NET background:
extension String {
func isEmptyOrWhitespace() -> Bool {
if(self.isEmpty) {
return true
}
return (self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) == "")
}
}
Swift 4.2
Extension for String is empty or whitespace
extension String {
func isEmptyOrWhitespace() -> Bool {
// Check empty string
if self.isEmpty {
return true
}
// Trim and check empty string
return (self.trimmingCharacters(in: .whitespaces) == "")
}
}
The original poster's code is checking text on a textfield which is optional. So he will need some code to check optional strings. So let's create a function to handle that too:
Extension for Optional String is nil, empty or whitespace
extension Optional where Wrapped == String {
func isEmptyOrWhitespace() -> Bool {
// Check nil
guard let this = self else { return true }
// Check empty string
if this.isEmpty {
return true
}
// Trim and check empty string
return (this.trimmingCharacters(in: .whitespaces) == "")
}
}
akashivskyy answer in Swift 3.0:
let whitespaceSet = CharacterSet.whitespaces
if !str.trimmingCharacters(in: whitespaceSet).isEmpty {
// string contains non-whitespace characters
}
extension StringProtocol where Index == String.Index {
var isEmptyField: Bool {
return trimmingCharacters(in: .whitespaces) == ""
}
}
if yourTextField.text.isEmptyField {
// Field is empty
} else {
// Field is NOT empty
}
Answer in swift 4:
extension String {
func isEmptyOrWhitespace() -> Bool {
if(self.isEmpty) {
return true
}
return (self.trimmingCharacters(in: NSCharacterSet.whitespaces) == "")
}
}
Answer in Swift 3.0
if stringValue.trimmingCharacters(in: CharacterSet.whitespaces).isEmpty
{}
Answer in Swift 3.*, considers newlines, tabs
extension String {
var containsNonWhitespace: Bool {
return !self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
Answer with a picture in case you need a demo
// MY FUNCTIONS
private func checkMandatoryFields(){
//CHECK EMPTY OR SPACES ONLY FIELDS
if let type = typeOutle.text, let name = nameOutlet.text, let address = addressOutlet.text, type.trimmingCharacters(in: .whitespaces).isEmpty || name.trimmingCharacters(in: .whitespaces).isEmpty || address.trimmingCharacters(in: .whitespaces).isEmpty {
print("Mandatory fields are: ")
errorDisplay(error: "Mandatory fields are: Type, Name, Address.")
return
}
}
swift 4.2
#IBAction func checkSendButton(_ sender: UITextField) {
if((sender.text?.count)! > 0 && !(sender.text!.trimmingCharacters(in: .whitespaces)).isEmpty){
self.sendButton.isEnabled = true
}
else{
self.sendButton.isEnabled = false
}
}
Method
func trim() -> String {
return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
}
Use
if textField.text.trim().count == 0 {
// Do your stuff
}

How to check if a text field is empty or not in swift

I am working on the code below to check the textField1 and textField2 text fields whether there is any input in them or not.
The IF statement is not doing anything when I press the button.
#IBOutlet var textField1 : UITextField = UITextField()
#IBOutlet var textField2 : UITextField = UITextField()
#IBAction func Button(sender : AnyObject)
{
if textField1 == "" || textField2 == ""
{
//then do something
}
}
Simply comparing the textfield object to the empty string "" is not the right way to go about this. You have to compare the textfield's text property, as it is a compatible type and holds the information you are looking for.
#IBAction func Button(sender: AnyObject) {
if textField1.text == "" || textField2.text == "" {
// either textfield 1 or 2's text is empty
}
}
Swift 2.0:
Guard:
guard let text = descriptionLabel.text where !text.isEmpty else {
return
}
text.characters.count //do something if it's not empty
if:
if let text = descriptionLabel.text where !text.isEmpty
{
//do something if it's not empty
text.characters.count
}
Swift 3.0:
Guard:
guard let text = descriptionLabel.text, !text.isEmpty else {
return
}
text.characters.count //do something if it's not empty
if:
if let text = descriptionLabel.text, !text.isEmpty
{
//do something if it's not empty
text.characters.count
}
Better and more beautiful use
#IBAction func Button(sender: AnyObject) {
if textField1.text.isEmpty || textField2.text.isEmpty {
}
}
another way to check in realtime textField source :
#IBOutlet var textField1 : UITextField = UITextField()
override func viewDidLoad()
{
....
self.textField1.addTarget(self, action: Selector("yourNameFunction:"), forControlEvents: UIControlEvents.EditingChanged)
}
func yourNameFunction(sender: UITextField) {
if sender.text.isEmpty {
// textfield is empty
} else {
// text field is not empty
}
}
if let ... where ... {
Swift 3:
if let _text = theTextField.text, _text.isEmpty {
// _text is not empty here
}
Swift 2:
if let theText = theTextField.text where !theTextField.text!.isEmpty {
// theText is not empty here
}
guard ... where ... else {
You can also use the keyword guard :
Swift 3:
guard let theText = theTextField.text where theText.isEmpty else {
// theText is empty
return // or throw
}
// you can use theText outside the guard scope !
print("user wrote \(theText)")
Swift 2:
guard let theText = theTextField.text where !theTextField.text!.isEmpty else {
// the text is empty
return
}
// you can use theText outside the guard scope !
print("user wrote \(theText)")
This is particularly great for validation chains, in forms for instance. You can write a guard let for each validation and return or throw an exception if there's a critical error.
As now in swift 3 / xcode 8 text property is optional you can do it like this:
if ((textField.text ?? "").isEmpty) {
// is empty
}
or:
if (textField.text?.isEmpty ?? true) {
// is empty
}
Alternatively you could make an extenstion such as below and use it instead:
extension UITextField {
var isEmpty: Bool {
return text?.isEmpty ?? true
}
}
...
if (textField.isEmpty) {
// is empty
}
use this extension
extension String {
func isBlankOrEmpty() -> Bool {
// Check empty string
if self.isEmpty {
return true
}
// Trim and check empty string
return (self.trimmingCharacters(in: .whitespaces) == "")
}
}
like so
// Disable the Save button if the text field is empty.
let text = nameTextField.text ?? ""
saveButton.isEnabled = !text.isBlankOrEmpty()
A compact little gem for Swift 2 / Xcode 7
#IBAction func SubmitAgeButton(sender: AnyObject) {
let newAge = String(inputField.text!)
if ((textField.text?.isEmpty) != false) {
label.text = "Enter a number!"
}
else {
label.text = "Oh, you're \(newAge)"
return
}
}
Maybe i'm a little too late, but can't we check like this:
#IBAction func Button(sender: AnyObject) {
if textField1.text.utf16Count == 0 || textField2.text.utf16Count == 0 {
}
}
Okay, this might be late, but in Xcode 8 I have a solution:
if(textbox.stringValue.isEmpty) {
// some code
} else {
//some code
}
I used UIKeyInput's built in feature hasText: docs
For Swift 2.3 I had to use it as a method instead of a property (as it is referenced in the docs):
if textField1.hasText() && textField2.hasText() {
// both textfields have some text
}
Swift 4.x Solution
#IBOutlet var yourTextField: UITextField!
override func viewDidLoad() {
....
yourTextField.addTarget(self, action: #selector(actionTextFieldIsEditingChanged), for: UIControlEvents.editingChanged)
}
#objc func actionTextFieldIsEditingChanged(sender: UITextField) {
if sender.text.isEmpty {
// textfield is empty
} else {
// text field is not empty
}
}
Swift 4.2
You can use a general function for your every textField just add the following function in your base controller
// White space validation.
func checkTextFieldIsNotEmpty(text:String) -> Bool
{
if (text.trimmingCharacters(in: .whitespaces).isEmpty)
{
return false
}else{
return true
}
}
I just tried to show you the solution in a simple code
#IBAction func Button(sender : AnyObject) {
if textField1.text != "" {
// either textfield 1 is not empty then do this task
}else{
//show error here that textfield1 is empty
}
}
It's too late and its working fine in Xcode 7.3.1
if _txtfield1.text!.isEmpty || _txtfield2.text!.isEmpty {
//is empty
}
Swift 4/xcode 9
IBAction func button(_ sender: UIButton) {
if (textField1.text?.isEmpty)! || (textfield2.text?.isEmpty)!{
..............
}
}
Easy way to Check
if TextField.stringValue.isEmpty {
}