首页 > 解决方案 > 调用中的无关参数标签“with:”

问题描述

在这段代码中

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

Extraneous argument label 'with:' in call将 xcode 更新为 10.01 后出现错误

如何修复错误?

标签: swiftxcodexcode10

解决方案


无法使用类型为 '(Range<String.Index>)' 的参数列表调用类型为 'Range<String.Index>' 的初始化程序,可以通过删除Range<String.Index>(...)转换来修复编译器错误。这仍然会导致警告

'characters' 已弃用:请直接使用字符串或子
字符串 substring(with:)' 已弃用:请使用字符串切片下标。

可以用

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

但是,您可以更简单地获得相同的结果

text = String(prospectiveText.prefix(maxLength))

推荐阅读