cristan lika cristan lika - 16 days ago 10
iOS Question

NumberFormatter Argument labels '(_:)' do not match any available overloads

Following this line

lable2xValue.text = numberFormatter.string(from: value)


First this error throwing


Cannot convert value of type 'Double' to expected argument type 'NSNumber'


Then Xcode suggest
value
replace with
NSNumber


lable2xValue.text = numberFormatter.string(from: NSNumber(value))


When i do that then this error showing


Argument labels '(_:)' do not match any available overloads


Here is my full code

//computed property
var value2x : Double? {
if let value = value1x{
return (value * 2)
}
else{
return nil
}
}
//objerver
var value1x : Double? {
didSet{
updateLabel2xValue()
}
}

func updateLabel2xValue(){

if let value = value2x {
//lable2xValue.text = "\(value)"


//MARK:- ERORR Cannot convert value of type 'Double' to expected argument type 'NSNumber'
//lable2xValue.text = numberFormatter.string(from: value)

//MARK:- ERROR Argument labels '(_:)' do not match any available overloads
lable2xValue.text = numberFormatter.string(from: NSNumber(value))

}
else {
lable2xValue.text = "???"
}

}

var numberFormatter : NumberFormatter = {
let ls = NumberFormatter()
ls.numberStyle = .decimal
ls.minimumFractionDigits = 0
ls.maximumFractionDigits = 2
return ls
}()


@IBAction func textField1xValueEditingChange(_ sender: AnyObject) {
//print(self.textField1xValue.text)
if let value = self.textField1xValue.text , let doubleValue = Double(value){
value1x = doubleValue

}else{
value1x = nil
}
}


Note : if i am trying this way then it is working fine

lable2xValue.text = numberFormatter.string(from: 50.33)

Answer

The compiler wants

numberFormatter.string(from: NSNumber(value:value))

unfortunately it doesn't suggest that.

A quick hint: ⌘-click on NSNumber to see the available initializers in the declaration file.


Alternatively in the current version of Swift in Xcode 8.1 you can also bridge cast Double to NSNumber

numberFormatter.string(from: value as NSNumber)