2

I'm trying to add a color picker in my add and I use this https://github.com/gizmosachin/ColorSlider library which is only written in swift and I use objective-c. I have followed this guide How to call Objective-C code from Swift on how to add swift libraries in objective-c projects. I'm 99% sure that I have properly configured xcode because I can import the swift library and run my app without an error, it's only when I try to instantiate the swift class that the app crashes and I see that the init method for the swift class is called infinitely. The source code for the library is one file and listed for reference just in case (https://github.com/gizmosachin/ColorSlider/blob/master/Source/ColorSlider.swift)

Here is one of the init methods (the other inits are overrides)

// MARK: Initializers
convenience init() {
    println("hi there swift")
    self.init()
    backgroundColor = UIColor.clearColor()
}

in my log I see "hi there swift" print out many times. This is how I initiate the swift class

ColorSlider *colorSlider = [[ColorSlider alloc] init];

I know that the function containing the line of code above is only being called once because I used NSLog(@"output") to see how many times this shows up and the output looks like this

output
hi there swift
hi there swift
hi there swift
hi there swift
hi there swift
etc...to infinity or until app crashes

Am I instantiating the swift class correctly? I'm not sure why the swift class's init method is called infinitely

----UPDATE-----

It looks as if the init methods below also use super.init? enter image description here

Community
  • 1
  • 1
poopit
  • 169
  • 9

2 Answers2

2

Remove self.init. The method is calling itself.

Dan Loughney
  • 4,647
  • 3
  • 25
  • 40
  • Thanks for answering, I tried changing self to super but I get a "Convenience initializer for 'ColorSlider' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super init')", I'm not too familiar with swift but I'm going to look more into swift's docs, but what was the reasoning behind changing self to super? going to update question with screenshot of error. Sorry if I made a dumb mistake :/ – poopit Aug 19 '15 at 01:21
  • 2
    Was assuming that your class was a subclass. In your case you don't need to call self.init(). What is happening is that your init() is recursively calling itself. Not even sure that you need to declare this as convenience. -- learning Swift myself right now:) – Dan Loughney Aug 19 '15 at 01:30
2

As in Objective-C a class can have convenience (secondary) initializers and designated (primary) initializers.

The path of code execution should be:

  1. A convenience initializer should call a designated initializer with self.designatedInit().
  2. A designated initializer should call a super's designated initializer with super.designatedInit().

In your code, init() is a convenience initializer (convenience init()). calling self.init() would lead to an infinite loop, because this is the actually running function itself as Dan said.

If you change it to super.init() a convenience initializer is calling a super's initializer, what is illegal because of the above rule #1.

What to do?

  1. Check, whether init() is really a convenience initializer.
  2. If so, call self.designatedInit() instead of self.init().
  3. If not so, change the classification of init() and call super.init() (or whatever is the designated initializer of the super class.
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • So I ended up just deleting convenience init and now I can call the color slider since trying to call self.designatedInit caused another error, not sure what the repercussions are but since the only line in convenience init is setting the background color it should be fine. But thanks for your in depth explanation of designated and convenience init – poopit Aug 20 '15 at 13:43