Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Ambiguous reference to member` when creating convenience init in extension

Tags:

ios

swift

swift2

This is my init:

extension NSNumberFormatter {

    convenience init(digits: Int = 0) {
        self.init() //ambiguous reference to member 'NSNumberFormatter.init'

        groupingSeparator = " "
        decimalSeparator = "."
        numberStyle = .DecimalStyle
        roundingMode = .RoundHalfDown

        maximumFractionDigits = digits
        minimumFractionDigits = digits
    }
}

What is the reason?

The same problem is when I put self.init() at the end of my convenience initializer.

like image 542
Bartłomiej Semańczyk Avatar asked Sep 11 '15 19:09

Bartłomiej Semańczyk


1 Answers

The reason is that you have created a new init that can be called without any parameters since digits has a default value of 0. So now, init() can refer to the default initializer or your new one. If you remove the default value for digits, it will then compile.

like image 51
vacawama Avatar answered Oct 20 '22 04:10

vacawama