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 trialMatthew Ingram
Courses Plus Student 4,718 PointsAdding Extensions trouble with treehouse's compiler
When I use the code in playground it works fine but in, I get an error message when I check my work!
// Enter your code below
extension String{
var Add : Int? {
if Int(self) != nil {
return Int(self)! + 1
} else {
return nil
}
}
}
5 Answers
Jeff McDivitt
23,970 PointsHi Matthew -
Yes it will work, but the treehouse challenges are very specific and that is why it does not work in there
Jeff McDivitt
23,970 PointsHi Matthew -
- You first need to create a method that lets you add an integer value (I do not see this in your code)
- I use a guard statement to check if the string contains an integer value and if not it returns nil
- If it does contain and integer then it returns the converted number plus the value
extension String {
func add(value: Int) -> Int?{
guard let convertedNumber = Int(self) else{
return nil
}
return convertedNumber + value
}
}
Matthew Ingram
Courses Plus Student 4,718 Pointsextension String{
func add(number: Int) -> Int?{
if Int(self) != nil {
return Int(self)! + number
} else {
return nil
}
}
}
Matthew Ingram
Courses Plus Student 4,718 PointsShould this work?
Jeff McDivitt
23,970 PointsNo this will not work I provided the correct way to do it in my last response. Are you just looking for other ways to achieve the result?
Matthew Ingram
Courses Plus Student 4,718 Pointsyes thank you for your response I wrote the code above in Playground and it seemed to work without any problem but within the treehouse compiler, it was denied. Is the main issue that I forced unwrapped it?