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 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!