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

后端 未结 5 1391
一生所求
一生所求 2020-12-09 01:52

Just converted a project to Swift 3 and cant figure out the following error.

public func currencyString(_ decimals: Int) -> String {

    let formatter =          


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 02:20

    To clarify the confusion as to what the error is,

    NSNumber is calling NSNumber.init( value: X ) method to instantiate a NSNumber object.

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

    The code produces the error because NSNumber is not a type rather it is a class with members. "NSNumber(...)" instantiates a class object to contain the 'value' of (1.0 / 1.29).

    This is not a type conversion or cast like in C/C++. where you are trying to cast the type to allow the compiler to do its job.

    float y = 1.3;
    int x = int( y );
    

    NSNumber is not a type like int, float, char

    The error comes into play because there are several ways to call NSNumber.init( value: type )

    Swift is requiring that you specifically say that you want the 'value' member of the NSNumber to contain the value x.

      let localRate = NSNumber( 1.0 / 1.29)
      var y = NSNumber( 0 )
      var b = NSNumber( false )
    
    
    
       let localRate = NSNumber(value: 1.0 / 1.29)
       var y = NSNumber( value: 0 )
       var b = NSNumber( value: false )
    

    The confusion might be coming into play because this works.

    w = String( "4" )
    

    The class String does not require the argument label, while NSNumber does require an argument label of 'value:'

    Perhaps this is due to how IOS treats NSNumber as coming from legacy?

提交回复
热议问题