Reputation: 567
I try to initial my class with block (closure) but I got
Ambiguous use of 'init'
all the time, my class in objective c and I try to use it in swift4
Objc init
typedef void (^updateCoordinate)(CLLocationCoordinate2D coordinate);
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate Dragable:(BOOL)isDragable updateCoordinate:(updateCoordinate)updateCoordinate;
Swift init
let map = FAMapViewController.init(coordinate: CLLocationCoordinate2DMake(0, 0), dragable: true) { (coordinate) in }
NS_SWIFT_NAME
does not help me, I got same issue any advice Please .
Upvotes: 4
Views: 15149
Reputation: 530
In my case, the issue of Ambiguous use of 'init'
was that I had a class in my project with the exact same name of another class inside a SPM Package dependency.
Therefore, Xcode didn't know which class to use, mine or the one from the Package.
If you ⌘ command
+click on the name of the class, you will see that Xcode will show you two different options of locations. That's where I understood the problem.
Upvotes: 0
Reputation: 480
In some cases, you need to check the DUPLICATE of the init
function in your class
or struct
. This example will be showing error Ambiguous use of 'init'
init(name: String = "") {
self.id = -1
self.name = name
}
init(id: Int = -1, name: String = "") {
self.id = id
self.name = name
}
Upvotes: 0
Reputation: 9330
Ambiguous use of 'init'
means the signature of the init
call you're making in your Swift can't be matched to an init
in the target class.
I've seen this error caused by simple mistakes in the call site - like capitalisation being incorrect. For example in your Swift init
call you have a lowercase dragable: true
but in the Objc init you have Dragable:(BOOL)
StackOverflow is full of examples of incorrect capitalisation, e.g.:
IOS: Ambiguous Use of init(CGImage)
SCNSceneSource init is giving Ambiguous use of 'init(URL:options:)' error
Upvotes: 2
Reputation: 567
I found solution for my issue , I use same method name with deferent parameter in Objective-C Class
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
Dragable:(BOOL)isDragable
updateCoordinate:(updateCoordinate)updateCoordinate;
and
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
Dragable:(BOOL)isDragable;
just used
NS_SWIFT_NAME
to change names and it's work fine
NS_SWIFT_NAME(init(withUpdateCoordinateAndCoordinate:isDragable:withUpdateCoordinate:));
and
NS_SWIFT_NAME(init(withCoordinate:isDragable:));
Upvotes: 0