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 Object-Oriented Swift 2.0 Classes Instances of Classes

Sheng Wei
Sheng Wei
4,382 Points

[Help!] How to write initialiser method without a 2nd property?

1) The example in the previous video used the second property in the init method, but the example doesn't have this, so Im not sure how to write it.

2) Also, Im a little confused as to wether a tuple is required for an init method.

3)Finally Im not sure if my instance is correctly assigned

Would appreciate any help!

classes.swift
// Enter your code below
class Shape {
    var numberOfSides: Int  = 3
    let x: Int
    let y: Int
    init(numberOfSides: Int) {
       self.numberOfSides = Int(x: x, y: y) //Not sure what goes in here?
    }

  } 

let someShape = Shape(x: Int, y: Int) // Not sure whats wrong with my Arguement?

1 Answer

There's no need for x and y. Shape objects just have a number of sides:

class Shape {
    var numberOfSides: Int  = 3

    init(numberOfSides: Int) {
       self.numberOfSides = numberOfSides
    }
  } 

  let someShape = Shape(numberOfSides: 3)
Sheng Wei
Sheng Wei
4,382 Points

Thanks once again jcorum, you have no idea how much I value your immediate feedback...it helps me to keep moving forward!