1

I updated shortly to Swift 1.2 which comes with Xcode 6.3.2 and wrote a few lines of code:

let originView:UIView!

  override init() {

      super.init()

  }

  init(sourceView:UIView, menuItems:Array<String>){

      super.init()

      originView = sourceView
  }

This code runs perfect till I updated the new version of Xcode (6.3.2).

Now I get the following error: Property 'self.originView' not initialized at super.init call

Until now I can't find a solution for this because I'm still learning Swift.

Do someone of you know how I can solve this problem?

P.S It's not a duplicate of this question because of this piece of code: init(sourceView:UIView, menuItems:Array<String>) instead of override init(frame: CGRect).

UPDATE:

New code:

override convenience init() {

    self.init()
}

init(sourceView:UIView, menuItems:Array<String>){

    originView = sourceView
    super.init()
}

New error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7fff5f300ff8) in this line of code: self.init()

Community
  • 1
  • 1
Vpor
  • 147
  • 1
  • 1
  • 7

4 Answers4

2

Try to modify this part.

 override init() {

      super.init()

  }

To

  override init() {

      self.init(UIView(), nil) // Or other initial values 

  }

If this is not resolve your bug, this is better way because init() call your custom init and initialize your variables.

Edit on update

Your modification is not correct

  override convenience init() {

      self.init() // This init referred "override convenience init()" !! So recursive call

  }

You must to give parameters of init call like i said before in order to redirect "override convenience init()" to "init(sourceView:UIView, menuItems:Array)".

Matthieu
  • 150
  • 7
  • For "nil" in second parameter you need to change the prototype of your init with "init(sourceView:UIView, menuItems:Array!)" – Matthieu Jun 16 '15 at 18:15
0

Interchange the 2 lines in the init(sourceView: UIView, menuItems:Array<String>):

init(sourceView:UIView, menuItems:Array<String>){
      originView = sourceView
      super.init()
  }

Hopefully, this will work.

Edit: Call super.init() in your first function

Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
  • I didn't get an error in the editor but if I run the app, I get an crash. I updated my question with the error message, crashing line ... . Hope you can help me. – Vpor Jun 16 '15 at 18:35
  • If I replace `self.init()` with `super.init()` in `override convenience init()`, I get the following error: `Convenience initializer for 'SideBar' must delegate (with 'self.init') rather than changing to a superclass initializer (with 'super.init')`. – Vpor Jun 16 '15 at 18:47
  • Remove the keyword convenience. – Ankit Goel Jun 16 '15 at 18:52
  • I removed `convenience`. Now I get the following error: `Property 'self.orignView' not initialized at super.init call`. – Vpor Jun 16 '15 at 18:58
0

You have to initialize all non optional mutable properties and all immutable properties (regardless of whether optional or not) before invoking the superclass initializer:

init(sourceView:UIView, menuItems:Array<String>){
    originView = sourceView
    super.init(frame: CGRect.zeroRect)
}

Note that since the originView property is immutable, you really don't need to make it an implicit unwrapped optional. The sourceView parameter passed to the initializer is not optional, so you can make that property a non optional:

let originView: UIView
Antonio
  • 71,651
  • 11
  • 148
  • 165
0

Change your this line :

let originView:UIView!

to

var originView:UIView!

And you are done.

itsji10dra
  • 4,603
  • 3
  • 39
  • 59