Can You Evaluate a String in Swift

Can you evaluate a string in Swift?

No.

There is no equivalent of eval() in JavaScript or ScriptEngine in Java for Swift.

A common use for string evaluation is mathematical expressions; if you want to evaluate these, you can use NSExpression.valueWithExpression(format: String):

let stringWithMathematicalOperation: String = "5*5" // Example
let exp: NSExpression = NSExpression(format: stringWithMathematicalOperation)
let result: Double = exp.expressionValue(with:nil, context: nil) as! Double // 25.0

Evaluating a string in swift

You have to pass the double value so that you can get result 1.5

print(mathsSolver(item: "3.0/2.0"))

Above you are trying to pass Int value so that it is not giving you the proper result because integer division will always gives you the answer in integer.

Is it possible to use a string variable in if condition in Swift?

You should use a comparison operator for this.

if myString == myFunc() { 
// your code
}

If statement always wants a condition that can return a bool value. i.e. true and false.

In your above code, you are not providing sufficient data to if statement so that it can calculate whether the result iss true or false.

When you compare it like if myString == myFunc() , if statement will compare the string and return true if string matches else false.

if the string matches, it will execute the code that is within if conditions scope. Otherwise it will calculate the else condition.

UPDATE1:

I see you have updated the question, so you want to check if myFunc() is empty or not?

For that you can compare it with empty string.

if myFunc() == "" { 
// your code
}

UPDATE2:

Question: (asked in comment) instead of writing "if(x == 1)" i am trying to use a variable so my if statement is "if(stringVaraible)" where stringVariable = "x ==1". Basically I am asking if it is possible to turn a string into normal code

Answer: No, you can't do that. Swift is a compiled language, not interpreted like Ajax. Read more here: https://stackoverflow.com/a/30058875/8374890

Swift Programming: Evaluate value in a string

For your second question, assuming you're using a textField, you can use the delegate method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField

This method is executed when the return key is pressed. If don't want to hide the keyboard, you need to return NO from this function at the end.


If you're using a textView, you can refer to this thread. You implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n.

How do I check if a string contains another string in Swift?

You can do exactly the same call with Swift:

Swift 4 & Swift 5

In Swift 4 String is a collection of Character values, it wasn't like this in Swift 2 and 3, so you can use this more concise code1:

let string = "hello Swift"
if string.contains("Swift") {
print("exists")
}

Swift 3.0+

var string = "hello Swift"

if string.range(of:"Swift") != nil {
print("exists")
}

// alternative: not case sensitive
if string.lowercased().range(of:"swift") != nil {
print("exists")
}

Older Swift

var string = "hello Swift"

if string.rangeOfString("Swift") != nil{
println("exists")
}

// alternative: not case sensitive
if string.lowercaseString.rangeOfString("swift") != nil {
println("exists")
}

I hope this is a helpful solution since some people, including me, encountered some strange problems by calling containsString().1

PS. Don't forget to import Foundation

Footnotes

  1. Just remember that using collection functions on Strings has some edge cases which can give you unexpected results, e. g. when dealing with emojis or other grapheme clusters like accented letters.


Related Topics



Leave a reply



Submit