I'm trying to do a check to see if there are any sub-strings in a UITextView. Currently, this is how I go about doing that:
if ([self.textView.text rangeOfString:#"string"].location == NSNotFound)
{
NSLog(#"self.textView.text contains string");
}
Unfortunately, when I try to run this code I don't get a response, indicating that the test failed, as there is in fact the word: string used in the UITextView. How would I go about checking if there are any sub-strings in a UITextView?
Just change != to ==! The if statement is telling you that the instance of string not found. Use this statement instead:
if ([self.textView.text rangeOfString:#"string"].location != NSNotFound)
{
NSLog(#"self.textView.text contains string");
}
Same thing you can implement like this below as well. If you do not want to change the equal operator condition:-
if (![self.textView.text rangeOfString:#"string"].location == NSNotFound)
{
NSLog(#"self.textView.text contains string");
}
Related
In this Hangman game how can I give the condition to check if input !=Char? It says that Kotlin: Operator '!=' cannot be applied to 'String?' and 'Char.Companion'
How can I solve this issue?
while (letters != correctGuesses) {
printExploredWord(word, correctGuesses)
println("\n#Wrong guesses: $fails\n\n")
print("Guess letter:")
val input = readLine()
if (input == null) {
continue
} else if (input.length != 1) {
println("Please enter one letter")
continue
} else if (input != Char) {
println("Please enter a character")
}
if (word.toLowerCase().contains(input.toLowerCase())) {
correctGuesses.add(input[0].toLowerCase())
} else {
++fails
}
Sounds from your comments that what you want to check is if the input String? has a single alphabetic character. You need to be precise with your terminology. Numbers and punctuation are also made up of characters. Char is a class representing any element of a String, so it doesn't make sense to be asking if something in a String is a Char because the answer is true no matter what.
The question you need to be asking is whether the first character in the given String is a letter. There's a function for that: Char.isLetter(). And since we're checking the content of the first character of the String, we need to get its value with input[0] because it doesn't make sense to ask if a whole String is a letter character. A String is never a Char because these are different classes. So in your case you would use:
if (input == null) {
continue
} else if (input.length != 1) {
println("Please enter one letter")
continue
} else if (!input[0].isLetter()) {
println("Please enter a character")
continue
}
But again, the terminology is wrong here. You should be reminding the user to enter a letter, not a character.
Your input variable is a type String?, so there's no reason to check if it's a char or not. To check for instanceof in Kotlin you use the "is" operator. So to compare if your input is a Char you can do,
else if(!(input is Char)) {
//This is unnecessary though since you're already checking if the length of the input isn't 1
}
Hi there I have some code at the moment that gives me the error ("0") is not equal to ("50") - condition not applied correctly Basically I am currently using a traditional for loop within a BOOL which goes through the list of items and checks whether or not the condition can be applied, if it can be applied to an item then the BOOL will return YES. I cannot see where I am currently going wrong and need guidance. My code is shown below:
-(BOOL)conditionCanBeApplied:(NSArray *)items{
bool itemConditionCanBeApplied = NO;
for (int i = 0; i < items.count; i++)
{
id myItem = [[items valueForKeyPath:#"itemId"]objectAtIndex: i];
if ([self.applicableItems containsObject:myItem]) {
itemConditionCanBeApplied = YES;
}
}
return itemConditionCanBeApplied;
}
First, don't mix BOOL and bool, they might be very similar but they aren't the same data type. Second, always use fast enumeration if you have a choice. I am assuming in the code below that your items collection is something like an NSArray. Also, there is no reason to test with an if statement just to set a BOOL since the test is a boolean statement. (I am doing it in my example to allow for the break) Lastly, short-circuiting your logic with a break keeps the processor from doing unnecessary work once you have at least one match.
Do something like this:
- (BOOL)conditionTest
{
BOOL itemConditionCanBeApplied = NO;
for (id item in items) {
if ([self.applicableItems containsObject:item]) {
itemConditionCanBeApplied = YES;
break;
}
}
return itemConditionCanBeApplied;
}
I am trying to create a class similar to the built in NSDictionary class, but I want to add some extra functionality and make it easier to use. In the .m file I have the following piece of code:
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (![theKey isEqual:#""]) && (![theValue isEqual:#""]){
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
}
}
It gives me an "Expected Identifier" error at the start of the second portion of the if statement after the "&&". Would someone be able to help me with this?
EDIT: The original problem is fixed but now there is a new error at the end of the if statement.
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (theKey.length && theValue.length) {
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
} //<-- error here "Expected expression"
}
It should be:
if (![theKey isEqual:#""] && ![theValue isEqual:#""]) {
Though a better check for non-empty strings would be:
if (theKey.length && theValue.length) {
Your original if statement will give incorrect results if either theKey or theValue is nil. My 2nd option works in either case.
Update:
The problem with the updated code is the missing ; after the return statement.
Usually after if you should have one condition in parentheses. You're missing parentheses.
In my instance method, would like to compare a BOOL parameter with the content of a static variable, for instance:
- (NSArray*)myMethod:(NSString*)someString actualValuesOnly:(BOOL)actualValuesOnly {
static NSString *prevSsomeString;
static BOOL prevActualValuesOnly;
static NSArray *prevResults
if ([someString isEqualToString:prevSomeString] &&
([actualValuesOnly isEqual: prevActualValuesOnly])
// HOW TO COMPARE THESE TWO BOOLEANS CORRECTLY??
{ return prevResults; }// parameters have not changed, return previous results
else { } // do calculations and store parameters and results for future comparisons)
What would be the correct way to do this?
Since BOOL is a primitive (or scalar) type, and not a class, you can compare it directly with ==
if ([someString isEqualToString:prevSomeString] && actualValuesOnly == prevActualValuesOnly)
Boolean variable is compare with == sign instead of isEqual
if(Bool1 == Bool2){
// do something here}
Boolean is compare with == sign instead of isequal:
The solutions mentioned here are not the safest way to compare 2 BOOL values, because a BOOL is really just an integer, so they can contain more than just YES/NO values. The best way is to XOR them together, like detailed here: https://stackoverflow.com/a/11135879/1026573
As Matthias Bauch suggests,
Simply do the comparison using == operator i.e
if (BOOL1 == BOOL2)
{
//enter code here
}
I want to make a selection before apply one of two animations,
what I thought is: make a Point one, if my myImageView is at the Point one, then apply animationNo1, else apply animationNo2, but I got this:"used struct type value where scalar is required", at line if (myImageView.layer.position = one)
What I do? how can I fix this?
Does anyone know exactly what makes the problem happen?
CGPoint one = CGPointMake(myImageView.layer.position.x, 100);
if (myImageView.layer.position = one)
{
animationNo1
}
else
{
animationNo2
}
First of all, your if-statement will not do what you think. If you want to compare something you have to use == (ie 2 =)
and you can't compare CGPoints like this.
use
if (CGPointEqualToPoint(one, self.view.layer.position))
if (myImageView.layer.position = one) { animationNo1 }
should be
if (CGPointIsEqualToPoint(myImageView.layer.position, one)) { animationNo1 }
You used a single = meaning assignment, rather than a == for comparison. But the == wouldn't do what you wanted here anyway.
You are passing a struct (int this case position) instead of a scalar. To do what you want you need to use CGPointIsEqualToPoint:
if (CGPointEqualToPoint(one, self.view.layer.position))
Full code with corrections:
CGPoint one = CGPointMake(myImageView.layer.position.x, 100);
if (CGPointEqualToPoint(one, self.view.layer.position))
{
animationNo1
}
else
{
animationNo2
}
Also, as others have pointed out: Be careful about = vs ==. They are different. In this case you don't use == for comparison fortunately, but if you use = for other stuff it will make it true instead of checking to see if it is true.