This question already has an answer here:
I get compiler error at this line:
UIDevice.currentDevice().identifierForVendor.UUIDString.substringToIndex(8)
Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible'
My intention is to get the substring, but how?
In Swift,
String
indexing respects grapheme clusters, and anIndexType
is not anInt
. You have two choices - cast the string (your UUID) to an NSString, and use it as "before", or create an index to the nth character.Both are illustrated below :
However, the method has changed radically between versions of Swift. Read down for later versions...
Swift 1
CMD-Click on
substringToIndex
confusingly takes you to theNSString
definition, however CMD-Click onString
and you will find the following:Swift 2
As commentator @DanielGalasko points out
advance
has now changed...Swift 3
In Swift 3, it's changed again:
Swift 4
In Swift 4, yet another change: