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 trialRichard Sun
11,257 PointsWhy does Treehouse say that I haven't added an add method?
Treehouse says that I haven't added an Add Method and one that returns an int even though I have. What is wrong?
// Enter your code below
extension String {
func add(value: Int) -> Int {
do {
let num = Int(self)
return num! + value
} catch {
return nil
}
}
}
1 Answer
Jeff McDivitt
23,970 PointsThe easiest way to handle this is to make the return Int an optional and use a guard statement
func add(value: Int) -> Int?{
guard let convertedNumber = Int(self) else{
return nil
}
return convertedNumber + value
}
}