Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Optionals Optional Chaining

Swift 2: replacing ?.toInt() with ?.int()

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

var testString1 = "1"   // -> "1"
Int(testString1)        // -> 1

var testStringA = "A"   // -> "A"
Int(testStringA)        // -> nil

However if I replace toInt() in the code with int() I get this error:

if let culprit = findApt("101")?.toInt() { // 'toInt()' is unavailable: Use Int() initializer
    sendNoticeTo(aptNumber: culprit)
}
if let culprit = findApt("101")?.int() { // Value of type 'String' has no member 'int'
    sendNoticeTo(aptNumber: culprit)
}

What am i doing wrong, or why isn't this working?

Thank you!

With out seeing the rest of what your doing, it should be: let culprit = findApt("101") as Int

Still, you souldn't declare a variable inside an "if" conditional statement. That's going to through an error.

Here is all my code, if I change it to if let culprit = findApt("101") as Int I get the error 'String?' is not convertible to 'Int'

Thank you James Lambert!

//: Swift Functions and Optionals - Optionals - Optional Chaining

import UIKit

func sendNoticeTo (aptNumber aptNumber: Int) {
    print("Message send")
}
func findApt (aptNumber : String ) -> String? {
    let aptNumbers = ["101", "202", "303", "404"]
    for tempAptNumber in aptNumbers {
        if ( tempAptNumber == aptNumber) {
            return aptNumber
        }
    }
    return nil
}
/*
if let culprit = findApt("505") {
    if let aptNumber = Int(culprit) {
        sendNoticeTo(aptNumber: aptNumber)
    }
}
*/
if let culprit = findApt("101") as Int { // Error: 'String?' is not convertible to 'Int'
    sendNoticeTo(aptNumber: culprit)
}

let testString1 = "1"   // -> "1"
Int(testString1)        // -> 1

let testStringA = "A"   // -> "A"
Int(testStringA)        // -> nil

3 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Because of the changes to Swift, we want to do this code a bit different.

if let culprit = findApt("101") { // We give a string value to culprit
    sendNoticeTo(aptNumber: Int(culprit)!) // Then we can change it to Int. 
}

This will be the easiest walk around solution.

Good luck ! :)

Brian Patterson
Brian Patterson
19,588 Points

Couldn't the Swift 2.0 videos have been released sooner. As I feel that we as customers are in no mans land. I understand they are being released mid October. Isn't this leaving it a bit late.

Christian Dangaard
Christian Dangaard
3,378 Points

Thank you, this also worked for me.

Hi,

With swift 2.0 it became: if let culprit = Int(findApt("101")!){

This works only if the aptNumber is in your array, otherwise you wil get an error code.

Mahdi Pedramrazi
Mahdi Pedramrazi
2,505 Points

this will fail when you add invalid apt number

Ok, Thank you Onur and Jhoan!

It worked :)