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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -