创建范围< String.Index>从常量Int [英] Creating Range<String.Index> from constant Ints

查看:118
本文介绍了创建范围< String.Index>从常量Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码构造了一个范围,然后应该在调用substringWithRange中使用时,这是怎么回事?

What is wrong with this piece of code for constructing a range that should then serve in a call to substringWithRange?

let range = Range<String.Index>(start: 0, end: 3)

Swift编译器(在Xcode 7.1.1中)使用以下错误消息对其进行标记:

The Swift compiler (in Xcode 7.1.1) marks it with this error message:

无法为类型"Range< Index>"调用初始化程序参数类型为'(start:Int,end:Int)'

Cannot invoke initializer for type 'Range<Index>' with an argument list of type '(start: Int, end: Int)'

推荐答案

您需要引用特定字符串的startIndex,然后前进:

You need to reference the startIndex of a specific string, then advance:

let longString = "Supercalifragilistic"
let startIndex = longString.startIndex
let range = Range(start: startIndex, end: startIndex.advancedBy(3))

这篇关于创建范围&lt; String.Index&gt;从常量Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆