Type cast string to Int

102 views Asked by At

I am getting string in response, i want to convert it into Int following is my code

Int("\(dicOtherData["active_job"])!")!

but it will giving fatal error at run time

1

There are 1 answers

0
Tom E On

Never force-unwrap an optional if you cannot be 100 % sure that it contains what you expect. Rather use a pattern like this:

if let myString = dicOtherData["active_job"], let myInt = Int(myString) {
    // do something
}