Swift不能将Range分配给变量?

时间:2015-06-23 01:58:51

标签: swift cocoa variable-assignment

我试图想出一种从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 );

1 个答案:

答案 0 :(得分:15)

rangeOfString产生的不是一系列索引;它是一系列String.Index。

相关问题