swift - Ranges in Swift3 -


i'm struggling 1 of swift3 changes. earlier treated ... , ..< range, have ton of new "sub-ranges". have function taking both range variants.

struct test {   var data = [1,2,3,4,5]   subscript(r:range<int>) -> [int] {     return array(data[r])   } } let t = test() print(t[0...1]) // fails because closedrange print(t[0..<2]) 

i'd have

  subscript(r:takesanyrange<int>) -> [int] { 

you can avoid duplication instead using countablerange , countableclosedrange, since 1 convertible other:

struct test {   var data = [1,2,3,4,5]    subscript(r: countablerange<int>) -> [int] {     return array(data[r])   }    subscript(r: countableclosedrange<int>) -> [int] {     return self[countablerange(r)]   } } 

this works fine int range, specified in question. apple documentation:

you can create countable range on type conforms strideable protocol , uses integer associated stride type. default, swift’s integer , pointer types usable bounds of countable range.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -