-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
475 lines (282 loc) · 32.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hjj博客</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="hjj博客">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="hjj博客">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="hjj博客">
<meta name="twitter:description">
<link rel="alternative" href="/atom.xml" title="hjj博客" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link rel="stylesheet" href="/css/style.css" type="text/css">
</head>
<body>
<div id="container">
<div class="left-col">
<div class="overlay"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img lazy-src="http://d.pcs.baidu.com/thumbnail/7c50d7ea9685e2d9f18a41b601aa7a8c?fid=3191109153-250528-611139016885341&time=1438056000&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-hcEj5YVeYwXKvSj1%2FdDBUxewK8s%3D&rt=sh&expires=2h&r=295772138&sharesign=unknown&size=c710_u500&quality=100" class="js-avatar">
</a>
<hgroup>
<h1 class="header-author"><a href="/">hjj博客</a></h1>
</hgroup>
<div class="switch-btn">
<div class="icon">
<div class="icon-ctn">
<div class="icon-wrap icon-house" data-idx="0">
<div class="birdhouse"></div>
<div class="birdhouse_holes"></div>
</div>
<div class="icon-wrap icon-ribbon hide" data-idx="1">
<div class="ribbon"></div>
</div>
<div class="icon-wrap icon-link hide" data-idx="2">
<div class="loopback_l"></div>
<div class="loopback_r"></div>
</div>
<div class="icon-wrap icon-me hide" data-idx="3">
<div class="user"></div>
<div class="shoulder"></div>
</div>
</div>
</div>
<div class="tips-box hide">
<div class="tips-arrow"></div>
<ul class="tips-inner">
<li>菜单</li>
<li>标签</li>
<li>友情链接</li>
<li>关于我</li>
</ul>
</div>
</div>
<div class="switch-area">
<div class="switch-wrap">
<section class="switch-part switch-part1">
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="https://github.com/hjjwinner" title="github">github</a>
<a class="weibo" target="_blank" href="/#" title="weibo">weibo</a>
<a class="mail" target="_blank" href="/[email protected]" title="mail">mail</a>
</div>
</nav>
</section>
<section class="switch-part switch-part2">
<div class="widget tagcloud" id="js-tagcloud">
<a href="/tags/Swift/" style="font-size: 10px;">Swift</a> <a href="/tags/我/" style="font-size: 10px;">我</a>
</div>
</section>
<section class="switch-part switch-part3">
<div id="js-friends">
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">奥巴马的博客</a>
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">卡卡的美丽传说</a>
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">本泽马的博客</a>
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">吉格斯的博客</a>
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">习大大大不同</a>
<a target="_blank" class="main-nav-link switch-friends-link" href="http://localhost:4000/">托蒂的博客</a>
</div>
</section>
<section class="switch-part switch-part4">
<div id="js-aboutme">只要我想,没有做不到!</div>
</section>
</div>
</div>
</header>
</div>
</div>
<div class="mid-col">
<nav id="mobile-nav">
<div class="overlay">
<div class="slider-trigger"></div>
<h1 class="header-author js-mobile-header hide">hjj博客</h1>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<div class="profilepic">
<img lazy-src="http://d.pcs.baidu.com/thumbnail/7c50d7ea9685e2d9f18a41b601aa7a8c?fid=3191109153-250528-611139016885341&time=1438056000&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-hcEj5YVeYwXKvSj1%2FdDBUxewK8s%3D&rt=sh&expires=2h&r=295772138&sharesign=unknown&size=c710_u500&quality=100" class="js-avatar">
</div>
<hgroup>
<h1 class="header-author">hjj博客</h1>
</hgroup>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
<div class="clearfix"></div>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="https://github.com/hjjwinner" title="github">github</a>
<a class="weibo" target="_blank" href="/#" title="weibo">weibo</a>
<a class="mail" target="_blank" href="/[email protected]" title="mail">mail</a>
</div>
</nav>
</header>
</div>
</nav>
<div class="body-wrap">
<article id="post-hjjnew" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/07/27/hjjnew/" class="article-date">
<time datetime="2015-07-27T08:57:32.000Z" itemprop="datePublished">2015-07-27</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/07/27/hjjnew/">IOS网络操作-使用Alamofire和ObjectMapper处理JSON转对象</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h1 id="Alamofire是一个使用Swift语言写的一个网络库,操作很简单,结合ObjectMapper、AlamofireObjectMapper,可以轻松的将JSON数据转换为对象类型!">Alamofire是一个使用Swift语言写的一个网络库,操作很简单,结合ObjectMapper、AlamofireObjectMapper,可以轻松的将JSON数据转换为对象类型!</h1><h1 id="Alamofire:https://github-com/Alamofire/Alamofire">Alamofire:<a href="https://github.com/Alamofire/Alamofire" target="_blank" rel="external">https://github.com/Alamofire/Alamofire</a></h1><h1 id="ObjectMapper:https://github-com/Hearst-DD/ObjectMapper">ObjectMapper:<a href="https://github.com/Hearst-DD/ObjectMapper" target="_blank" rel="external">https://github.com/Hearst-DD/ObjectMapper</a></h1><h1 id="ALamoObjectmapper:https://github-com/tristanhimmelman/AlamofireObjectMapper">ALamoObjectmapper:<a href="https://github.com/tristanhimmelman/AlamofireObjectMapper" target="_blank" rel="external">https://github.com/tristanhimmelman/AlamofireObjectMapper</a></h1><h1 id="假设有个地址返回如下JSON数据:">假设有个地址返回如下JSON数据:</h1><h2 id="代码块">代码块</h2><figure class="highlight json"><figcaption><span>.compact</span><a href="http://underscorejs.org/#compact" target="_blank" rel="external">Underscore.js</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> "<span class="attribute">meta</span>":</span><br><span class="line"> <span class="value">{</span><br><span class="line"> "<span class="attribute">total</span>": <span class="value"><span class="string">"16868"</span></span>,</span><br><span class="line"> "<span class="attribute">time</span>": <span class="value"><span class="string">"0.435"</span></span>,</span><br><span class="line"> "<span class="attribute">limit</span>": <span class="value"><span class="string">"10"</span></span>,</span><br><span class="line"> "<span class="attribute">offset</span>": <span class="value"><span class="number">0</span></span>,</span><br><span class="line"> "<span class="attribute">currency</span>": <span class="value"><span class="string">"CNY"</span></span>,</span><br><span class="line"> "<span class="attribute">symbol</span>": <span class="value"><span class="string">"¥"</span></span><br><span class="line"> </span>}</span>,</span><br><span class="line"> "<span class="attribute">products</span>": <span class="value">[</span><br><span class="line"> {</span><br><span class="line"> "<span class="attribute">time_updated</span>": <span class="value"><span class="string">"1432808931"</span></span>,</span><br><span class="line"> "<span class="attribute">commission</span>": <span class="value"><span class="string">"363.42"</span></span>,</span><br><span class="line"> "<span class="attribute">price</span>": <span class="value"><span class="string">"6,056.92"</span></span>,</span><br><span class="line"> "<span class="attribute">sku</span>": <span class="value"><span class="string">"517569"</span></span>,</span><br><span class="line"> "<span class="attribute">upc</span>": <span class="value"><span class="string">""</span></span>,</span><br><span class="line"> "<span class="attribute">pweight</span>": <span class="value"><span class="literal">null</span></span>,</span><br><span class="line"> "<span class="attribute">product_id</span>": <span class="value"><span class="string">"82808bcd27eeb65dd15740aee40ec07c"</span></span>,</span><br><span class="line"> "<span class="attribute">product_image</span>": <span class="value"><span class="string">"http://images.rewardstyle.com/img?v=1.3&p=82808bcd27eeb65dd15740aee40ec07c"</span></span>,</span><br><span class="line"> "<span class="attribute">product_type</span>": <span class="value"><span class="string">"1"</span></span>,</span><br><span class="line"> "<span class="attribute">product_name</span>": <span class="value"><span class="string">"Cape-back crepe and chiffon mini dress"</span></span>,</span><br><span class="line"> "<span class="attribute">advertiser</span>": <span class="value"><span class="string">"NET-A-PORTER UK"</span></span>,</span><br><span class="line"> "<span class="attribute">designer</span>": <span class="value"><span class="string">"Gareth Pugh"</span></span>,</span><br><span class="line"> "<span class="attribute">category</span>": <span class="value"><span class="string">"WOMEN->Clothing->Dresses"</span></span><br><span class="line"> </span>},</span><br><span class="line"> {</span><br><span class="line"> "<span class="attribute">time_updated</span>": <span class="value"><span class="string">"1432808931"</span></span>,</span><br><span class="line"> "<span class="attribute">commission</span>": <span class="value"><span class="string">"1,359.23"</span></span>,</span><br><span class="line"> "<span class="attribute">price</span>": <span class="value"><span class="string">"22,653.85"</span></span>,</span><br><span class="line"> "<span class="attribute">sku</span>": <span class="value"><span class="string">"549499"</span></span>,</span><br><span class="line"> "<span class="attribute">upc</span>": <span class="value"><span class="string">""</span></span>,</span><br><span class="line"> "<span class="attribute">pweight</span>": <span class="value"><span class="literal">null</span></span>,</span><br><span class="line"> "<span class="attribute">product_id</span>": <span class="value"><span class="string">"e147fbbf30c65538b367f3a6ab539252"</span></span>,</span><br><span class="line"> "<span class="attribute">product_image</span>": <span class="value"><span class="string">"http://images.rewardstyle.com/img?v=1.3&p=e147fbbf30c65538b367f3a6ab539252"</span></span>,</span><br><span class="line"> "<span class="attribute">product_type</span>": <span class="value"><span class="string">"1"</span></span>,</span><br><span class="line"> "<span class="attribute">product_name</span>": <span class="value"><span class="string">"Woven suede tote"</span></span>,</span><br><span class="line"> "<span class="attribute">advertiser</span>": <span class="value"><span class="string">"NET-A-PORTER UK"</span></span>,</span><br><span class="line"> "<span class="attribute">designer</span>": <span class="value"><span class="string">"Loewe"</span></span>,</span><br><span class="line"> "<span class="attribute">category</span>": <span class="value"><span class="string">"WOMEN->Bags->Tote"</span></span><br><span class="line"> </span>}</span><br><span class="line"> ]</span><br><span class="line"></span>}</span><br></pre></td></tr></table></figure>
<h1 id="使用对象的概念来解读这段JSON数据,它包含Meta和Product:Meta是一个对象,Products是一个数组类型,包含了多个Product对象。">使用对象的概念来解读这段JSON数据,它包含Meta和Product:Meta是一个对象,Products是一个数组类型,包含了多个Product对象。</h1><h1 id="我们首先使用ObjectMapper库定义的规则,来写一个实体类。">我们首先使用ObjectMapper库定义的规则,来写一个实体类。</h1><figure class="highlight swift"><figcaption><span>.compact</span><a href="http://underscorejs.org/#compact" target="_blank" rel="external">Underscore.js</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Meta</span> : <span class="title">Mappable</span></span>{</span><br><span class="line"> <span class="keyword">var</span> total:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> time:<span class="type">Double</span>?</span><br><span class="line"> <span class="keyword">var</span> limit:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> offset:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> currency:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> symbol:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">init</span>(){}</span><br><span class="line"> required <span class="keyword">init</span>?(<span class="number">_</span> <span class="built_in">map</span>: <span class="type">Map</span>){</span><br><span class="line"> mapping(<span class="built_in">map</span>)</span><br><span class="line"> }</span><br><span class="line"> <span class="func"><span class="keyword">func</span> <span class="title">mapping</span><span class="params">(<span class="built_in">map</span>: Map)</span></span> {</span><br><span class="line"> total <- <span class="built_in">map</span>[<span class="string">"total"</span>]</span><br><span class="line"> time <- <span class="built_in">map</span>[<span class="string">"time"</span>]</span><br><span class="line"> limit <- <span class="built_in">map</span>[<span class="string">"limit"</span>]</span><br><span class="line"> offset <- <span class="built_in">map</span>[<span class="string">"offset"</span>]</span><br><span class="line"> currency <- <span class="built_in">map</span>[<span class="string">"currency"</span>]</span><br><span class="line"> symbol <- <span class="built_in">map</span>[<span class="string">"symbol"</span>]</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product</span> : <span class="title">Mappable</span></span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">var</span> time_updated:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> commission:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> price:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> sku:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> product_id:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> product_image:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> product_name:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> advertiser:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> designer:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">var</span> category:<span class="type">String</span>?</span><br><span class="line"> <span class="keyword">init</span>(){}</span><br><span class="line"> </span><br><span class="line"> required <span class="keyword">init</span>?(<span class="number">_</span> <span class="built_in">map</span>: <span class="type">Map</span>) {</span><br><span class="line"> mapping(<span class="built_in">map</span>)</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="func"><span class="keyword">func</span> <span class="title">mapping</span><span class="params">(<span class="built_in">map</span>: Map)</span></span> {</span><br><span class="line"> time_updated <- <span class="built_in">map</span>[<span class="string">"prtime_updated"</span>]</span><br><span class="line"> commission <- <span class="built_in">map</span>[<span class="string">"age"</span>]</span><br><span class="line"> price <- <span class="built_in">map</span>[<span class="string">"price"</span>]</span><br><span class="line"> sku <- <span class="built_in">map</span>[<span class="string">"sku"</span>]</span><br><span class="line"> product_id <- <span class="built_in">map</span>[<span class="string">"product_id"</span>]</span><br><span class="line"> product_image <- <span class="built_in">map</span>[<span class="string">"product_image"</span>]</span><br><span class="line"> product_name <- <span class="built_in">map</span>[<span class="string">"product_name"</span>]</span><br><span class="line"> advertiser <- <span class="built_in">map</span>[<span class="string">"advertiser"</span>]</span><br><span class="line"> designer <- <span class="built_in">map</span>[<span class="string">"designer"</span>]</span><br><span class="line"> category <- <span class="built_in">map</span>[<span class="string">"category"</span>]</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h1 id="每个实体类都实现了Mappable协议。这仅仅是JSON数据中的两个对象,现在我们要再声明下整个JSON数据的对象定义,它将作为顶层包含着Meta类和Product类:">每个实体类都实现了Mappable协议。这仅仅是JSON数据中的两个对象,现在我们要再声明下整个JSON数据的对象定义,它将作为顶层包含着Meta类和Product类:</h1><figure class="highlight swift"><figcaption><span>.compact</span><a href="http://underscorejs.org/#compact" target="_blank" rel="external">Underscore.js</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ProductResponse</span>:<span class="title">Mappable</span></span>{</span><br><span class="line"> <span class="keyword">var</span> meta:<span class="type">Meta</span>?</span><br><span class="line"> <span class="keyword">var</span> products:[<span class="type">Product</span>]?</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">init</span>(){}</span><br><span class="line"> required <span class="keyword">init</span>?(<span class="number">_</span> <span class="built_in">map</span>: <span class="type">Map</span>) {</span><br><span class="line"> mapping(<span class="built_in">map</span>)</span><br><span class="line"> }</span><br><span class="line"> <span class="func"><span class="keyword">func</span> <span class="title">mapping</span><span class="params">(<span class="built_in">map</span>: Map)</span></span> {</span><br><span class="line"> meta <- <span class="built_in">map</span>[<span class="string">"meta"</span>]</span><br><span class="line"> products <- <span class="built_in">map</span>[<span class="string">"products"</span>]</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h1 id="接下来,就可以使用Alamofire来操作了。这时候,可以使用AlamofireObjectMapper库,它使用extension扩展了Alamofire的response方法:">接下来,就可以使用Alamofire来操作了。这时候,可以使用AlamofireObjectMapper库,它使用extension扩展了Alamofire的response方法:</h1><figure class="highlight oxygene"><figcaption><span>.compact</span><a href="http://underscorejs.org/#compact" target="_blank" rel="external">Underscore.js</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Alamofire.request(<span class="function"><span class="keyword">Method</span>.<span class="title">GET</span>, "请求的地址", <span class="title">parameters</span>:</span> <span class="keyword">nil</span>, encoding: ParameterEncoding.URL).responseObject <span class="comment">{ (response:ProductResponse?, error:NSError?) -> Void in</span><br><span class="line"> if let products = response?.products{</span><br><span class="line"> for p in products{</span><br><span class="line"> println(p.product_name)</span><br><span class="line"> }</span></span><br><span class="line"> }</span><br><span class="line">}<span class="comment">// end request</span></span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Swift/">Swift</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/我/">我</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/Swift/">Swift</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-My-New-Post" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/07/27/My-New-Post/" class="article-date">
<time datetime="2015-07-27T08:57:20.000Z" itemprop="datePublished">2015-07-27</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/07/27/My-New-Post/">My New Post</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-Hello-Hexo" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/07/27/Hello-Hexo/" class="article-date">
<time datetime="2015-07-27T08:10:12.000Z" itemprop="datePublished">2015-07-27</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/07/27/Hello-Hexo/">Hello Hexo</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-hello-world" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2015/07/27/hello-world/" class="article-date">
<time datetime="2015-07-27T08:07:00.000Z" itemprop="datePublished">2015-07-27</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2015/07/27/hello-world/">Hello World</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Welcome to <a href="http://hexo.io/" target="_blank" rel="external">Hexo</a>! This is your very first post. Check <a href="http://hexo.io/docs/" target="_blank" rel="external">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="http://hexo.io/docs/troubleshooting.html" target="_blank" rel="external">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="external">GitHub</a>.</p>
<h2 id="Quick_Start">Quick Start</h2><h3 id="Create_a_new_post">Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/writing.html" target="_blank" rel="external">Writing</a></p>
<h3 id="Run_server">Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/server.html" target="_blank" rel="external">Server</a></p>
<h3 id="Generate_static_files">Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/generating.html" target="_blank" rel="external">Generating</a></p>
<h3 id="Deploy_to_remote_sites">Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/deployment.html" target="_blank" rel="external">Deployment</a></p>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info">
<div class="footer-left">
© 2015 hjj博客
</div>
<div class="footer-right">
<a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/litten/hexo-theme-yilia" target="_blank">Yilia</a> by Litten
</div>
</div>
</div>
</footer>
</div>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
<script>
var yiliaConfig = {
fancybox: true,
mathjax: true,
animate: true,
isHome: true,
isPost: false,
isArchive: false,
isTag: false,
isCategory: false,
open_in_new: false
}
</script>
<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="/js/main.js" type="text/javascript"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</div>
</body>
</html>