.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
How Does String Substring Work in Swift
How to Create a Global Variable
Initialize @Stateobject With a Parameter in Swiftui
Realitykit VS Scenekit VS Metal - High-Quality Rendering
Get Integer Value from String in Swift
How to Get Monday'S Date of the Current Week in Swift
Swiftui View and @Fetchrequest Predicate With Variable That Can Change
How to See Which Version of Swift I'M Using
Segue from Modal View to Tab Bar View Controller and Not Lose Tab Bar
Wait Until Swift For Loop With Asynchronous Network Requests Finishes Executing
Uncaught Error/Exception Handling in Swift
Swift Constants: Struct or Enum
How to Use Attributed String in Swiftui
How to Set Layer Cornerradius For Only Bottom-Left, Bottom-Right, and Top-Left Corner