Weird String Comparison Error - iphone

The "currentPasswordBox" is a UITextField.
if (currentPasswordBox.text == #"1234") {
NSLog(#"Correct");
}
else {
NSLog(#"Incorrect");
}
}
The Log says (when I type 1234 into the UITextField) "Incorrect"
What am I doing wrong?

== will do a pointer compare, which is not correct. You want
if ([currentPasswordBox.text isEqualToString:#"1234"]) {

Related

How to check the string doesn’t contain any letters in swift?

i have trouble during making the letter checker, my code is like this: if !containLetters(“1234h”){print(“pass”)}
my function is
func containsOnlyNum(input: String) -> Bool {
var ok = false
for chr in input {
for check in "1234567890.-"{
if chr == check{
ok = true
}
}
if ok != true{
return false
}
}
return true
}
If I check for “h” then didn’t pass, but if i check for ”1h” then it still pass! Please help me to fix this problem. I will give a big thank for anyone who helped me
The simplest way to fix the algorithm is this way:
func containsOnlyNum(input: String) -> Bool {
// check every character
for chr in input {
var isNum = false
for check in "1234567890.-"{
if chr == check {
isNum = true
// if we have found a valid one, we can break the iteration
break
}
}
if !isNum {
return false
}
}
return true
}
print(containsOnlyNum(input: "1234")) // true
print(containsOnlyNum(input: "1234h")) // false
However, then you can directly simplify it to:
func containsOnlyNum(input: String) -> Bool {
return input.allSatisfy { chr in
"1234567890.-".contains(chr)
}
}
which does exatly the same but uses allSatisfy and contains functions, which represent the logical operators ALL and EXISTS.
However, programmers normally use regular expressions for similar tasks:
func containsOnlyNum(input: String) -> Bool {
return input.range(of: "^[0-9.\\-]+$", options: .regularExpression) != nil
}
You can check that a string contains only the characters you're interested in like this:
extension String {
var containsOnlyNum: Bool {
let wanted = CharacterSet.decimalDigits
.union(CharacterSet(charactersIn: "-."))
return unicodeScalars
.allSatisfy(wanted.contains)
}
}
"-12.34".containsOnlyNum // true
"A1234".containsOnlyNum // false
But if you are interested in numbers, then this is a problem:
"-12.-34.".containsOnlyNum // true
Instead, you can just try casting the string to a double and see if it is a number or not
Double("1234") != nil // true, a number
Double("-1.234") != nil // true, a number
Double("A1234") != nil // false, not a number
Double("-12.-34.") != nil // false, not a number
Which is almost right unless you don't want this case:
Double("1234e2") != nil // true, a number
But you can use both checks if you don't want to allow that, or else if you are able to parse a Double from the input you can just do the cast.

Setting variable equal to if statement condition

I have an if statement that checks to see if an array element matches a local variable.
if pinArray.contains(where: {$0.title == restaurantName})
How would I create a variable of this element?
I attempted
let thePin = pinArray.contains(where: {$0.title == restaurantName})
but this comes with "could not cast boolean to MKAnnotation".
I also tried variations of
let pins = [pinArray.indexPath.row]
let pinn = pins(where: pin.title == restaurantName) (or close to it)
mapp.selectAnnotation(thePin as! MKAnnotation, animated: true)
to no avail. What basic step am I missing?
contains(where:) returns a Bool indicating whether a match was found or not. It does not return the matched value.
So thePin is a Bool which you then attempt to force-cast to a MKAnnotation which of course crashes.
If you want the matching value, change your code to:
if let thePin = pinArray.first(where: { $0.title == restaurantName }) {
do {
mapp.selectionAnnotation(thePin, animated: true)
} catch {
}
} else {
// no match in the array
}
No need for contains at all. No need to cast (assuming pinArray is an array of MKAnnotation).

In swift how do I see if a specific character is present in a UITextField?

Firstly, apologies - new to all this.
I am trying to take whatever text a user has inputted to a UITextField and see if it contains a certain letter, then count the number of times that letter has been entered.
So for example, if the string was "Hello" I would want it to return, as a score if like, that there are two l's. I don't want it to be case sensitive.
Thanks!
You can use try this extension:
extension String
{
func numberOfChars(char:Character) -> Int
{
var counter = 0
for chr in self
{
if chr == char
{
counter++
}
}
return counter
}
}
And then use it like:
println(textField.text.numberOfChars(Character("a")))
To check when the user has entered text in your UITextField you can use: addObserver to the UITextField and create a method using observeValueForKeyPath. In this method you can use to count the number of characters:
let someString = "Hello"
let count = Array(someString).map { $0 == "l" }.filter { $0 == true }.count
To use an extension like Dejan Skledar suggested:
extension String {
func numberOfChars(char: Character) -> Int {
return Array(someString).map { $0 == "l" }.filter { $0 == true }.count
}
}
and use it like:
println(textField.text.numberOfChars("l") // prints "3"

Get the text in string variable

I m confused with this from many days and couldn't understand how to resolve it.
I get some data from web server and assigning it to string variable.In assigning it if sometimes no data is available then that string is updated to null(NULL) and to nil(nil) sometimes to (null).So I m confused how to compare data in that variable.
if(stringvariable==NULL) // couldnot understand how to compare here ,with NULL or nil or (null)
{
// do something
}
When will the string variable change its state (to NULL or nil or (null)) ?
use this code..
if([stringvariable isEqualToString:#""] || [stringvariable isEqual:nil])
{
//Data not Found
}
else{
// Data not nil
}
You can check like
if([str length]>0 || ![str isEqualToString:#""]) {
// String is not empty
}
It should be :
if(![stringvariable isEqualToString:#""])
{
// stringvariable is not Empty.
}
else
{
// stringvariable is Empty.
}

How to compare self.title to a string in Objective C?

What am I doing wrong in the below code? My if statement is missing something:
if ([self.title] = "Upcoming Events") {
} else {
}
Correct would be:
if( [self.title isEqualToString:#"Upcoming Events"] )
self.title is a pointer, you can only compare it to other pointers using == not their values.
if ([self.title isEqualToString:#"Upcoming Events"]) {
NSLog(#"True");
} else {
NSLog(#"False");
}
You just have to write like this:
if([self.title isEqualToString:#"Upcoming Events"])
{
}
else
{
}
also .... in a if you should use "==" instead of "=". When there is "==" it is checking if they are equal while if there is "=" it gives the first one the value of the second.