Swift3.0 Cannot convert value of type 'ClosedRange<Index>' to type 'Range<Index>' Swift3.0 Cannot convert value of type 'ClosedRange<Index>' to type 'Range<Index>' xcode xcode

Swift3.0 Cannot convert value of type 'ClosedRange<Index>' to type 'Range<Index>'


You can use ..< instead of ... for range to be of type Range<Index> instead of ClosedRange<Index>, in which case the call to stringByReplacingCharactersInRange(...) wont yield an error (notice the offsetBy increase by 1).

let range = key.startIndex..<key.index(key.startIndex, offsetBy: 1)// range is now type Range<Index>

Now, I might be wrong, but it seems as if you simply want the selectorString to be the version of key with the first character uppercased. An alternative method to your range solution you can e.g. use a String extension solution as follows:

extension String {    var firstCharacterUppercased: String {        guard case let c = self.characters,                   let c1 = c.first else { return self }        return String(c1).uppercased() + String(c.dropFirst())    }}/* example usage */let key = "fooBar"let selectorString = key.firstCharacterUppercasedprint(selectorString) // FooBar


Swift 3+

override func setValue(_ value: Any?, forKey key: String) {        let upperCaseFirstCharacter = String(key.characters.first!).uppercased()        let range = key.startIndex..<key.index(key.startIndex, offsetBy: 1)        let selectorString = key.replacingCharacters(in: range, with: upperCaseFirstCharacter)        let selector = NSSelectorFromString("set\(selectorString):")        let responds = self.responds(to: selector)        if !responds{            return        }        super.setValue(value, forKey: key)    }    init(dictionary: [String: Any]){        super.init()        setValuesForKeys(dictionary)    }