.Toint() Removed in Swift 2

.toInt() removed in Swift 2?

In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String

Int(myString)

In your case, you could use Int(textField.text!) insted of textField.text!.toInt()

Swift 1.x

let myString: String = "256"
let myInt: Int? = myString.toInt()

Swift 2.x, 3.x

let myString: String = "256"
let myInt: Int? = Int(myString)

Converting String to Int with Swift

Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):

    // toInt returns optional that's why we used a:Int?
let a:Int? = firstText.text.toInt() // firstText is UITextField
let b:Int? = secondText.text.toInt() // secondText is UITextField

// check a and b before unwrapping using !
if a && b {
var ans = a! + b!
answerLabel.text = "Answer is \(ans)" // answerLabel ie UILabel
} else {
answerLabel.text = "Input values are not numeric"
}

Update for Swift 4

...
let a:Int? = Int(firstText.text) // firstText is UITextField
let b:Int? = Int(secondText.text) // secondText is UITextField
...

Swift 2.0 String to Integer

In Swift 2 you have to use the Int initializer instead of .toInt() (returns Int?):

var enteredYears = Int(age.text)

Removing the decimal part of a double without converting it to Int in swift

I recommend NumberFormatter, this example creates a reusable formatter.

let decimalFormatter : NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter
}()

private var displayValue : Double {
get {
return Double(display.text!)!
}
set {
display.text = decimalFormatter.string(for: newValue)
}
}

How convert [Int] to int?

index is array of Int. But you need to pass Int to the remove method.

If you want to remove all objects for selected row indexes, than write:

let indexes = tableView.selectedRowIndexes.map { Int($0) }
indexes.reversed().forEach { arrDomains.remove(at: $0) }

If you want to remove object at some index, than write:

guard let index = tableView.selectedRowIndexes.map { Int($0) }.first(where: { /*.your condition here */ }) else { return }
arrDomains.remove(at: $0)


Related Topics



Leave a reply



Submit