やること
I like http://hoge.com and https://piyo.net.
この文章から URL を削除して
I like and .
にしたいと思います。
Playground
import UIKit
var input = "I like http://hoge.com and https://piyo.net."
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
var urls: [String] = []
for match in matches {
guard let range = Range(match.range, in: input) else { continue }
let url = input[range]
urls.append(String(url))
}
print(urls)
urls.forEach{ url in
input = input.replacingOccurrences(of: url, with: "")
}
print(input)
output
["http://hoge.com", "https://piyo.net"]
I like and .
できました。