A section of code parsing for network requests with Alamofire (ii)

Source: Internet
Author: User

/*

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.