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 trialEli MBS
1,158 PointsClasses
Hey guys! Would be very glad if someone could explain me step by step how I should solve this task: "Let's get in some practice creating a class. Declare a class named Shape with a variable property named numberOfSides of type Int.
Remember that with classes you are required to write an initializer method.
Once you have a class definition, create an instance and assign it to a constant named someShape."
Thank you!
// Enter your code below
1 Answer
Jennifer Nordell
Treehouse TeacherI would sincerely prefer to know what you've tried thus far that's failed so we can say what's going wrong. But here is my solution along with comments as explanations.
// Enter your code below
class Shape { //declare the class Shape
var numberOfSides: Int //declare a variable to hold the number of sides of type int
init (numberOfSides: Int) { // initialize the number of sides of the shape
self.numberOfSides = numberOfSides // this sets the number of sides to the number of sides passed in by the creation of a new shape
}
}
let someShape = Shape(numberOfSides: 5) // this creates a new object of type Shape with 5 sides... aka a pentagon
Hope this helps, but if you have questions let me know!