0

I got trouble with that when I convert from String to Int in swift 2

func sendNoticeTo(aptNumber aptNumber: Int) {

}

func findApt (aptNumber : String ) -> String? {
let aptNumbers = ["101","202","303","404"]
for tempAptNumber in aptNumbers {
    if ( tempAptNumber == aptNumber) {
        return aptNumber
    }
}

if let culprit = findApt("101")?.toInt() {
sendNoticeTo(aptNumber: culprit)
}

Swift say that function isn't available and I searched that problem but those didn't help.

Po Seidon
  • 19
  • 4

2 Answers2

2

Swift 2.0 its changed from toInt() to int()

Upto Swift 1.2

let str: String = "101"
let myInt: Int? = str.toInt()

we use like

if let culprit = findApt("101")?.toInt() {
sendNoticeTo(aptNumber: culprit)
}

Swift 2.0 & above

let str: String = "101"
let myInt: Int? = Int(str)

so use

if let culprit = Int(findApt("101"))  // or use like findApt("101") as! Int
{
sendNoticeTo(aptNumber: culprit)
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • I tried that method before i ask but it still show error so i edit my post and post all codes. Thanks your time. – Po Seidon Oct 11 '15 at 17:39
0

In Swift 2 toInt() got replaced by int() and this works:

Int("222") // -> 2

Or to match your example:

Int(findApt("101")) // -> 101
Mate Hegedus
  • 2,887
  • 1
  • 19
  • 30
  • I tried that method before i ask but it still show error so i edit my post and post all codes. Thanks your time. – Po Seidon Oct 11 '15 at 17:39