iOS Swift: Cannot Manipulate String: Value of type 'String' has no member 'firstIndex' [duplicate]
Clash Royale CLAN TAG#URR8PPP
iOS Swift: Cannot Manipulate String: Value of type 'String' has no member 'firstIndex' [duplicate]
This question already has an answer here:
Hi I am trying to manipulate a String, though the code from Apples developer Site does not work:
let name = "Marie Curie"
let firstSpace = name.firstIndex(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]
// firstName == "Marie"
You can find the example here: https://developer.apple.com/documentation/swift/string
Seems like firstIndex(of: " ")
does not exist any longer
firstIndex(of: " ")
I don't understand why this is not working is there a new func with a different name or what could be the problem? I am confused did Apple missed this one and nobody needed it so far???
Thanks a lot
There is a question that is similar though its answered with index(of: Character) though I am looking for something like index(of: String)
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
got version 9.4.1
– SirJoe
Aug 6 at 14:01
See Carlos' answer for a fix along your original intention; if you only want to use this to get first/last name from a string you might also be interested in
name.split(separator: " ")
which returns ["Marie", "Curie"]
.– dr_barto
Aug 6 at 14:16
name.split(separator: " ")
["Marie", "Curie"]
no actually its a little more complex, I could not do it that way, I just took this as an easy example
– SirJoe
Aug 6 at 14:28
2 Answers
2
it all depends on what version of swift you are using, the continuous improvement of the swift language , includes syntax changes
replace firstIndex(of:
to .index(of:
firstIndex(of:
.index(of:
let name = "Marie Curie"
let firstSpace = name.index(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]
Yeah I tried that too but I am getting an error there "Cannot convert value of type 'String' to expected argument type 'Character" so seems like this is only available for Character and not for Strings
– SirJoe
Aug 6 at 14:27
what version of xcode' and swift do you have in your project
– Carlos Andres Chaguendo
Aug 6 at 15:36
Xcode: 9.4.1 and Swift: 4.1
– SirJoe
Aug 6 at 16:32
Well there's your answer @SirJoe. You are looking at the docs for Xcode 10 but you are using Xcode 9.
– matt
Aug 6 at 17:26
ah okay, then what was the way you did it before that (I don't wanna download Xcode 10 if not necessary since its still in beta)
– SirJoe
Aug 6 at 17:48
let string = "Marie Curie"
let firstCharIndex = string.index(string.startIndex, offsetBy: 1)
let firstChar = string.substring(to: firstCharIndex)
print(firstChar)
the request is to search for a string this would not solve the problem in my case since the string I have to manipulate is unknown and could be anything I just know for sure that it contains some string sequence
– SirJoe
Aug 6 at 14:02
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
– Nic3500
Aug 7 at 0:26
developer.apple.com/documentation/swift/string/… Check your Xcode version
– rv7284
Aug 6 at 13:53