Ensure UITextField isn't ONLY full of spaces [duplicate] - ios

This question already has answers here:
How to know if a UITextField in iOS has blank spaces
(9 answers)
Closed 7 years ago.
I have an iOS app which has a UITextField where the user can enter in a name. I want to make sure that they user doesn't just enter a bunch of spaces and then clicks save.
Accepted as valid:
blah blah blah with spaces
Not accepted as valid
only spaces
In other words I don't care if the user adds a name with some blank spaces, what I don't want is if the user presses the space bar a few times and then saves the name.
I have thought of performing this check:
if ((nameField == nil) || ([nameField isEqualToString:#" "])) {
// Not valid....
}
else {
// Valid...
}
You can see that in my if statement, I am checking for #" ". That checks to make sure the user hasn't just enter a empty string with a space. But thats only for one space, if the user enters space twice and saves it, then the if statement still thinks its valid. So how do I scale this up?
Thanks for your time, Dan.

You can trim all white spaces like this
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
and if it was all spaces then text will be empty string and you can check
if (text.length == 0)
{
// not acceptable
}
else
{
//valid
}

Related

Parsing a UITextDocumentProxy in Swift 3

I'm currently developing a custom keyboard app and am having trouble parsing what the keyboard has outputted onto the text document proxy. How does one go about this? I feel like I'm losing my mind. Currently I'm looping:
for letter in (proxy.documentContextBeforeInput?.characters)!
However, this is only getting the text on the line the cursor is currently on, before the cursor, such that if my textDocumentProxy contains:
Some text above
Some text below (cursor position)
My loop only iterates throught the "Some text below" portion.
Is there any way to loop through the entirety of a UITextDocumentProxy? Thank you.
documentContextBeforeInput, as its name mentions, only returns the text before the input. To get the full text string, you should do something like:
let entireText = (proxy.documentContextBeforeInput ?? "") + (proxy.documentContextAfterInput ?? "")
if let chars = entireText.characters {
for letter in chars {
//DO something useful
}
}

How to search word in Array when clicking on button in Iphone

I have database with 50,000 english words,on storyboard i have 26 buttons with alphabetic text(A,B,C.....).
So,when i click on any button first,then words that start from this letter will store in MutableArray and i want to search word in this array.If word is in array then message shows that you found word....
Problem is that Let I want to search word HelloAll,then pressing button with titletext 'H' store all words with starting letter 'H' in MutableArray from Databsase and searching starts in array on clicking next button,but when my text is on point HELL alertview shows that YOU FOUND WORD because HELL word is also in this Mutablearray.
Im going to assume your Array is an string array:
NSString *word = #"HelloAll";
for(NSString *currentWord in Array)
{
if([currentWord isEqualToString:word])
{
NSLog(#"Word found");
}
}
with the isEqualToString method you check if the current string is exactly the same as the one you're comparing.

Method To Delete 2nd Label, Then First Label, Not Functioning Correctly

I know I am missing something obvious, but I just cannot see it. This method is meant to compare the text of a label to the text of a text box, and then delete the text. So if the 1st label reads "Puppy" and the 2nd label reads "Kittens," and the text box says "Kittens," the method should delete the text of the 2nd label and leave the 1st label's text. If the 2nd label is blank, then the method should delete the text of the 1st label.
But no matter how I mess with the method, either it deletes the 2nd label but not the 1st, deletes both of them, or deletes neither of them. Here's what I've tried
(lblFabric1 is the 1st label, lblFabric2 is the 2nd label, txtType is the text box):
-(IBAction)btnDelete:(id)sender
{
if ((self.lblFabric2.text== self.txtType.text))
{
self.lblFabric2.text = #"";
}
else if ((self.lblFabric2.text != self.txtType.text))
{
self.lblFabric1.text=#"";
}
}
It deletes the 2nd label, but not the 1st label. If I try to set the "Else if" to:
else if ((self.lblFabric2.text==#""))
it gives me an error (""Direct comparison of a string literal has undefined behavior.") Am I just going about this the wrong way? What am I missing?
You should not use == or != for string comparison in Objective C. You need to use the isEqualToString or isEqual method.
if (([self.lblFabric2.text isEqualToString:self.txtType.text]))
When you use == or != you are comparing the pointers where the strings are stored.
To compare NSStrings use:
if ([myString1 isEqualToString:myString2])
Documentation
Compairing String literals using == is not guaranteed to behave as you might expect. Use isEqual: or isEqualToString: instead.
See http://nshipster.com/equality/.
When you are comparing NSStrings with == what you are actually comparing are two memory addresses and that is not what you are really intended for. You want to compare the values of two strings what == operator is not suitable for and thus you are getting the warning
Direct comparison of a string literal has undefined behavior.
To compare the values of NSStrings you should use isEqualToString: method. You could have also use isEqual: method derived from NSObject class but that is not suitable for Unicode comparison. So isEqualToString: is always the safest bet.
After using isEqualToString: your code should look something like:
-(IBAction)btnDelete:(id)sender
{
if ([self.lblFabric2.text isEqualToString:self.txtType.text])
{
self.lblFabric2.text = #"";
}
else
{
self.lblFabric1.text=#"";
}
}

iPhone string comparison strange issue in iOS 6

I used to compare the text entered in a txt field as below,
if ([myTextfield.text isEqualToString: #""])
{
// success code
}
else
{
// my code
}
in iOS 7 this check works perfectly. If nothing entered in to the text field, condition is success. But in iOS 6 if the field is blank, this condition always false and moves to the else block.
So i did like,
if (myTextfield.text.length == 0)
{
// success code
}
else
{
// my code
}
This works fine, and I just wanted to know what is wrong in my first method.
If myTextfield.text is nil, [myTextfield.text isEqualToString: #""] will fail because messaging nil returns nil (or 0, NO as appropriate).
In the second case you have, you are checking for == 0, so even if the string is nil you will still get a positive result.
In iOS7, untouched UITextFields return nil, whereas in previous iOS versions they return an empty string. Touched UITextFields in both cases should return an empty string.
(Did you ask the question in reverse mistaking iOS6 w 7? If not, I'd also make sure the text field is hooked up properly since a touched iOS7 text field could return an empty string while an unsynthesized iOS6 text field could return NULL since iOS6 is especially strict in this way.)

iOS NSString from UITextField - checking value

In my app I have a dialog box where the user enters some information into a UITextField. Before moving on I need to check that UITextField.text value equals something (the value is captured when the user clicks a submit button). However, my conditional (see below) keeps failing.
if (userAssignedName || userAssignedName.length > 0 || ![userAssignedName isEqualToString:#""]) {
//do something
} else {
[alertManager showAlert:#"You must enter a name for this OR"];
}
When I console log userAssignedName right before the conditional I get what I expect, nothing.
How can I check to make sure the string has value?
Well the solution is simple. Either of the 3 values returns a YES/true value.
![userAssignedName isEqualToString:#""]
This checks if your string is equal to an empty string. (which could be ur default of the textfield?) This is good.
userAssignedName.length > 0
This checks the chars in your string. This is also fine except you don't need it. It's mostly used for keeping a max number of characters in a textfield.
userAssignedName
This checks if the actual variable is instantiated. And doesn't and shouldn't involve the userinput in any way. If you remove this your check won't fail.
Solution:
if (/*userAssignedName.length > 0 ||*/ ![userAssignedName isEqualToString:#""])
{
//do something
}
else
{
[alertManager showAlert:#"You must enter a name for this OR"];
}
This should do the trick. And whether or not u use the comment or not is up to you.
Replace this:
if (userAssignedName || userAssignedName.length > 0 || ![userAssignedName isEqualToString:#""]) {
with:
if (userAssignedName.length > 0) {
You don't want to use:
if (![userAssignedName isEqualToString:#""]) {
because if userAssignedName is nil, this if statement will succeed when you want it to fail.

Resources