46
devios1
7y

Getting the nth character of a string.

Every other language:

string[n]

Swift:

string[index(string.startIndex, offsetBy: n)]

Comments
  • 19
    A truly revolutionary an extraordinary language
  • 3
    @dontPanic I can't tell if you're being sarcastic or not. ;)
  • 4
    Fucking hassle!
    Cant you do something like:
    (This is psudocode, i dont know Swift)

    public static int str_index(str, n) {
    return index(str.startIndex, offsetBy: n);
    }

    ..
    string[str_index(string, n)];

    ?
  • 2
    Apple magic
  • 6
    Starting Swift 4 you can do String[x]
    If I'm not mistaken now you can use: String.characters[x]

    Though let's hope this revolutionary language doesn't fuck up our code again in the next release, been working with Swift since Jan 2015 and been crying once a year when new Swift is out 😒
  • 0
    Wouldn't that be the n+1th character?
  • 1
    @-eth It's a zero-indexed n. ;)
  • 1
    Google brings me here after typing "swift fuckifg String.index"

    What happened at Apple's quarters?
Add Comment