Swift3 從入門到精通Day24:使用者產生器(2/2)

Alice
Alice
Jul 23, 2017 · 3 min read

時間:7/21(五)

作品:連接網路資料API隨機選擇的APP

連接資料有兩種方式:downloadWithData、downWithURLSession


downloadWithData:把API連接上程式碼

但要先處理一些問題


下載工作都要在共時佇列裡才不會卡到畫面

DispatchQueue.global().async

確定是否有連接網路

拉到專案資料夾中

使用下載圖片那堂課時使用的reachability類別

確定是否有連接網路

else的部分可以用print但我們可以做一個alert

沒有連接網路的狀況下就會顯示No Internet

第二種方式downWithURLSession:


JSON就是Dictionary跟Array

Dictionary就是以下形式

{

name:”Thomas”,

id:”9428”

}

Array就是以下形式

[“apple”,”banana”]

dictionary用key取值時會拿到Array

用JSON取值

終於終於抓到網路資料可以用拉!!!!!!
終於有圖了
有圖片了拉!

Daily Swift

Hi, I’m Alice. :-]

Alice

Written by

Alice

Swift addict, Taipei|wl02722691@gmail.com

Daily Swift

Hi, I’m Alice. :-]

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade