Error after update to Swift 1.2

182 views Asked by At

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()

4

There are 4 answers

0
itsji10dra On BEST ANSWER

Change your this line :

let originView:UIView!

to

var originView:UIView!

And you are done.

4
Ankit Goel On

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

0
Antonio On

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
1
Matthieu On

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)".