SWIFT: Subscript int in String (Cnndition works, assignment not) -
i want assign first char of string. of course there advance function, me (i come c++) easier read when using [index]. defined extension string type:
extension string { subscript (i: int) -> character { return self[advance(self.startindex, i)] } subscript (i: int) -> string { return string(self[i] character) } ...
this works fine condition like:
if (s[0] == "u")
but when assigning value like
var c = s[0]
i error compiler can't subscript string type of int. difference?
i think compiler cannot infer return type of subscript
, since these 2 subscript
have same input signature.
so have specify subscript
want use. this,
var c:character = s[0]
or
var c:string = s[0]
Comments
Post a Comment