Search code examples
swiftxcodexcode10

Extraneous argument label 'with:' in call


In this code

text = prospectiveText.substring( with: Range<String.Index>(prospectiveText.startIndex ..< prospectiveText.characters.index(prospectiveText.startIndex, offsetBy: maxLength)) )

i get the error Extraneous argument label 'with:' in call after I updated xcode to 10.01

How to fix the bug?


Solution

  • As in Cannot invoke initializer for type 'Range<String.Index>' with an argument list of type '(Range<String.Index>)', the compiler error can be fixed by removing the Range<String.Index>(...) conversion. This will still cause warnings

    'characters' is deprecated: Please use String or Substring directly
    substring(with:)' is deprecated: Please use String slicing subscript.

    which can be fixed with

    text = prospectiveText[..<prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)]
    

    However, you can achieve the same result far simpler with

    text = String(prospectiveText.prefix(maxLength))