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

Jeff McDivitt
Jeff McDivitt
23,970 Points

Your task is to extend the String type and add a method that lets you add an integer value to the string. For example: "

Still getting error on this one when I am doing everything that is asked

types.swift
// Enter your code below

extension String {
    func add(number: Int) -> Int? {
        guard let convertedNumber = Int(self) else{
            return nil
        }
        return convertedNumber + number
    }
}

1 Answer

Anthony Lafont
Anthony Lafont
17,075 Points

Hi jeff,

I think the only problem in your code is that you call the parameter in the add function "number", and in the exercice they want you to name it "value".

Here is my code:

extension String {
    func add(value: Int) -> Int? {
        guard let convertedNumber = Int(self) else { return nil }
        return convertedNumber + value
    }
}
Jeff McDivitt
Jeff McDivitt
23,970 Points

Ahh!! I knew it was something little! Thanks Anthony!