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 2 Extensions and Protocols Extending a Native Type

Kittipol Munkatitum
Kittipol Munkatitum
13,074 Points

got stuck with challenge

Hi,I can not pass this challenge when I write my code and check my work, treehouse always told that have problem about communication and this is my code

extension String{


    func add(number: Int) -> Int  {
        if let unwrapped:Int = Int(self) ?? 0{
            return unwrapped + number
        }
    }

}

could you help me solve this challenge thanks

3 Answers

You were definitely on the right track. This one took me a while to figure out. Since the string that we are trying to add a number to might or might not have a number inside of it, we aren't actually returning and Int but an Optional of type Int. We also have to remember to return nil if the string doesn't have an integer inside of it. This is how I did it.

extension String {
    func add(num: Int) -> Int? {
        if let value = Int(self) {
            return value + num
        } else {
            return nil
        }
    }
}

I hope that helps. Good luck!

Can you explain your code? I did not understand the part "Int(self)". I'm also stuck on this problem. Thank you.

Sure Farhan Hussain . We are being asked to add an extension to the String type but return a value of type Int. It's impossible to do computations with 2 values of type Int and String. So the way I decided to do this was by converting the string that we're adding the method to into an integer. That is, if I have something like this:

"1"

I convert it into an integer by doing this:

Int("1") == 1

Once I have done that, I can now use it for calculations in my method and return an integer. The

Int(self)

part just means that the conversion gets applied to whatever string we happen to be running the method on at the moment. I hope that cleared things up a bit.

Thank you it helped a lot.

Hey guys,

Jason's answer works just fine but I thought I'd add that this is the perfect time to use guard because casting a String into an Int — via Int("someString") — returns an optional. So below is a more semantic way to do the deed. Feel free to ask for clarification, if you need it :)

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

        return this + val
    }
 }

"1".add(val: 2) // returns 3 as an Int
"two".add(val: 3) // returns nil