Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中将子字符串升级到索引_Swift_Substring - Fatal编程技术网

在Swift中将子字符串升级到索引

在Swift中将子字符串升级到索引,swift,substring,Swift,Substring,我想得到一个子串到某个索引 let index = n % s.count let remainderOfS = s.substring(from: index) 但它不允许索引,因为它是Int而不是String.index。所以我试着 let index = n % s.count let strIndex = s.formIndex(s.startIndex, offsetBy: index) let remainderOfS = s.substring(from: strIndex)

我想得到一个子串到某个索引

let index = n % s.count
let remainderOfS = s.substring(from: index)
但它不允许索引,因为它是Int而不是String.index。所以我试着

let index = n % s.count
let strIndex = s.formIndex(s.startIndex, offsetBy: index)
let remainderOfS = s.substring(from: strIndex)
但现在这就产生了问题:无法使用类型为“(String.Index,offsetBy:Int)”的参数列表调用“formIndex”,需要类型为“(inout Self.Index,offsetBy:Int)”的参数列表,即使这是我在其他人问这个问题时发现的解决方案。我错过了什么

编辑:这也不起作用

let remainderOfS = String(s[..<index])

let remainderOfS=String(s[…此扩展可能会对您有所帮助

extension String {
    func index(from: Int) -> Index {
        return self.index(startIndex, offsetBy: from)
    }

    func substring(from: Int) -> String {
        let fromIndex = index(from: from)
        return substring(from: fromIndex)
    }

    func substring(to: Int) -> String {
        let toIndex = index(from: to)
        return substring(to: toIndex)
    }
}

@汤米:那你是从哪里找到解决办法的?