0

In function, that perfectly worked in Xcode 6.2, now I have a mistake 'subscript' in unavailable: Indexing a String's UTF16View requires a String.UTF16View.Index, which can be constructed from Int when Foundation is imported. This is the code:

extension Character {
    var keyCode: Int {
        return Int(String(self).utf16[0])
    }
}

And in this code I get the same mistake:

extension NSEvent {
    var character: Int {
        return Int(charactersIgnoringModifiers!.utf16[0])
    }
}
pomo_mondreganto
  • 2,028
  • 2
  • 28
  • 56

1 Answers1

1

String.UTF16View is a CollectionType, therefore you can get the index of the first element by using its startIndex property:

extension Character {
    var keyCode: Int {
        let utf16view = String(self).utf16
        return Int(utf16view[utf16view.startIndex])
    }
}

(I don't have Xcode 6.2 anymore on my computer, therefore I cannot explain why your code compiled before.)

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382