Reputation: 169
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?
Upvotes: 2
Views: 1103
Reputation: 16660
As in Objective-C a class can have convenience (secondary) initializers and designated (primary) initializers.
The path of code execution should be:
self.designatedInit()
.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?
init()
is really a convenience initializer.self.designatedInit()
instead of self.init()
.init()
and call super.init()
(or whatever is the designated initializer of the super class.Upvotes: 2