Swift 无法将范围分配给变量?

Swift can't assign Range to variable?

我正在尝试想出一种从 HTML 页面中提取图像的方法。 (我还没有查看这实际上是如何完成的,我只是想从头开始想出一些东西所以不要判断)无论如何,我到目前为止编写的代码得到了错误 "Cannot assign a value of type Range<Index>? to a value of type Range<Int>?"

//variable "html" has type String
        let variations = ["img","IMG","Img"]
        var tagStart: Range<Int>?
        var index = 0
        repeat
        {
           tagStart = html.rangeOfString(variations[index]) //Cannot assign a value of type Range<Index>? to a value of type Range<Int>?
            index += 1
        }while( tagStart == nil );

好的,所以构成 Range 的类型是 Index,而不是 Int。但是当我尝试更改声明时出现 "Use of undeclared type 'Index'" 错误。所以现在它告诉我索引是我想象中的虚构?

//variable "html" has type String
        let variations = ["img","IMG","Img"]
        var tagStart: Range<Index>?
        var index = 0
        repeat
        {
           tagStart = html.rangeOfString(variations[index]) //"Use of undeclared type 'Index'"
            index += 1
        }while( tagStart == nil );

rangeOfString 生成的不是索引范围;这是 String.Index.

的范围