0

as you know there are few changes in xcode 7 and swift 2. I got 2 errors shown below, how can I fix them? Thanks

extension String {
    var wordList:[String] {
        return "".join(componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet())).componentsSeparatedByString(" ")
    }
    var first: String {
        return String(self[startIndex])
    }
    var last: String {
        return String(self[endIndex.predecessor()])
    }
    var scrambleMiddle: String {
        if count(self) < 4 {   //'(String) -> _' is not identical  to 'Int'
            return self
        }
        return first + String(Array(dropLast(dropFirst(self))).shuffled) + last   //Type 'String' does not conform to protocol 'Sliceable'
    }
}
Encul
  • 93
  • 1
  • 10

2 Answers2

0

'(String) -> _' is not identical to 'Int' is because String does not conform to SequenceType anymore, instead you have to get the characters property then call the count method on it:

if self.characters.count < 4 {
    return self
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

As of Swift 2 String is no longer a CollectionType and Sliceable so you should perform these actions on the CharacterView of it with .characters:

var scrambleMiddle: String {
    if self.characters.count < 4 {
        return self
    }
    return first + String(Array(dropLast(dropFirst(self.characters))).shuffled) + last
}
Qbyte
  • 12,753
  • 4
  • 41
  • 57