Convert Equation String to Int [duplicate] - ios

This question already has answers here:
Swift - Resolving a math operation in a string
(4 answers)
Closed 7 years ago.
I'm trying to convert a string like so:
var equation = "8+3*4/5-1*(5+5)"
to an Int so that it will calculate it with order of operations but with my code it is just return nil. Here is what I have tried
var answer = Int(equation)

Try it this way:
var equation = "\(8+3*4/5-1*(5+5))" //"0"
And this way you can convert it to Int
var answer = equation.toInt() // 0
Or you can directly do it this way:
var equation = 8+3*4/5-1*(5+5) // 0
And As Martin R suggested if you want to do math operation in a string you can do it this way:
let expn = NSExpression(format:"8+3*4/5-1*(5+5)")
println(expn.expressionValueWithObject(nil, context: nil)) //"0"
Which will result as String but you can convert it to Int as I suggested above.
Here is original link for math operation in a string: Swift - Resolving a math operation in a string

Related

how can I convert String to Data [duplicate]

This question already has answers here:
Creating NSData from NSString in Swift
(9 answers)
Closed 4 years ago.
I have next:
deviceName = String.init(bytes: temp.prefix(upTo: index), encoding: .windowsCP1251)
where temp - [UInt8]. My question is: how can I convert this string back to Data?
I'm trying to convert like this:
newDataName = Data(newName.windowsCP1251)
But result is:
Value of type String has no member windowsCP1251
It is works with converting using utf8, but it shows russian characters incorrect. I need to use windowsCP1251 only:
newDataName = Data(newName.utf8)
Maybe this is what you looking for:
newDataName = newName.data(using: .windowsCP1251)

What is use of underScore in Double value in swift? [duplicate]

This question already has answers here:
What is type of 123_456_789?
(2 answers)
Closed 5 years ago.
I just recently look into Apple documentation I found code like below
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
What is the use of "_" in double value? it's working without error why?
Just a way to improve readability. According to Swift documentation:
Numeric literals can contain extra formatting to make them easier to
read. Both integers and floats can be padded with extra zeros and can
contain underscores to help with readability. Neither type of
formatting affects the underlying value of the literal.

How to find index of Int element in an array? [duplicate]

This question already has answers here:
How to find index of list item in Swift?
(23 answers)
Closed 7 years ago.
Is there a "better"(less verbose) way of doing this?
var found: Int?
for i in 0...myArray-1 {
if myArray[i] == 3 {
found = i
}
}
similar to
let i = find(myArray, "1")
but for Int ?
This question is slightly "different" than How to find index of list item in Apple's swift? because it directly address the find function for Int, instead of String. Granted, the only difference is the title and pointing out that you can remove the quotation marks to get Int.
Here's an Int example
let myArray = [1, 2, 3]
var i = find(myArray, 2)
Good luck

How can I convert a String to an Int in swift? [duplicate]

This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 8 years ago.
I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?
try like this
In Swift 2.0, toInt(),replaced with initializers. (In this case, Int(someString).)
let myString : String = "50"
let x : Int? = Int(myString)
if (x != null) {
// converted String to Int
}
myString.toInt() - convert the string value into int .

EXC_BAD_INSTRUCTION - Fetching a Random Item from an Array [duplicate]

This question already has answers here:
Crash when casting the result of arc4random() to Int
(7 answers)
Closed 8 years ago.
I am trying to fetch a random item from an array containing 3 strings as follows :
func selectRandomImage () {
var imageName : String? //Optional
var arrayCount : NSNumber = animalsArray.count //Bridges to an NSNumber
var x : Int = Int(arc4random())%(arrayCount.integerValue)
var name : String = animalsArray[x] as String
println("Name is \(name)")
}
However every 5 runs or so of the code, I am getting a crash with EXC_BAD_INSTRUCTION.
Could anyone advise on what could be wrong or how to troubleshoot ?
Thanks !
arc4random doesent work well with swift, it crushes when you try to cast it to Int
Try Int(rand()) instead of arc4random()

Resources