4

I have a class and initialize the attributes with default values:

class Point {
  var x : Int
  var y : Int

  public init() {
    x = 1
    y = 1
  }
}

Now I want to have a reset() method which sets the attributes to these default values. Because I want to prevent redundancies I try to move the lines from the initializer to the reset method and call this method from init:

class Point {
  var x : Int
  var y : Int

  public init() {
    reset()
  }

  public func reset() {
    x = 1
    y = 1
  }
}

But it doesn't work. It says that the attributes have to be initialized. How can I fix this issue?

altralaser
  • 2,035
  • 5
  • 36
  • 55
  • http://stackoverflow.com/questions/41879186/swift-3-init-method-that-accepts-json-with-optional-parameters/41880053#41880053 – Leo Dabus Jan 27 '17 at 10:26
  • You should assign a default value in your class initializer. Note that a struct is preferred – Leo Dabus Jan 27 '17 at 10:28
  • If this is a simple lightweight class, you can consider making it immutable. Then you won't have a problem – Sulthan Jan 27 '17 at 10:33
  • `struct Point { let x: Int let y: Int init(x: Int = 1, y: Int = 1) { self.x = x self.y = y } }` – Leo Dabus Jan 27 '17 at 10:37
  • 1
    I know I have to set the default values in the initializer but this is not my use case. I want to have a class which is created at app start, modified and then I want to "reset" it to these default values at later time. I know that I can have the same lines of code in the init() and a second reset() method but this is worse. – altralaser Jan 27 '17 at 11:12

5 Answers5

1

Actually, you could provide default values at property declaration:

class Point {
    var x: Int = 1
    var y: Int = 1

    public init() {
        // No need to reset x & y
        // You can event omit init alltogether, if all properties have default values...
    }

    public func reset() {
        x = 1
        y = 1
    }
}
Alladinian
  • 34,483
  • 6
  • 89
  • 91
1

You can init your attributes on declaration

class Point {
  var x : Int = 1
  var y : Int = 1

  public init() {
  }

  public func reset() {
    x = 1
    y = 1
  }

While it add some duplication, it definitely solves your problem

Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31
1

You could have two private type members that hold the default values for your x and y properties, and use these as default parameter values in your initializer (as well as use these when resetting a Point instance):

class Point {
    static private let xDefault = 1
    static private let yDefault = 1

    var x: Int
    var y: Int

    public init(x: Int = Point.xDefault, y: Int = Point.yDefault) {
        self.x = x
        self.y = y
    }

    public func reset() {
        x = Point.xDefault
        y = Point.yDefault
    }
}

let p1 = Point()
print(p1.x, p1.y)    // 1 1

let p2 = Point(y: 2)
print(p2.x, p2.y)    // 1 2
p2.reset()
print(p2.x, p2.y)    // 1 1

let p3 = Point(x: -1, y: 2)
print(p3.x, p3.y)    // -1 2
p3.reset()
print(p3.x, p3.y)    // 1 1
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • But then I have all attributes twice. – altralaser Jan 27 '17 at 11:43
  • @altralaser but you don't have duplicate typed _literal values_, meaning if you choose to update the default values to your `Point` class, you only need to perform this update at a single location (at the type properties). – dfrib Jan 27 '17 at 12:08
0

You can skip the init altogether…

class Point {
    var x = 1
    var y = 1

    public func reset() {
        x = 1
        y = 1
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • But does it make a difference to preset the attributes or do it with an init method? It seems that's not possible without duplicates. But your solution works so I accepted it. – altralaser Jan 27 '17 at 11:50
-1

Declare your attributes optional like below.

class Point {
  var x : Int!
  var y : Int!

  public init() {
    reset()
  }

  public func reset() {
    x = 1
    y = 1
  }
}
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67