/*
Router Router, this enumeration type uses the urlrequestconvertible protocol. The type of this protocol is used to construct URL requests. This protocol has only one read-only property , URLRequest, which is of type nsurlrequest.
*/
enum Router: urlrequestconvertible {
Static Let baseurlstring = "Https://api.500px.com/v2"
static let consumerkey = "4qf9gguzhwjvfjmbvoqvpjjbbsjogv7vborsnrgt"
/*
in the Swift enumeration, the Popularphotos of the following example is called an enumeration value, and the value pointed to by the enumeration value is referred to as the associated value. The type in parentheses is the type of the associated value.
*/
Case Popularphotos (INT)
Case Photoinfo (Int, ImageSize)
Case Comments (int, int)
var URLRequest: nsurlrequest {
/*
This is the definition of a tuple.
Let (,) = {} () This statement means that the tuple (,) is initialized with the return value of a function; the parenthesis means that the function is immediately executed and the return value is assigned to the tuple.
*/
Let (path: string, Parameters: [string: anyobject]) = {
Switch Self {
case. Popularphotos ( let page):
Letparams = ["Consumer_key": Router. Consumerkey,"Page": "\ (page)", "Feature": "Popular", "RPP": "50", "Include_store": "Store_download", "Include_states": "Votes"]
return ("/photos", params)
case . Photoinfo (let photoid, let imageSize):
var params = [" Consumer_key ": router . Consumerkey, "iamge_size" : Span style= "font-variant-ligatures:no-common-ligatures; Color: #c91b13 ">" \ (Imagesize. RawValue ) "]
return ("/photos/\ (photoid)", params)
case . Comments (let photoid, let commentspage):
var params = [" Consumer_key ": router . Consumerkey, "comments" : " 1 ", " Comments_ Page ": " \ (commentspage) "]
return ("/photos/\ (photoid)/comments", params)
}
}()
Let URL = Nsurl(string: Router. baseurlstring)
Let URLRequest = nsurlrequest(url:url!. urlbyappendingpathcomponent (path))
Let encoding = Alamofire. parameterencoding. URL
/*
The encode function of the Parameterencoding enumeration type has no function body, so how is the return value implemented??
*/
return encoding. encode (URLRequest, Parameters:parameters). 0
}
}
A section of code parsing for network requests with Alamofire (ii)