Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift can't assign Range to variable?

I'm trying to come up with a way to extract a image from an HTML page. (I haven't looked up how this is actually done, I'm just trying to come up with something from scratch so don't judge) Anyway, the code I've written so far gets the error "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 );

Okay, so a the type a Range is composed of is Index, not Int. But when I try to change the declaration I get a "Use of undeclared type 'Index'" error. So now it's telling me Index is a figment of my imagination?

//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 );
like image 894
PopKernel Avatar asked Dec 05 '22 21:12

PopKernel


1 Answers

What rangeOfString produces isn't a range of Index; it's a range of String.Index.

like image 72
matt Avatar answered Dec 18 '22 00:12

matt