Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Intermediate Swift Extensions and Protocols Extending a Native Type

Jesse Gay
seal-mask
.a{fill-rule:evenodd;}techdegree
Jesse Gay
Full Stack JavaScript Techdegree Student 14,044 Points

Extending String with add func works in Xcode, but fails challenge. Why?

My solution works in Xcode but fails the challenge. Does anyone know why? The hint says "Make sure you're adding an add method that takes an Int and returns Int?" but I think my solution meets those criteria. The "best answer" for this challenge was similar, but used a guard statement instead. Thanks!

types.swift
// Enter your code below
extension String {
    func add(_ value: Int) -> Int? {
        if let convertedNumber = Int(self) {
            return (value + convertedNumber)
        } else {
            return nil
        }
    }
}

2 Answers

Magnus Hållberg
Magnus Hållberg
17,232 Points

Its just a small thing, you are omitting the external name in your method. The challenge doesn't tell you to do that so that gives you an error even though there is nothing wrong with your code. /M