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 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,045 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