-1

As per title.

Because of the required init(a:), I can't initialize 'b', unless I resort to overloading as above.

Question (1):

Is there a better way of approaching this? It seems a little off looking at self.b=0 has to be included there.

class BaseClass {
var a: Int

required init(a: Int) {
    self.a = a
    }
}

class SubClass: BaseClass {
var b: Int

init(b: Int, a: Int){
    self.b = b
    super.init(a: a)
     }

required init(a: Int) {
    self.b = 0         // have to put something here, otherwise complain
    super.init(a: a)
     }
}

Then, calling, e.g.:

let mySubClass = SubClass(b:2, a:3) //works by calling init(b:a:)

note: another workaround is resorting to fatal error in required init as such (but again, it kinda doesn't look right):

required init(a: Int) {
 // fatal error()
 }

Additionally, this brings me to the next question, regarding the example below.

Question (2):

With the presence of a protocol, for example:

protocol RandomProtocol {
    init(randomProp: Int)
}

Calling e.g.:

let mySubClassWithProtocol = SubClassWithProtocol(c: 1, a: 2, b: 3)      //works ok

Is there a better way of implementing this? again, like above, it seems a little convoluted and counter-intuitive

SubClassWithProtocol below needs to implement required init from RandomProtocol and SubClass:

class SubClassWithProtocol: SubClass, RandomProtocol {

var c: Int

init(c: Int, a: Int, b: Int, randomProp: Int) {
    self.c = c
    super.init(b: b, a: a)
}

required init(a: Int) {
    self.c = 0
    super.init(a: a)
}

required init(randomProp: Int) {
//need to call self.c and super.init, otherwise complain    
    self.c = 0
    super.init(b: 0, a: 0)
}   

note:

Just starting out with Swift here. Trying to figure out my way around. I understand the questions above probably are more academic than practical. But thanks for any help.

  • **var b: Int?** – Shehata Gamal Jul 20 '18 at 12:14
  • Clarification on the questions: Is there a better way of writing the code simpler and cleaner rather than resorting to having to declare 3 different types of initializers (e.g. in SubClassWithProtocol)? – Rizal Atan Jul 20 '18 at 12:28
  • 1
    How will this make you learn Swift better? Most often when designing classes for real world situations it comes natural how properties needs to be initialized and you write init methods for those situations, trying to solve problems that doesn’t exists is something you should never do. – Joakim Danielson Jul 20 '18 at 12:37

1 Answers1

1

Are any other ways to initialise properties other than having to set defaults in the the initialisers? Yes there are -

  • You could make b an optional - var b: Int?

  • You could make b an implicitly-unwrapped optional - var b: Int!

  • You could set a default value for b at declaration - var b: Int = 0

These methods will prevent you from having to call self.b = 0 in your initialisers but as Joakim has pointed out in the real world which method you use depends on your situation.

Chris Edgington
  • 2,937
  • 5
  • 23
  • 42