Unwrapping UITextField String and converting it to Int with new syntax

With the latest Xcode update, we are required to update the syntax on some of our older apps. Whilst Xcode will guide us through it the results do not always work. Being fairly new Swift and Xcode this is a big preoblem for me. The simple code below has been converted to the new syntax:

@IBAction func buttonPressed(sender: AnyObject) {

var numberInt = Int(number.text)

if numberInt != nil {

var unwrappedNumber = numberInt!

var isPrime = true

if unwrappedNumber == 1 {

isPrime = false

}

From the second line of this I am getting the error of "Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?' ?". I take the recommended action and change the line to:


var numberInt = Int(number.text!)


Then it tells me "Type 'Int' has no member 'number'". i am really stuck. Can anybody help?

If number is UITextField I don't see anything wrong with your code. Well maybe you should check optionals instead of using forced unwrapping. 😉

Unwrapping UITextField String and converting it to Int with new syntax
 
 
Q