test if object is in an array of objects - ios

I know I'm overlooking something easy, but I am using
if (textField == self.nameTextField || textField == self.ageTextField || textField == ...)
when ideally I'd like to use something that allows the textField to be compared to an array of allowed (named) textField objects.
I've tried to pseudocode what I think the solution would look like a couple of times, I just don't know what I'm doing. Can anyone point me in the right direction?

Just check like:
if([textFieldArray containsObject:textField])
{
//do stuff here
}
Here textFieldArray is the array which holds all textFields.
If the textField object is in the textFieldArray, it'll return true.

Firstly add all references of UITextFields in NSArray.
Now use containsObject in NSArray to check if exists
BOOL contains = [yourArrayofTextFields containsObject:textField];
if(contains)
// contains
else
// not contains

Related

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=#"";
}
}

How to tell if a selection count has been called an even amount of times?

I need a way to tell when the deslect count is equal to 0 an even amount of times. For example, if the count ==0 once dont do anything but if equals 0 twice then call the deselect nslog function. What is the best current way to make this work?
if([self.mapView.selectedAnnotations count] == 0){
NSLog(#"DE SELECT");
}
As long as you're always going to do the same thing on odd selections and the same other thing on even selections, just use a static BOOL.
if([self.mapView.selectedAnnotations count] == 0){
static BOOL odd = YES;
if(odd) {
//do something
} else {
//do something else
}
odd = !odd
}
Since your code needs to react to the user deselecting annoationations, you probably need to implement the mapView:didDeselectAnnotationView: method and add some logic that checks to see if the selection count has dropped to zero. If it has, increment a counter.

Weird Comparison of UITextField and UILabel

How come this happens? Using the code...
- (IBAction)dismissKeyboard:(id)sender {
if(textField.text == label.text) {
NSLog(#"Correct!");
}
else {
NSLog(#"Error?");
NSLog(textField.text);
NSLog(label.text);
}
}
The statement evaulates to be false and prints...
Error?
Word
Word
The words are the same, but the if statement fails... Why is this? I have no clue why this is happening if it is printing the exact same words but the statement is comparing them to be false?
Is it something I am doing wrong in the if statement? Please help. Thank you in advance.
NSString is a class. The proper way to see if two NSString objects (or objects of any class) have the same value is to use isEqual:. In the case of NSString you can use isEqualToString:.
if ([textField.text isEqualToString:label.text]) {
}
The sign == compares the memory location. It sees if you are referring to the same object or not.
The method "isEqualToString: " should be used to compare string values inside variables.

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.

iOS Compare Value of Label

I'm using a label to display the string result of function. However I have a class variable that stores the previous result and I need to update that variable in different ways depending on different conditions. The code I wrote is
if(displayPassword.text == #"Memorable")
{
prevpass = [newPassword returnPassword];
}
else
{
prevpass = displayPassword.text;
}
However it always jumps to the else as it seems to show under debugging that displayPassword.text is always empty depsite it showing a value.
You can only use == to compare scalar values. A string is an object. You need to use the isEqual: or isEqualToString: method instead.
if([displayPassword.text isEqualToString:#"Memorable"]) {

Resources