'advancedBy' is unavailable in Xcode 8 [closed]
Asked Answered
J

2

14

Currently, I've been using XCode 8 and on the line

let range = key.startIndex...key.startIndex.advancedBy(0)

I get the error:

error: 'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the CharacterView instance that produced the index.

How can I fix this?

A screenshot of the error is below:

advancedBy is unavailable

Jolin answered 19/7, 2016 at 4:0 Comment(3)
Please include your code in the question as well as the screenshot.Marbut
You need to use index offsetBy https://mcmap.net/q/23461/-get-nth-character-of-a-string-in-swiftPraline
See github.com/apple/swift-evolution/blob/master/proposals/…Dulcinea
P
23

advancedBy(_:) has been deprecated in Swift v3 and replaced by index(_:offsetBy:). Refer to Apple's migration guide for more info.

The solution to your error:

let range = key.startIndex...key.index(key.startIndex, offsetBy: 0)
Phylum answered 3/9, 2016 at 5:27 Comment(0)
M
0

You can achieve what you are after more simply by building a new string rather than manipulating the old one:

let upperCasedFirstCharacter = String(key.characters.first!).uppercased()

let remainder = key.substring(from: key.characters.index(after: key.characters.startIndex))

let selector = "set\(upperCasedFirstCharacter)\(remainder)"
Marbut answered 19/7, 2016 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.