ios - How can I transform String from a TextField to Int in Swift 3? -
this question has answer here:
i have function introduces new number using textfield:
//first node var root = treenode(7) @ibaction func introducirnumero(_ sender: any) { var d = int(entrada1.text!) print(d) var call = treenode(d!) root = treenode(d!) //let numero: int? = (int(entrada1.text!)!) }
this function should cast type , send int class:
class treenode { var value : int var left : treenode? = nil var right : treenode? = nil //incia como val init(_ rootvalue:int) { value = rootvalue } @discardableresult // if don't use method can send warning func addvalue( _ newvalue:int) -> treenode { if newvalue == value // exclude duplicate entries { return self } else if newvalue < value { if let newnode = left?.addvalue(newvalue) { return newnode } left = treenode(newvalue) return left! } else { if let newnode = right?.addvalue(newvalue) { return newnode } right = treenode(newvalue) return right! } } }
the problem seems there no cast because receive error in line:
var d = int(entrada1.text!)
and app crashes.
you can int("your number") , believe change int
Comments
Post a Comment