metatext/Shared/Extensions/String+Extensions.swift

22 lines
455 B
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
extension String {
2020-08-09 20:40:14 +00:00
private static let HTTPSPrefix = "https://"
2020-08-09 20:40:14 +00:00
func url() throws -> URL {
let url: URL?
2020-08-09 20:40:14 +00:00
if hasPrefix(Self.HTTPSPrefix) {
url = URL(string: self)
} else {
2020-08-09 20:40:14 +00:00
url = URL(string: Self.HTTPSPrefix + self)
}
guard let validURL = url else { throw URLError(.badURL) }
return validURL
}
}