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

Florian Groussin
Florian Groussin
4,563 Points

Hi

hello this code is obviously not working could some one could tell what i'am not doing right then permit me to correct myself?

Thank you in advance have a nice day & week end.

Florian

types.swift
extension String {

    var add: Int {
        return self + Int if {
            self = Int }
        else {
            return nil }

        }

    }
Lukas Muller
Lukas Muller
7,347 Points

Could you describe what you are supposed to do?

1 Answer

Alexander Karan
PLUS
Alexander Karan
Courses Plus Student 15,220 Points

So in this challenge you need to add a function and convert the string to a int. The function need to return an optional because the string might not be a number. You can use int(self) to convert

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