Pārlūkot izejas kodu

Added URL initializer extension for URLStringConvertible types that throws.

Christian Noon 9 gadi atpakaļ
vecāks
revīzija
279eba95b8
2 mainītis faili ar 17 papildinājumiem un 13 dzēšanām
  1. 3 9
      README.md
  2. 14 4
      Source/Alamofire.swift

+ 3 - 9
README.md

@@ -1076,14 +1076,10 @@ enum Router: URLRequestConvertible {
             }
         }()
 
-        let url = URL(string: Router.baseURLString)!
+        let url = try URL(urlString: Router.baseURLString)
         let urlRequest = URLRequest(url: url.appendingPathComponent(result.path))
 
-        do {
-            return try URLEncoding.default.encode(urlRequest, with: result.parameters)
-        } catch {
-            return urlRequest
-        }
+        return try URLEncoding.default.encode(urlRequest, with: result.parameters)
     }
 }
 ```
@@ -1134,9 +1130,7 @@ enum Router: URLRequestConvertible {
     // MARK: URLRequestConvertible
 
     func asURLRequest() throws -> URLRequest {
-    	guard let url = URL(string: Router.baseURLString) else {
-    	    throw AFError.invalidURLString(urlString: Router.baseURLString)
-    	}
+    	let url = try URL(urlString: Router.baseURLString)
 
         var urlRequest = URLRequest(url: url.appendingPathComponent(path))
         urlRequest.httpMethod = method.rawValue

+ 14 - 4
Source/Alamofire.swift

@@ -58,6 +58,19 @@ extension String: URLStringConvertible {
 extension URL: URLStringConvertible {
     /// Returns a URL string that conforms to RFC 2396.
     public func asURLString() throws -> String { return absoluteString }
+
+    /// Creates a URL with the specified URL string if possible, otherwise throws an `Error`.
+    ///
+    /// - parameter urlString: The URL string convertible to create the URL with.
+    ///
+    /// - throws: An `AFError.invalidURLString` if invalid.
+    ///
+    /// - returns: A URL if created successfully, otherwise throws an `AFError`.
+    public init(urlString: URLStringConvertible) throws {
+        let urlString = try urlString.asURLString()
+        guard let url = URL(string: urlString) else { throw AFError.invalidURLString(urlString: urlString) }
+        self = url
+    }
 }
 
 extension URLComponents: URLStringConvertible {
@@ -88,7 +101,6 @@ extension URLRequestConvertible {
     public var urlRequest: URLRequest? { return try? asURLRequest() }
 }
 
-
 extension URLRequest: URLRequestConvertible {
     /// Returns a URL request or throws if an `Error` was encountered.
     public func asURLRequest() throws -> URLRequest { return self }
@@ -105,9 +117,7 @@ extension URLRequest {
     ///
     /// - returns: The new `URLRequest` instance.
     public init(urlString: URLStringConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
-        let urlString = try urlString.asURLString()
-
-        guard let url = URL(string: urlString) else { throw AFError.invalidURLString(urlString: urlString) }
+        let url = try URL(urlString: urlString)
 
         self.init(url: url)