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

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 .

Related

Swift split String into array [duplicate]

This question already has answers here:
Swift Put String to Array from one to all letters
(5 answers)
Closed 2 years ago.
I have a string with no spaces I would like to turn the string into an array.
Let's say the string is "guru1", I would like my array to be ["g", "gu", "gur", "guru", "guru1"]
Thanks in advance.
You can use reduce :
"guru1".reduce([]) { $0 + [($0.last ?? "") + "\($1)"] }

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)

Find out if a Swift string contains emoji [duplicate]

This question already has answers here:
Find out if Character in String is emoji?
(17 answers)
Closed 5 years ago.
I am working in an application in which i need to look up emoji characters from the string means a string contains one or more emojis ? As i am new in swift so i am not aware about it.
exa. "Newbie 😀" will returns yes and
"Newbie" will returns no
Yes please use below method to identify emojies
public var checkcontainEmoji: Bool
{
for ucode in unicodeScalars
{
switch ucode.value
{
case 0x3030, 0x00AE, 0x00A9,
0x1D000...0x1F77F,
0x2100...0x27BF,
0xFE00...0xFE0F,
0x1F900...0x1F9FF:
return true
default:
continue
}
}
return false
}
Output : - "HI 😀".checkcontainEmoji --> TRUE

Convert Equation String to Int [duplicate]

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

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