쉽게 들리지만 나는 혼란 스럽습니다. Range의 구문과 함수는 매우 혼란 스럽습니다.범위를 사용하여 문자열에서 구문을 추출하는 방법은 무엇입니까?
https://github.com/shakked/Command-for-Instagram/blob/master/Analytics%20Pro.md#global-best-time-to-post
내가 문자열의 끝 부분 #global-best-time-to-post
, 본질적으로 #
를 추출해야합니다
나는이 같은 URL이 있습니다.
urlString.rangeOfString("#")
반환 Range
은 그 때 나는 advanceBy(100)
를 호출하면 바로 문자열의 끝으로 이동하지만, 대신 충돌 것이라고 가정이 일을 시도했다.
import Foundation
let urlString = "https://github.com/shakked/Command-for-Instagram/blob/master/Analytics%20Pro.md#global-best-time-to-post"
// using NSURL - best option since it validates the URL
if let url = NSURL(string: urlString),
fragment = url.fragment {
print(fragment)
}
// output: "global-best-time-to-post"
// using split - pure Swift, no Foundation necessary
let split = urlString.characters.split("#")
if split.count > 1,
let fragment = split.last {
print(String(fragment))
}
// output: "global-best-time-to-post"
// using rangeofString - asked in the question
if let endOctothorpe = urlString.rangeOfString("#")?.endIndex {
// Note that I use the index of the end of the found Range
// and the index of the end of the urlString to form the
// Range of my string
let fragment = urlString[endOctothorpe..<urlString.endIndex]
print(fragment)
}
// output: "global-best-time-to-post"
내가 오해 질문 :( –