<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alice Lee on Medium]]></title>
        <description><![CDATA[Stories by Alice Lee on Medium]]></description>
        <link>https://medium.com/@alice812?source=rss-70b2d2ddce1d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*G0-xXatAFitFi_WvQ9DQuQ@2x.jpeg</url>
            <title>Stories by Alice Lee on Medium</title>
            <link>https://medium.com/@alice812?source=rss-70b2d2ddce1d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:26:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alice812/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[得正訂飲料 App｜Part 1:串接 Airtable API]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%BE%97%E6%AD%A3%E8%A8%82%E9%A3%B2%E6%96%99-app-part-1-%E4%B8%B2%E6%8E%A5-airtable-api-bc6f173e60a9?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc6f173e60a9</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Sat, 01 Jul 2023 07:32:42 GMT</pubDate>
            <atom:updated>2023-08-14T08:43:14.484Z</atom:updated>
            <content:encoded><![CDATA[<h3>#6 得正訂飲料 App｜Part 1: 串接 Airtable API</h3><p>前陣子一直在網路上看到得正這間飲料店，朋友也說好喝，有一次剛好看到並買來喝喝看，結果真的很不錯！於是決定來挑戰看看，練習串接後台 API，新增讀取後台資料。</p><blockquote><strong><em>◾️功能應用</em></strong></blockquote><p>◾️ 檢視飲料 menu，將 menu 存在 Airtable，透過 API 讀取 JSON 。</p><p>◽️新增訂購的飲料 (可選擇甜度、冰塊、大小及配料)</p><p>◽️訂單上傳至Airtable</p><p>◽️訂單可修改編輯或刪除</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UJhjf8-tWRtQwpTDPcm-Fw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*yj4XjYzuOLdQf5IcuWcLYg.gif" /></figure><blockquote><strong><em>以Airtable 為後台，串接 API 抓取 JSON 資料</em></strong></blockquote><p><strong>1.註冊並建立</strong>飲料Menu資料庫</p><p><a href="https://airtable.com/">The platform to build next‒gen apps | Airtable</a></p><p>→ 將需要的欄位資料建立在飲料Ｍenu中：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rv6kCPGTJCJCxthZpi2sAw.png" /></figure><p><strong>2.連結API頁面</strong></p><p><a href="https://airtable.com/developers/web/api/introduction">Airtable | Everyone&#39;s app platform</a></p><p>→ <strong>選取要查看的</strong>Workspace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aBM7xxllFWILHNorJy6r7Q.png" /></figure><p>→ 根據 AUTHENTICATION 頁面說明，建議使用<a href="https://airtable.com/developers/web/guides/personal-access-tokens"><strong>personal access tokens</strong></a>，在<a href="https://airtable.com/create/tokens"><strong>/create/tokens</strong></a>產生Secret API Token，以利後續串接API。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QDUzK8hZQGl0FRGAKWD4Jw.png" /></figure><p>→ 填寫好token的名稱、選擇需要的scope與access，然後點選Creat token。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*70LrDR4lOPp7RyRuLZohxQ.png" /></figure><p>→ 記得另存以下token，因為只會顯示一次。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*IOPfnRTtdzo46zD4ZeYcyQ.png" /></figure><p>🌟🌟🌟備註🌟🌟🌟<br>官方說明API key將於2024年1月底不再支援使用，建議使用 personal access tokens 和 OAuth access tokens。<br><a href="https://airtable.com/developers/web/api/authentication">https://airtable.com/developers/web/api/authentication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OPhi43mhuPnLjKGB8_5-RQ.png" /></figure><p><strong>3.以Postman檢查Airtable後台功能是否正常</strong></p><p>→ 將Authorization頁面的API 的網址貼在欄位1中，並選擇GET取得資料。<br>→ 欄位2的Type 選 “Bearer Token”<br>→ 將另存的token貼在欄位3中</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t4zEsYfKXfVPdzeBbTkQow.png" /></figure><p>→ 接著就可以在下方見到Json格式的資料</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A2zWaaUsd-QCgXJac5VjNQ.png" /></figure><blockquote><strong><em>程式撰寫</em></strong></blockquote><p><strong>🔸透過Codable將JSON物件轉換成自訂的型別</strong></p><pre>import Foundation<br><br>struct MenuResponse: Codable {<br>    let records: [Record]<br>}<br>struct Record: Codable {<br>    let id: String<br>    let fields: Field<br>}<br>        <br>struct Field: Codable {<br>    let classification: String<br>    let name: String<br>    let medium: Int<br>    let large: String<br>}</pre><p><strong>(Menu)TableViewCell</strong></p><p>→ Table View Cell設定ID，style選擇custom</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*DcBqWYbboWbgEHs0L3r9HA.png" /></figure><p>→ 將要顯示於cell中的元件拉<em>@IBOutlet</em> 在Table View Cell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2pVq3ZXDQDLCKtupqWKbJA.png" /></figure><p><strong>(Menu)TableViewController</strong></p><p>→ 定義抓資料的fetchMenu</p><pre>    //定義抓資料的fetchMenu<br>    func fetchMenu() {<br>        let urlString = &quot;https://api.airtable.com/v0/appK8yMkSmXCX0Hg6/Menu&quot;<br>        if let url = URL(string: urlString) {<br>            var request = URLRequest(url: url)<br>            request.setValue(&quot;Bearer patKwiqVg9pn00EPC.e03c8fc0b41480f7ce9b57d84039979edc288edf570304bbee3c89bb00a3f2a9&quot;, forHTTPHeaderField: &quot;Authorization&quot;)<br>            //{}的程式是closure，資料下載完成時會執行{}的程式，傳入data(抓到的資料),response(後台回傳抓資料的結果),error(錯誤資訊)三個參數。<br>            URLSession.shared.dataTask(with: request) { data, response , error in<br>                if let data {<br>                    let decoder = JSONDecoder()<br>                    do {<br>                        //透過decode將Data轉成對應的物件內容<br>                        let menuResponse = try decoder.decode(MenuResponse.self, from: data)<br>                        //需在main thread執行UI相關的程式<br>                        DispatchQueue.main.async {<br>                            self.allDrinks = menuResponse.records<br>                            for drink in self.allDrinks {<br>                                switch drink.fields.classification {<br>                                case &quot;#Original TEA 原茶系列&quot;:<br>                                    self.teaDrinks.append(drink)<br>                                case &quot;#Classic MILK TEA 經典奶茶&quot;:<br>                                    self.milkteaDrinks.append(drink)<br>                                case &quot;#Double FRUIT 雙重水果&quot;:<br>                                    self.fruitDrinks.append(drink)<br>                                case &quot;#Fresh MILK 鮮奶系列&quot;:<br>                                    self.milkDrinks.append(drink)<br>                                case &quot;#Cheese MILK FOAM 芝士奶蓋&quot;:<br>                                    self.cheeseDrinks.append(drink)<br>                                default:<br>                                    break<br>                                }<br>                            }<br>                            <br>                            //載完資料後filteredDrinks先出現的是原萃系列的品項<br>                            self.filteredDrinks = self.teaDrinks<br>                            //如果沒有reload data，表格不會更新，只會看到一片空白<br>                            self.tableView.reloadData()<br>                        }<br>                        print(&quot;get data&quot;)<br>                    } catch {<br>                        print(error)<br>                    }<br>                } else {<br>                    print(&quot;下載失敗&quot;)<br>                }<br>            }.resume()<br>            print(&quot;function dataTask執行完會先回傳task，然後呼叫task的resume啟動它&quot;)<br>        }<br>    }</pre><p>→ 建立array存放資料</p><pre>    //宣告儲存圖片的drinksPicture<br>    let drinksPicture = [&quot;tea&quot;, &quot;milk tea&quot;, &quot;fruit&quot;, &quot;milk&quot;, &quot;cheese&quot;]<br>    <br>    //宣告變數儲存對應的影片<br>    var filteredDrinks = [Record]()<br>    var allDrinks = [Record]()<br>    var teaDrinks = [Record]()<br>    var milkteaDrinks = [Record]()<br>    var fruitDrinks = [Record]()<br>    var milkDrinks = [Record]()<br>    var cheeseDrinks = [Record]()<br><br>    //宣告index變數儲存並判斷對應的內容<br>    var index = 0</pre><p>→ segmentedControl拉@IBOutlet與@IBAction，當點選不同的segment時，會更新表格對應的內容，顯示不同類別的飲料品項。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uoP_4R6GQhLztxioC1EiWw.png" /></figure><p>→ 表格顯示資料</p><ul><li>表格有幾段 (此次只有一段)</li></ul><pre>    //表格有幾段<br>    override func numberOfSections(in tableView: UITableView) -&gt; Int {<br>        return 1<br>    }</pre><ul><li>每一段表格有幾列</li></ul><pre>    //每一段表格有幾列<br>    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br>        return filteredDrinks.count<br>    }</pre><ul><li>回傳哪一個cell，可從參數 indexPath 得到 section &amp; row</li></ul><pre>    //回傳哪一個cell，可從參數 indexPath 得到 section &amp; row<br>    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {<br>        let cell = tableView.dequeueReusableCell(withIdentifier: &quot;MenuTableViewCell&quot;, for: indexPath) as! MenuTableViewCell<br>        let drink = filteredDrinks[indexPath.row]<br>        //顯示飲料名稱<br>        cell.nameLabel.text = drink.fields.name<br>        //顯示中杯飲料價位，mPrice型別為Int<br>        cell.mPrice.text = &quot;$\(drink.fields.medium)&quot;<br>        //顯示中杯飲料價位，lPrice型別為String<br>        cell.lPrice.text = &quot;$&quot;+drink.fields.large<br><br>        return cell<br>    }</pre><p>→ 建立與更新畫面有關的函式，後面可以不用一直重複寫</p><pre>func updateUI() {<br>        //根據index顯示對應的圖片<br>        drinkImageView.image = UIImage(named: drinksPicture[index])<br>        //當前的PageControl會根據index而變換<br>        drinkPageControl.currentPage = index<br>        //SegmentedControl會根據index而變換<br>        typeSegmentedControl.selectedSegmentIndex = index<br>    }</pre><p>→ 進入畫面時：</p><pre>override func viewDidLoad() {<br>        super.viewDidLoad()<br><br>        fetchMenu()<br>        updateUI()<br>        <br>        //segmentedControl正常情況下的字是黑色<br>        typeSegmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)<br>        //segmentedControl被選中的時候字是白色<br>        typeSegmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)<br>        <br>        //固定row的高度<br>        tableView.rowHeight = 85<br>    }</pre><blockquote><strong><em>Part2</em></strong></blockquote><p>努力產出中!</p><blockquote><strong><em>Reference</em></strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/%E4%BD%A0%E5%87%BA%E5%8A%9B%E5%AF%AB-app-%E6%88%91%E5%87%BA%E9%8C%A2%E8%AB%8B%E9%A3%B2%E6%96%99-6a9ac14a2a22">#20 訂飲料 App，上傳資料到後台</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/135-%E5%88%A9%E7%94%A8-airtable-%E9%96%8B%E7%99%BC-ios-app-%E6%AF%94%E6%96%B9%E9%9B%BB%E5%BD%B1-app-db88fc3bf6c2">#135 利用 Airtable 開發 iOS App，比方電影 App</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-airtable-%E8%A3%BD%E4%BD%9C-ios-app-%E9%80%A3%E7%B5%90%E7%9A%84%E5%BE%8C%E5%8F%B0%E8%B3%87%E6%96%99-e22378536acb">利用 Airtable 製作 iOS App 連結的後台資料</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E4%B8%B2%E6%8E%A5-airtable%E7%9A%84-api-%E9%96%8B%E7%99%BC-ios-app-5e5c86c8ca7d">串接 Airtable的 API 開發 iOS App</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E4%BB%A5-api-key-%E4%B8%B2%E6%8E%A5%E5%BF%83%E6%9C%89%E8%A8%AD%E9%98%B2%E7%9A%84api-a499dee188e8">以 API key 串接心有設防的API</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E4%BD%BF%E7%94%A8-urlsession-%E4%B8%B2%E6%8E%A5-http-get-post-delete-put-api-%E4%BB%A5-reqres-api-%E7%82%BA%E4%BE%8B-f977ec876c33">使用 URLSession 串接 REST API，練習 http get，post，delete，put，以 Reqres API 為例</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc6f173e60a9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%BE%97%E6%AD%A3%E8%A8%82%E9%A3%B2%E6%96%99-app-part-1-%E4%B8%B2%E6%8E%A5-airtable-api-bc6f173e60a9">得正訂飲料 App｜Part 1:串接 Airtable API</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#5 YouTube Data API｜詹姆士姆士流]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/5-youtube-data-api-%E8%A9%B9%E5%A7%86%E5%A3%AB%E5%A7%86%E5%A3%AB%E6%B5%81-6c9a5d940033?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c9a5d940033</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Fri, 07 Apr 2023 09:49:10 GMT</pubDate>
            <atom:updated>2023-04-07T09:49:10.363Z</atom:updated>
            <content:encoded><![CDATA[<p>偶爾喜歡在廚房搞東搞西的我，除了從食譜、IG找靈感外，也會看YouTube上各種神人的分享，其中一位就是詹姆士大神，很喜歡他說過的一句話：「吃飯，可以一個人吃，但是不要隨便。」</p><blockquote><strong><em>學習目標</em></strong></blockquote><ol><li>使用YouTube API取得影片資料</li><li>利用 URLSession 抓取後台的 JSON 資料</li><li>利用 JSONDecoder 和 Codable 將 JSON 資料生成自訂型別</li><li>以TableViewController顯示回傳的資料，點選cell連結至YouTube影片</li><li>影片like按鈕功能</li><li>Segmented Control應用</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*QfjbIpvNWjE5Khub3_wGPQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vibg98ylB_U_JECExVV8mg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9x5OvTiD2eBbD3CnSQgODg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R6J2KZBX3lqNe7A2IL81VA.png" /></figure><blockquote><strong><em>StoryBoard架構</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jKDvxCKD0-R5enX6UR9SAQ.png" /></figure><blockquote><strong><em>YouTube API抓資料</em></strong></blockquote><p>1. <strong>先從videos list API取得channel ID</strong></p><p><a href="https://developers.google.com/youtube/v3/docs/videos/list?hl=zh-tw">Videos: list | YouTube Data API | Google Developers</a></p><p>以下videos API 可取得影片蘑菇醬鐵板麵的相關資訊，影片的 id 為 BTgIPEhFvfo，且必須包含key=自己申請的金鑰<strong>。</strong></p><pre>https://youtube.googleapis.com/youtube/v3/videos?part=snippet&amp;id=BTgIPEhFvfo&amp;t=12s&amp;key=[YOUR_API_KEY]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X0Wkp9BBgENhEbMzHdfFmA.png" /><figcaption>影片id為橘色畫線處</figcaption></figure><p>當part=snippet 時，JSON包含以下內容，可取得channel id為UC4_Ds33FmwzcTDL3G2pbs7g</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X2b6LpucR4AKKJ8wDX4hLg.png" /></figure><p>2. <strong>再從channel list API裡取得playlists ID</strong></p><p><a href="https://developers.google.com/youtube/v3/docs/channels/list?hl=zh-tw">Channels: list | YouTube Data API | Google Developers</a></p><p>以下channel API 的 id 為上一步驟取得的channel idUC4_Ds33FmwzcTDL3G2pbs7g，一樣必須包含key=自己申請的金鑰<strong>。</strong></p><pre>https://youtube.googleapis.com/youtube/v3/channels?part=contentDetails&amp;id=UC4_Ds33FmwzcTDL3G2pbs7g&amp;key=[YOUR_API_KEY]</pre><p>當part=contentDetails時，JSON包含以下內容，可從uploads 欄位取得 playlist 的 id 為 UU4_Ds33FmwzcTDL3G2pbs7g。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YQjxUtR7j_kJUFKh3mgxmg.png" /></figure><p><strong>3. 最後從playlistItems list API取得 playlist 的影片清單</strong></p><p><a href="https://developers.google.com/youtube/v3/docs/playlistItems/list?hl=zh-tw#usage">PlaylistItems: list | YouTube Data API | Google Developers</a></p><p>以下playlistItems API 的 id 為上一步驟取得的playlist idUU4_Ds33FmwzcTDL3G2pbs7g，maxResults 代表回傳的影片數量，可指定的最大數字為 50 (預設值為5)，一樣必須包含key=自己申請的金鑰<strong>。</strong></p><pre>https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=UU4_Ds33FmwzcTDL3G2pbs7g&amp;key=[YOUR_API_KEY]</pre><p>當part=snippet時，JSON包含以下內容，可看到items包含了50筆array資料，可查詢影片相關資訊。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NcUf2gA6Rec_TULsWeUwow.png" /></figure><p>從上方JSON的內容可以得知總影片數量是427筆，但因為maxResults一次最多只能抓取50筆資料，因此可以每一次得到的<strong>nextPageToken</strong>來取得下一頁的50筆資料，直到抓取完所有資料。<br><strong>→ </strong>此次我只抓取50筆資料，若要抓取下一頁的50筆資料，只要在 playlistItems list API 裡將參數 <strong>pageToken</strong>指定為上方nextPageToken 的 EAAaBlBUOkNESQ，就可以抓取第二頁的50筆資料。</p><pre>https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=UU4_Ds33FmwzcTDL3G2pbs7g&amp;key=[YOUR_API_KEY]&amp;pageToken=EAAaBlBUOkNESQ</pre><p>🌟備註：將從YouTube Data API取得的API，貼到Postman檢查後台功能是否正常，並將自己申請的金鑰填入，以取得回傳的JSON。</p><blockquote><strong><em>程式撰寫</em></strong></blockquote><p><strong>🔸透過Codable將JSON物件轉換成自訂的型別</strong></p><pre>import Foundation<br><br>//透過Codable將影片清單的JSON資料轉成自訂型別<br>struct SearchResponse: Codable {<br>    let nextPageToken: String<br>    let items: [Item]<br>    <br>    struct Item: Codable {<br>        let snippet: Snippet<br>        <br>        struct Snippet: Codable {<br>            let title: String<br>            let description: String<br>            let thumbnails: Thumbnail<br>            let resourceId: ResourceID<br>            <br>            struct Thumbnail: Codable {<br>                let medium: ThumbnailImage<br>                let standard: ThumbnailImage<br>                let maxres: ThumbnailImage<br>                <br>                struct ThumbnailImage: Codable {<br>                    let url: URL<br>                }<br>            }<br>            <br>            struct ResourceID: Codable {<br>                let videoId: String<br>            }<br>        }<br>    }<br>}</pre><p><strong>🔸自訂型別儲存要在TableViewController上呈現的影片資料</strong></p><pre>import Foundation<br><br>//建立表格要呈現的影片資料，包含：影片縮圖、標題、影片ID與isFavorite四個變數。<br>struct Video {<br>    var thumbnailUrl: URL<br>    var title: String<br>    var videoID: String<br>    var isFavorite: Bool = false    //預設為false<br>}</pre><p><strong>(James)TableViewCell</strong></p><p>→ Table View Cell設定ID，style選擇custom</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ehMGnoatmDchvpEZs46dUw.png" /></figure><p>→ 將要顯示於cell中的元件拉<em>@IBOutlet</em> 在Table View Cell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RWYVw80gFta9gREPfkPcqw.png" /></figure><p><strong>(James)TableViewController</strong></p><p>→ 定義抓資料的fetchItems</p><pre>//定義抓資料的fetchItems<br>    func fetchItems() {<br>        let apikey = &quot;[YOUR_API_KEY]&quot;<br>        //let nextPageToken = &quot;EAAaBlBUOkNESQ&quot;<br>        <br>        let urlString = &quot;https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=UU4_Ds33FmwzcTDL3G2pbs7g&amp;key=[YOUR_API_KEY]&quot;<br>        if let url = URL(string: urlString) {<br>            //{}的程式是closure，資料下載完成時會執行{}的程式，傳入data(抓到的資料),reponse(後台回傳抓資料的結果),error(錯誤資訊)三個參數。<br>            URLSession.shared.dataTask(with: url) { data, response, error in<br>                if let data {<br>                    let decoder = JSONDecoder()<br>                    do {<br>                        //透過decode將Data轉成對應的物件內容<br>                        let searchResponse = try decoder.decode(SearchResponse.self, from: data)<br>                        // 將解碼後的資料取出需要的項目整理到新的array中<br>                        var newvideos: [Video] = []<br>                        for item in searchResponse.items {<br>                            let video = Video(thumbnailUrl: item.snippet.thumbnails.standard.url , title: item.snippet.title, videoID: item.snippet.resourceId.videoId)<br>                            newvideos += [video]<br>                        }<br>                        //因為completionHandler將在function dataTask執行過後一段時間才執行，所以ul4ur8<br>                        self.allVideos = newvideos<br>                        //需在main thread執行UI相關的程式<br>                        DispatchQueue.main.async {<br>                            //如果沒有reload data，表格不會更新，只會看到一片空白<br>                            self.tableView.reloadData()<br>                        }<br>                        print(&quot;get data&quot;)<br>                    } catch {<br>                        print(error)<br>                    }<br>                }<br>            } .resume()<br>            print(&quot;function dataTask執行完會先回傳task，然後呼叫task的resume啟動它&quot;)<br>        }<br>        <br>    }</pre><p>→ 建立array存放資料</p><pre>//宣告allVideos變數儲存所有影片<br>   var allVideos = [Video]()<br>   //宣告favoriteVideos變數儲存isFavorite為true的影片<br>   var favoriteVideos = [Video]()</pre><p>→ segmentedControl拉@IBOutlet與@IBAction，當點選segmentedControl時，會更新表格，分為顯示全部影片或顯示喜愛的影片。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYd2ZvnhrMkNj6slzdWPlw.png" /></figure><p>→ 點選cell的likeButton(愛心)時，將影片加入喜愛項目，並用reloadRows來更新被點選的cell內容。</p><pre>  //點選likeButton時，將影片加入喜愛項目，並更新點選的cell內容<br>    @IBAction func likeAction(_ sender: UIButton) {<br>        //更新allVideos的isFavorite狀態<br>        allVideos[sender.tag].isFavorite = !allVideos[sender.tag].isFavorite<br>        //更新指定的cell(at indexPaths: [IndexPath],with animation: UITableView.RowAnimation)<br>        tableView.reloadRows(at: [IndexPath(row: sender.tag, section: 0)], with: .none)<br>    }</pre><p>→ 表格顯示資料</p><ul><li>表格有幾段 (此次只有一段)</li></ul><pre>//表格有幾段<br>    override func numberOfSections(in tableView: UITableView) -&gt; Int {<br>        return 1<br>    }</pre><ul><li>每一段表格有幾列<br>當segmentedControl選在0時，顯示所有抓到的影片 ; <br>當segmentedControl選在1時，顯示喜愛的影片(isFavorite為true)</li></ul><pre>//每一段表格有幾列<br>    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br>        //當segment為0時，顯示所有抓到的影片<br>        if segmentedControl.selectedSegmentIndex == 0  {<br>            return allVideos.count<br>        //當segment為1時，顯示有點選isFavorite的影片<br>        } else {<br>            //array過濾函示filter，可以把$0看成array的元素<br>            favoriteVideos = allVideos.filter({ $0.isFavorite})<br>            return favoriteVideos.count<br>        }<br>    }</pre><ul><li>回傳哪一個cell，可從參數 indexPath 得到 section &amp; row</li></ul><pre>//回傳哪一個cell，可從參數 indexPath 得到 section &amp; row<br>    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {<br>    <br>        let cell = tableView.dequeueReusableCell(withIdentifier: &quot;JamesTableViewCell&quot;, for: indexPath) as! JamesTableViewCell<br>        <br>        //當segment為0時，顯示所有抓到的影片; 當segment為1時，顯示有點選isFavorite的影片<br>        var video: Video<br>        //也可以這樣寫：var video = allVideos[indexPath.row]<br>        if segmentedControl.selectedSegmentIndex == 0  {<br>            video = allVideos[indexPath.row]<br>        } else {<br>            video = favoriteVideos[indexPath.row]<br>        }<br>        <br>        //顯示影片標題<br>        cell.titleLabel.text = video.title<br>        <br>        //用三元運算子來簡化條件判斷式的寫法 (likeButton顯示的image)<br>        cell.likeButton.imageView?.image = video.isFavorite ? UIImage(systemName: &quot;heart.fill&quot;) : UIImage(systemName: &quot;heart&quot;)<br>        //原本條件判斷式寫法 (likeButton顯示的image)<br>        /*<br>        var isFavoriate = true<br>        if isFavoriate {<br>            cell.likeButton.imageView?.image = UIImage(systemName: &quot;heart.fill&quot;)<br>        } else {<br>            cell.likeButton.imageView?.image = UIImage(systemName: &quot;heart&quot;)<br>        }<br>        */<br>        <br>        //用tag辨別點選了哪一個按鈕<br>        cell.likeButton.tag = indexPath.row<br>        <br>        //先顯示預設圖片，才不會因從網路抓取需時間而看到先前的圖片<br>        cell.thumbnailImageView.image = UIImage(systemName: &quot;video.circle.fill&quot;)<br>        URLSession.shared.dataTask(with:<br>                                    video.thumbnailUrl) { data, response, error in<br>            if let data {<br>                let image = UIImage(data: data)<br>                    DispatchQueue.main.async {<br>                        cell.thumbnailImageView.image = image<br>                    }<br>            }<br>        }.resume()<br>        <br>        return cell<br>    }</pre><p>→ 以prepare 傳資料到下一頁(Video)View Controller</p><pre> //以prepare傳遞資料到下一頁<br>    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {<br>        if let videoVC = segue.destination as? VideoViewController {<br>            let selectedRow = tableView.indexPathForSelectedRow<br>            if let passIndex = selectedRow?.row {<br>                videoVC.index = passIndex<br>                videoVC.data = allVideos<br>            }<br>        }<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pO7rffsLwGjxy4avpK9MSw.png" /><figcaption>TableViewCell拉segue到下一頁</figcaption></figure><p><strong>(Video)ViewController</strong></p><p>→ 點選影片後，在此頁面顯示被點選的youtube影片，在頁面上加入WebKit View，並宣告儲存要顯示資料的屬性。</p><pre> //前一頁select到的tableview的row的位置<br>    var index: Int!<br> //宣告儲存前一頁解析完成的API資料<br>    var data = [Video]()</pre><p>→ 此頁需import Webkit，並將WebKit View拉@IBOutlet</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Ew5kWB98CZj52BX4usS7w.png" /></figure><p>→ 使用WebKit View播放YouYube</p><pre>override func viewDidLoad() {<br>        super.viewDidLoad()<br>        <br>        //透過URLRequest指派並透過JamesWebView顯示網頁<br>        if let url = URL(string: &quot;https://www.youtube.com/watch?v=\(data[index].videoID)&quot;) {<br>            let request = URLRequest(url: url)<br>            JamesWebView.load(request)<br>        }<br>    }</pre><blockquote><strong><em>遇到的困難</em></strong></blockquote><p>Youtube Data API屬於層層包裹的資料，一開始花了滿多時間研究，但其實不難取得。覺得最困難的部分是將取得的資料呈現在表格上，並且此次學習Chia學姐加入segmentedControl與喜愛button的應用，雖然還是有比較模糊的地方，但有做出想要的樣子，相信不懂的部分會在某一天茅塞頓開的!</p><p>🌟小筆記-如何將網路上抓下來的JSON 資料，從Data型別變成自訂型別🌟</p><ol><li>將JSON裡以{ }描述的object變成自訂型別，可用class或struct。</li><li>讓自訂型別遵從protocol Codable或Decodable或Encodable。</li><li>JSON裡object的key將成為自訂型別的property名字。</li><li>只要定義App需要的欄位就好，不用將JSON object的key都寫出來。</li><li>若<em>JSON object不一定有的key</em> 或 <em>某個key對應的value可能是null</em>，property要宣告為optional。</li></ol><blockquote><strong><em>GitHub</em></strong></blockquote><p><a href="https://github.com/alicelee812/YouTube-Data-API">GitHub - alicelee812/YouTube-Data-API</a></p><blockquote><strong><em>Reference</em></strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/19-%E4%B8%B2%E6%8E%A5%E7%AC%AC%E4%B8%89%E6%96%B9-api-%E8%A7%A3%E6%9E%90-json-%E8%B3%87%E6%96%99%E9%A1%AF%E7%A4%BA-f1556e84daec">#19 串接第三方 API，解析 JSON 資料，轉換成自訂型別顯示</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/101-%E4%BD%BF%E7%94%A8-youtube-data-api-%E6%8A%93%E5%8F%96%E6%9C%89%E8%B6%A3%E7%9A%84-youtuber-%E5%BD%B1%E7%89%87-mv-d05c3a0c70aa">#101 使用 YouTube Data API 抓取有趣的 Youtuber 影片 &amp; MV</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-enum-codingkeys-%E5%AE%A2%E8%A3%BD-json-%E5%B0%8D%E6%87%89%E7%9A%84-property-1b27f29c0c32">利用 CodingKey 客製 JSON 對應的 property</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E6%89%BE%E5%87%BA-jsondecoder-%E7%9A%84-decode-%E9%8C%AF%E8%AA%A4-c9c6deae297">找出 JSONDecoder 的 decode 錯誤</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/16-youtube-data-api%E4%B8%B2%E6%8E%A5%E7%B7%B4%E7%BF%92-80373697a197">#16 YouTube Data API串接練習</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c9a5d940033" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/5-youtube-data-api-%E8%A9%B9%E5%A7%86%E5%A3%AB%E5%A7%86%E5%A3%AB%E6%B5%81-6c9a5d940033">#5 YouTube Data API｜詹姆士姆士流</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#4 選擇題App｜卡通人物猜猜樂]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/3-%E9%81%B8%E6%93%87%E9%A1%8Capp-%E5%8D%A1%E9%80%9A%E4%BA%BA%E7%89%A9%E7%8C%9C%E7%8C%9C%E6%A8%82-a186887cc12?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a186887cc12</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Sun, 05 Mar 2023 04:14:11 GMT</pubDate>
            <atom:updated>2023-03-06T07:07:35.956Z</atom:updated>
            <content:encoded><![CDATA[<p>前陣子因朋友分享台灣本色檢定考測驗，做完後覺得很難但也很有趣，因此給了我靈感來做這次的選擇題App，大家一起來挑戰看看吧!</p><blockquote><strong><em>學習目標</em></strong></blockquote><ol><li>製作選擇題App，每題有四個選項。</li><li>答對一題加10分。</li><li>畫面上顯示目前題目是第幾題。</li><li>自訂選擇題的資料型別。</li><li>題庫有 n 題，隨機出其中的 10 題，每次玩的時候題目順序都不一樣。( n &gt; 10 )</li><li>使用 UIAlertController 顯示 alert。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uqs1LVHZ6jpF-nlA6yg--A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uH0CJPl1pzFpvIBCctFp0g.png" /></figure><blockquote><strong><em>StoryBoard架構</em></strong></blockquote><ul><li>第一頁(Home)ViewCountroller：首頁，點選Go按鈕進入第二頁。</li><li>第二頁(Question)ViewController：題目頁面，功能應用如下：<br>1) 題目共有20題，每一次會從中隨機出10題<br>2) 答對一題可得10分<br>3) 上方Slider與Label會顯示目前題數進度 (Slider需取消勾選User Interaction Enabled，才會只有進度功能，無法滑動)<br>4) 若答對，button會呈現橘色底 ; 答錯會呈現灰底灰字，並以橘色底顯示正確答案<br>5) 不論答對與否，皆會呈現正確角色圖片<br>6) 按下一題才能進入下一題<br>7) alert訊息視窗顯示總分 &amp; 是否再玩一次/結束</li></ul><blockquote><strong><em>程式撰寫</em></strong></blockquote><h4>自訂型別(Cartoon)</h4><pre>import Foundation<br><br>struct Cartoon {<br>    var questionPic1: String<br>    var questionPic2: String<br>    var questionPic3: String<br>    var questionPic4: String<br>    var option: [String]<br>    var answer: String<br>    var characterPic: String<br>}</pre><h4>第一頁 Country View Controller</h4><ul><li>Go按鈕拉Segue到第二頁Question View Controller<br>Kind選「Present Modally」 ; Presentation選「Full Screen」</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Jl0pI1IWM9ftE-24U916Q.png" /></figure><h4>第二頁 Question View Controller</h4><ol><li>拉<em>@IBOutlet</em></li></ol><pre>  @IBOutlet weak var numberSlider: UISlider!<br>    @IBOutlet weak var numberLabel: UILabel!<br>    @IBOutlet weak var scoreLabel: UILabel!<br>    @IBOutlet var questionImageView1: UIImageView!<br>    @IBOutlet weak var questionImageView2: UIImageView!<br>    @IBOutlet weak var questionImageView3: UIImageView!<br>    @IBOutlet weak var questionImageView4: UIImageView!<br>    @IBOutlet var optionButton: [UIButton]!<br>    @IBOutlet weak var characterImageView: UIImageView!<br>    @IBOutlet weak var nextButton: UIButton!</pre><p>2. 建立array存放資料</p><pre>//宣告儲存Cartoon屬性的questions<br>    var questions =<br>    [Cartoon(questionPic1: &quot;Anya_Color1&quot;, questionPic2: &quot;Anya_Color2&quot;, questionPic3: &quot;Anya_Color3&quot;, questionPic4: &quot;Anya_Color4&quot;, option: [&quot;佩佩豬&quot;, &quot;安妮亞&quot;, &quot;胖丁&quot;, &quot;卡比獸&quot;], answer: &quot;安妮亞&quot;, characterPic: &quot;Anya&quot;),<br>     Cartoon(questionPic1: &quot;Baikinman_Color1&quot;, questionPic2: &quot;Baikinman_Color2&quot;, questionPic3: &quot;Baikinman_Color3&quot;, questionPic4: &quot;white&quot;, option: [&quot;灶門禰豆子&quot;, &quot;天線寶寶-丁丁&quot;, &quot;耿鬼&quot;, &quot;細菌人&quot;], answer: &quot;細菌人&quot;, characterPic: &quot;Baikinman&quot;),<br>     Cartoon(questionPic1: &quot;Charles_Color1&quot;, questionPic2: &quot;Charles_Color2&quot;, questionPic3: &quot;Charles_Color3&quot;, questionPic4: &quot;black&quot;, option: [&quot;查理布朗&quot;, &quot;皮卡丘&quot;, &quot;海綿寶寶&quot;, &quot;小熊維尼&quot;], answer: &quot;查理布朗&quot;, characterPic: &quot;Charles&quot;),<br>     Cartoon(questionPic1: &quot;Conan_Color1&quot;, questionPic2: &quot;Conan_Color2&quot;, questionPic3: &quot;Conan_Color3&quot;, questionPic4: &quot;white&quot;, option: [&quot;哆啦A夢&quot;, &quot;柯南&quot;, &quot;唐老鴨&quot;, &quot;白雪公主&quot;], answer: &quot;柯南&quot;, characterPic: &quot;Conan&quot;),<br>     Cartoon(questionPic1: &quot;Crayon_Color1&quot;, questionPic2: &quot;Crayon_Color2&quot;, questionPic3: &quot;Crayon_Color3&quot;, questionPic4: &quot;black&quot;, option: [&quot;小小兵&quot;, &quot;老皮&quot;, &quot;蠟筆小新&quot;, &quot;布丁狗&quot;], answer: &quot;蠟筆小新&quot;, characterPic: &quot;Crayon&quot;),<br>     Cartoon(questionPic1: &quot;Doraemon_Color1&quot;, questionPic2: &quot;Doraemon_Color2&quot;, questionPic3: &quot;Crayon_Color1&quot;, questionPic4: &quot;white&quot;, option: [&quot;辛普森&quot;, &quot;威力&quot;, &quot;藍色小精靈&quot;, &quot;哆啦A夢&quot;], answer: &quot;哆啦A夢&quot;, characterPic: &quot;Doraemon&quot;),<br>     Cartoon(questionPic1: &quot;Doremi_Color1&quot;, questionPic2: &quot;Doremi_Color2&quot;, questionPic3: &quot;Doremi_Color3&quot;, questionPic4: &quot;Anya_Color1&quot;, option: [&quot;小魔女DoReMi&quot;, &quot;頑皮豹&quot;, &quot;熊抱哥&quot;, &quot;小豬&quot;], answer: &quot;小魔女DoReMi&quot;, characterPic: &quot;DoReMi&quot;),<br>     Cartoon(questionPic1: &quot;Hattori_Color1&quot;, questionPic2: &quot;Charles_Color1&quot;, questionPic3: &quot;Crayon_Color1&quot;, questionPic4: &quot;white&quot;, option: [&quot;音速小子&quot;, &quot;伊布&quot;, &quot;忍者哈特利&quot;, &quot;水箭龜&quot;], answer: &quot;忍者哈特利&quot;, characterPic: &quot;Hattori&quot;),<br>     Cartoon(questionPic1: &quot;Jerry_Color1&quot;, questionPic2: &quot;Jerry_Color2&quot;, questionPic3: &quot;Jerry_Color3&quot;, questionPic4: &quot;black&quot;, option: [&quot;咖哩麵包超人&quot;, &quot;傑利鼠&quot;, &quot;布丁狗&quot;, &quot;章魚哥&quot;], answer: &quot;傑利鼠&quot;, characterPic: &quot;Jerry&quot;),<br>     Cartoon(questionPic1: &quot;Kenny_Color1&quot;, questionPic2: &quot;Kenny_Color2&quot;, questionPic3: &quot;Crayon_Color3&quot;, questionPic4: &quot;black&quot;, option: [&quot;肯尼&quot;, &quot;噴火龍&quot;, &quot;加菲貓&quot;, &quot;小病毒&quot;], answer: &quot;肯尼&quot;, characterPic: &quot;Kenny&quot;),<br>     Cartoon(questionPic1: &quot;Maruko_Color1&quot;, questionPic2: &quot;white&quot;, questionPic3: &quot;Jerry_Color4&quot;, questionPic4: &quot;black&quot;, option: [&quot;小紅帽&quot;, &quot;小不點&quot;, &quot;佩佩豬&quot;, &quot;櫻桃小丸子&quot;], answer: &quot;櫻桃小丸子&quot;, characterPic: &quot;Maruko&quot;),<br>     Cartoon(questionPic1: &quot;Naruto_Color1&quot;, questionPic2: &quot;Naruto_Color2&quot;, questionPic3: &quot;Naruto_Color3&quot;, questionPic4: &quot;Naruto_Color4&quot;, option: [&quot;漩渦鳴人&quot;, &quot;麵包超人&quot;, &quot;小火龍&quot;, &quot;咖哩麵包超人&quot; ], answer: &quot;漩渦鳴人&quot;, characterPic: &quot;Naruto&quot;),<br>     Cartoon(questionPic1: &quot;Patrick_Color1&quot;, questionPic2: &quot;Patrick_Color2&quot;, questionPic3: &quot;Patrick_Color3&quot;, questionPic4: &quot;white&quot;, option: [&quot;妙蛙種子&quot;, &quot;派大星&quot;, &quot;天線寶寶-丁丁&quot;, &quot;阿拉蕾&quot;], answer: &quot;派大星&quot;, characterPic: &quot;Patrick&quot;),<br>     Cartoon(questionPic1: &quot;Pig_Color1&quot;, questionPic2: &quot;Virus_Color1&quot;, questionPic3: &quot;black&quot;, questionPic4: &quot;white&quot;, option: [&quot;佩佩豬&quot;, &quot;吐司麵包超人&quot;, &quot;史奴比&quot;, &quot;小豬&quot;], answer: &quot;佩佩豬&quot;, characterPic: &quot;Pig&quot;),<br>     Cartoon(questionPic1: &quot;Pikachu_Color1&quot;, questionPic2: &quot;Pikachu_Color2&quot;, questionPic3: &quot;Crayon_Color1&quot;, questionPic4: &quot;black&quot;, option: [&quot;蛋黃哥&quot;, &quot;可達鴨&quot;, &quot;皮卡丘&quot;, &quot;小拉達&quot;], answer: &quot;皮卡丘&quot;, characterPic: &quot;Pikachu&quot;),<br>     Cartoon(questionPic1: &quot;Police_Color1&quot;, questionPic2: &quot;Charles_Color2&quot;, questionPic3: &quot;white&quot;, questionPic4: &quot;black&quot;, option: [&quot;兩津勘吉&quot;, &quot;口呆花&quot;, &quot;小小兵&quot;, &quot;酷企鵝&quot;], answer: &quot;兩津勘吉&quot;, characterPic: &quot;Police&quot;),<br>     Cartoon(questionPic1: &quot;Sponge_Color1&quot;, questionPic2: &quot;Sponge_Color2&quot;, questionPic3: &quot;Sponge_Color3&quot;, questionPic4: &quot;Crayon_Color1&quot;, option: [&quot;跳跳虎&quot;, &quot;米老鼠&quot;, &quot;Hello Kitty&quot;, &quot;海綿寶寶&quot;], answer: &quot;海綿寶寶&quot;, characterPic: &quot;SpongeBob&quot;),<br>     Cartoon(questionPic1: &quot;Stitch_Color1&quot;, questionPic2: &quot;Stitch_Color2&quot;, questionPic3: &quot;Stitch_Color3&quot;, questionPic4: &quot;Stitch_Color4&quot;, option: [&quot;鬼斯&quot;, &quot;柴郡貓&quot;, &quot;史迪奇&quot;, &quot;烏蘇拉&quot;], answer: &quot;史迪奇&quot;, characterPic: &quot;Stitch&quot;),<br>     Cartoon(questionPic1: &quot;Tom_Color1&quot;, questionPic2: &quot;Tom_Color2&quot;, questionPic3: &quot;Tom_Color3&quot;, questionPic4: &quot;white&quot;, option: [&quot;湯姆貓&quot;, &quot;音速小子&quot;, &quot;布魯托&quot;, &quot;龍貓&quot;], answer: &quot;湯姆貓&quot;, characterPic: &quot;Tom&quot;),<br>     Cartoon(questionPic1: &quot;Virus_Color1&quot;, questionPic2: &quot;Virus_Color2&quot;, questionPic3: &quot;Kenny_Color1&quot;, questionPic4: &quot;white&quot;, option: [&quot;跳跳虎&quot;, &quot;胖虎&quot;, &quot;加菲貓&quot;, &quot;小病毒&quot;], answer: &quot;小病毒&quot;, characterPic: &quot;Virus&quot;)<br>    ]</pre><p>3. 宣告題數、分數、答案、選項button狀態之變數</p><pre>    var index = 0             //宣告index儲存目前題數<br>    var score = 0             //宣告答題總分變數<br>    var answer = String()     //宣告答案變數<br>    var buttonEnabled = true  //button的狀態</pre><p>4. 建立與畫面有關的函式，後面可以不用一直重複寫</p><pre>func updateView() {<br>        //slider顯示目前題數<br>        numberSlider.value = Float(index + 1) <br>        //label顯示目前題數      <br>        numberLabel.text = &quot;\(index + 1) / 10&quot; <br>        //分數計算     <br>        scoreLabel.text = &quot;\(score)&quot;                <br>        //讀取每個題目的圖片<br>        questionImageView1.image = UIImage(named: &quot;\(questions[index].questionPic1)&quot;)<br>        questionImageView2.image = UIImage(named: &quot;\(questions[index].questionPic2)&quot;)<br>        questionImageView3.image = UIImage(named: &quot;\(questions[index].questionPic3)&quot;)<br>        questionImageView4.image = UIImage(named: &quot;\(questions[index].questionPic4)&quot;)<br>        //讀取答案<br>        answer = questions[index].answer           <br>        //正確圖片出來前的預設頭像<br>        characterImageView.image = UIImage(systemName: &quot;questionmark.app&quot;)<br>        //設定option button顯示的title<br>        for i in 0...3 {<br>            //檢查i<br>            if i &gt;= questions.startIndex &amp;&amp; i &lt; questions.endIndex {<br>               print(questions[i])<br>            }<br>            optionButton[i].setTitle(questions[index].option[i], for: .normal)<br>            <br>        }<br>    }</pre><p>5. 進入畫面時：<br>→使用 .shuffle() 更改題目與選項順序<br>→更改slider thumb的圖案<br>→設定四個questionImageView的邊框顏色及寬度</p><pre>override func viewDidLoad() {<br>        super.viewDidLoad()<br>        <br>        //每次打開app時會更改題目順序<br>        questions.shuffle()<br>        //更改option順序             <br>        questions[index].option.shuffle()<br>        updateView()<br>        //更改slider thumb image<br>        let image = UIImage(named: &quot;footprints&quot;)<br>        numberSlider.setThumbImage(image, for: .normal)<br>        //設定questionImageView的邊框顏色<br>        questionImageView1.layer.borderColor = CGColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)<br>        questionImageView1.layer.borderWidth = 2<br>        questionImageView2.layer.borderColor = CGColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)<br>        questionImageView2.layer.borderWidth = 2<br>        questionImageView3.layer.borderColor = CGColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)<br>        questionImageView3.layer.borderWidth = 2<br>        questionImageView4.layer.borderColor = CGColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)<br>        questionImageView4.layer.borderWidth = 2<br>    }</pre><p>6. 四個選項button: 只能點選一次<br>→若答對：button會變橘色底，分數加10分，並顯示正確角色圖片<br>→若答錯：button會變灰底灰字，並且會以橘色底顯示正確答案，也會顯示正確角色圖片<br>→做完十題：計算分數</p><pre> //選擇答案<br>    @IBAction func selectAnswer(_ sender: UIButton) {<br>        if buttonEnabled {<br>            buttonEnabled = false<br>            //如果選到正確答案<br>            if sender.titleLabel?.text == questions[index].answer {<br>                sender.tintColor = .orange<br>                score += 10<br>                scoreLabel.text = &quot;\(score)&quot;<br>                characterImageView.image = UIImage(named: &quot;\(questions[index].characterPic)&quot;)<br>            } else {<br>                sender.tintColor = .lightGray<br>                sender.setTitleColor(.gray, for: .normal)<br>                characterImageView.image = UIImage(named: &quot;\(questions[index].characterPic)&quot;)<br>                //找出正確答案，並呈現橘色<br>                for choicebtn in 0...optionButton.count-1 {<br>                    if optionButton[choicebtn].titleLabel?.text == questions[index].answer {<br>                        optionButton[choicebtn].tintColor = .orange<br>                    }<br>                }<br>            }<br>        }<br>        //做答完第十題則計算分數<br>        if index == 9 {<br>            caculateScore()<br>        }<br>    }</pre><p>7. 下一題</p><pre>//下一題<br>    @IBAction func nextQuestion(_ sender: UIButton) {<br>        index += 1<br>        updateView()<br>        <br>        //當回答到第10題，但沒有選擇答案時，按下一題會直接計算分數<br>        if index == 10 {<br>            caculateScore()<br>        }<br>        <br>        //option button和字體的顏色改回原本的樣子<br>        for i in 0...3 {<br>            optionButton[i].tintColor = .systemTeal<br>            optionButton[i].setTitleColor(.white, for: .normal)<br>        }<br>        <br>        buttonEnabled = true<br>    }</pre><p>8. 再玩一次<br>題數需從0開始，分數要歸0，選項button狀態為可點選</p><pre>//再玩一次<br>    func playAgain() {<br>        index = 0<br>        score = 0<br>        viewDidLoad()<br>        <br>        for i in 0...3 {<br>            optionButton[i].tintColor = .systemTeal<br>            optionButton[i].setTitleColor(.white, for: .normal)<br>        }<br>        <br>        buttonEnabled = true<br>    }</pre><p>9. 做答完10題後，以UIAlertController產生訊息視窗，並顯示分數，可點選再玩一次或是結束遊戲。</p><p>→ UIAlertController產生訊息視窗<br>→ UIAlertAction產生視窗上的按鈕(確認、取消)<br>→ addAction將按鈕加入訊息視窗中<br>→ present切換頁面來顯示訊息視窗</p><p>訊息視窗依總分區分為四種不同的訊息，但都可以選擇再玩一次或是結束。</p><pre>//第十題作答完成時，計算分數<br>    func caculateScore() {<br>        if score &gt;= 80 {<br>            let alertController = UIAlertController(title: &quot;總分\(score)分&quot;, message: &quot;你是卡通神人!!!&quot;, preferredStyle: .alert)<br>            //確認按鈕<br>            let okAction = UIAlertAction(title: &quot;再玩一次&quot;, style: .default) { _<br>                in<br>                self.playAgain()<br>            }<br>            //取消按鈕<br>            let cancelAction = UIAlertAction(title: &quot;結束&quot;, style: .destructive) {<br>                _ in<br>                self.dismiss(animated: true)<br>            }<br>            //將按鈕加入訊息視窗<br>            alertController.addAction(okAction)<br>            alertController.addAction(cancelAction)<br>            //顯示訊息視窗<br>            present(alertController, animated: true, completion: nil)<br>        } else if score &gt;= 60 &amp;&amp; score &lt; 80 {<br>            let alertController2 = UIAlertController(title: &quot;總分\(score)分&quot;, message: &quot;哇嗚～你好厲害!&quot;, preferredStyle: .alert)<br>            //確認按鈕<br>            let okAction = UIAlertAction(title: &quot;再玩一次&quot;, style: .default) { _<br>                in<br>                self.playAgain()<br>            }<br>            //取消按鈕<br>            let cancelAction = UIAlertAction(title: &quot;結束&quot;, style: .destructive) {<br>                _ in<br>                self.dismiss(animated: true)<br>            }<br>            //將按鈕加入訊息視窗<br>            alertController2.addAction(okAction)<br>            alertController2.addAction(cancelAction)<br>            //顯示訊息視窗<br>            present(alertController2, animated: true, completion: nil)<br>        } else if score &gt;= 40 &amp;&amp; score &lt; 60 {<br>            let alertController3 = UIAlertController(title: &quot;總分\(score)分&quot;, message: &quot;加油加油!&quot;, preferredStyle: .alert)<br>            //確認按鈕<br>            let okAction = UIAlertAction(title: &quot;再玩一次&quot;, style: .default) { _<br>                in<br>                self.playAgain()<br>            }<br>            //取消按鈕<br>            let cancelAction = UIAlertAction(title: &quot;結束&quot;, style: .destructive) {<br>                _ in<br>                self.dismiss(animated: true)<br>            }<br>            //將按鈕加入訊息視窗<br>            alertController3.addAction(okAction)<br>            alertController3.addAction(cancelAction)<br>            //顯示訊息視窗<br>            present(alertController3, animated: true, completion: nil)<br>        } else {<br>            let alertController4 = UIAlertController(title: &quot;總分\(score)分&quot;, message: &quot;可以多看一些卡通喔！&quot;, preferredStyle: .alert)<br>            //確認按鈕<br>            let okAction = UIAlertAction(title: &quot;再玩一次&quot;, style: .default) { _<br>                in<br>                self.playAgain()<br>            }<br>            //取消按鈕<br>            let cancelAction = UIAlertAction(title: &quot;結束&quot;, style: .destructive) {<br>                _ in<br>                self.dismiss(animated: true)<br>            }<br>            //將按鈕加入訊息視窗<br>            alertController4.addAction(okAction)<br>            alertController4.addAction(cancelAction)<br>            //顯示訊息視窗<br>            present(alertController4, animated: true, completion: nil)<br>        }<br>    }</pre><blockquote><strong>App實際操作</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*NKYN_0B4LTPiCipR7r5aWA.gif" /></figure><blockquote><strong><em>遇到的困難</em></strong></blockquote><p>當app執行的過程中，會偶發性地出現「Fatal error: Index out of range 」錯誤，經Peter提醒，回頭檢查才發現是array中某一題的四個button選項有少寫一個，所以只要遇到該題，app就會死掉，深深覺得這個失誤很不可取🫠須謹惕自己!!!</p><blockquote><strong><em>GitHub</em></strong></blockquote><p><a href="https://github.com/alicelee812/CartoonQA">GitHub - alicelee812/CartoonQA</a></p><blockquote><strong>Reference</strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/swift-uikit-ios-app-%E5%85%A5%E9%96%80%E6%B8%AC%E9%A9%97-1-5164b7318d40">Swift UIKit iOS App 入門測驗 1</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E7%B7%B4%E7%BF%92%E7%AD%86%E8%A8%98-43-%E5%95%8F%E7%AD%94%E9%A1%8Capp-%E5%9F%8E%E5%B8%82%E5%9C%B0%E6%A8%99%E5%A4%A7%E6%8C%91%E6%88%B0-733eac886dfa">練習筆記 43 ：問答題App-城市地標大挑戰</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/47-%E9%81%B8%E6%93%87%E9%A1%8C-app-61de8734559e">#47 選擇題 APP</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/38-%E9%81%B8%E6%93%87%E9%A1%8C-app-sharpimageview-performsegue-identifier-uialertcontroller-1e8f470577b0">#38 選擇題 App、SharpImageView、performSegue ＆ Identifier、UIAlertController</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E8%AE%93-view-%E8%AE%8A%E5%9C%93%E8%A7%92%E7%9A%84-layer-cornerradius-54aa7afda2a1">讓 view 變圓角的 layer.cornerRadius</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E8%AA%8D%E8%AD%98-swiftui-%E7%9A%84-alert-83c41ddedca">認識 SwiftUI 的 Alert</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a186887cc12" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/3-%E9%81%B8%E6%93%87%E9%A1%8Capp-%E5%8D%A1%E9%80%9A%E4%BA%BA%E7%89%A9%E7%8C%9C%E7%8C%9C%E6%A8%82-a186887cc12">#4 選擇題App｜卡通人物猜猜樂</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#3 Table View ｜來聽地表最強男人唱歌]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/3-table-view-%E4%BE%86%E8%81%BD%E5%9C%B0%E8%A1%A8%E6%9C%80%E5%BC%B7%E7%94%B7%E4%BA%BA%E5%94%B1%E6%AD%8C-34d3b39346a3?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/34d3b39346a3</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Mon, 13 Feb 2023 10:19:59 GMT</pubDate>
            <atom:updated>2023-02-13T10:19:59.162Z</atom:updated>
            <content:encoded><![CDATA[<p>說到最喜歡的男歌手，絕對非杰倫莫屬！但自從去年聽過伍佰的演唱會後，真的覺得太強了～讓我們一起來聽聽他們的歌吧😆</p><p>表格的呈現方式可以使用Table View Controller 或 View Controller + Table View，這次是使用Table View Controller來練習表格的基本功能。</p><blockquote><strong><em>學習目標</em></strong></blockquote><ol><li>客製 cell 顯示表格的內容</li><li>IBSegueAction 傳送資料</li><li>多個section製作 (實際只有呈現一個section）</li><li>dequeueReusableCell 傳入 cell reuse ID (使用 extension)</li><li>cell 採用固定高度，cell 裡的元件設定 auto layout 條件</li><li>Tab Bar Controller / Navigation Bar Controller / View Controller三者之應用</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n0u8wVpzkUs9wByLXzbnVQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SxMNvMni2F7kL3g-x6Qf3Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ZXPqfdOASyoqg2O1DMFdA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJopfvoiHFMyYm4z4Pk3EA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*pulPm9rj_DI6gPAjP6u7nw.gif" /></figure><blockquote><strong><em>StoryBoard架構</em></strong></blockquote><ul><li>Tab Bar: 選擇要聽周杰倫或是伍佰的歌</li><li>選周杰倫 <strong>→ <br></strong>會到第二頁(Jay)TableViewController: 可選擇要聽哪一首歌 <strong>→</strong><br><strong> </strong>會到第三頁AV Player View Controller: 播放該首歌MV</li><li>選伍佰 <strong>→ <br></strong>會到第二頁(WuBai)TableViewController: 可選擇要聽哪一首歌 <strong>→</strong><br><strong> </strong>會到第三頁AV Player View Controller: 播放該首歌MV</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3ABACbEjbFBycXbQDB-Xg.png" /><figcaption>storyboard架構</figcaption></figure><h4>在Assets中加入專輯圖片</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jz4BrSQdPx_K0rFJZHhmGg.png" /><figcaption>Assets加圖片</figcaption></figure><blockquote><strong><em>程式撰寫</em></strong></blockquote><h4>自訂型別(Concert)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NMsJ2qRAPm0wYr1lie2NTg.png" /><figcaption>自訂型別儲存多個資料</figcaption></figure><h4>(Jay/WuBai) Table View Controller</h4><p>這次會從兩位歌手出道至今的每張專輯中選出一首歌列出來，雖然已確定共會列出30首歌曲，但因為數量較多，因此將會以<strong>Dynamic Prototypes</strong>呈現。</p><p>— (小筆記) Table View Cell可以分為兩類 —</p><p><strong>[靜態Static Cells]:</strong></p><ol><li>只能搭配Table View Controller。</li><li>可固定顯示格數，適合用在少量且確定格數的地方。</li><li>可直接進行排版，不需寫程式。</li></ol><p><strong>[動態Dynamic Prototypes]:</strong></p><ol><li>不局限於只能搭配Table View Controller。</li><li>適合用在會顯示很多儲存格及不確定cell數量的時候。</li><li>可透過storyboard排版，但必須以程式顯示。</li></ol><p>1. 建立Table View Controller 與 Table View Cell 類別</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9tzLrh40JqBD_nX931AwQ.png" /><figcaption>Table View Controller已遵從DataSource(顯示畫面) &amp;Delegate(觸發元件)，不需自行設定。</figcaption></figure><p>2. Table View Cell設定ID，style選擇custom</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*wcXI0tbSexuMWLM5iDsHdg.png" /></figure><p>3. 將要顯示於cell中的元件拉<em>@IBOutlet</em> 在Table View Cell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bOB2ogbH-bbOwgq5zMqP3Q.png" /></figure><p>4. 建立儲存Jay屬性的資料</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fuCEQNy_jO3OrmPMkeWgug.png" /></figure><p>5. 表格顯示資料，實作協定UITableViewDataSource的function</p><p>▪️表格有幾段</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TO2Pzi76GgzNf5SgJdMxqg.png" /><figcaption><strong>numberOfSections </strong>(in tableView: UITableView) -&gt; Int</figcaption></figure><p>▪️每一段表格有幾列</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U-m3ZV53fXTrHiQG7rTTSg.png" /><figcaption>tableView(_ tableView: UITableView, <strong>numberOfRowsInSection</strong> section: Int) -&gt; Int</figcaption></figure><p>▪️回傳哪一個cell，可從參數 indexPath 得到 section &amp; row</p><p><strong>tableView.dequeueReusableCell(withIdentifier: for: )</strong>可以重複使用設有Id名稱的cell，並利用as!或as?轉型，取得cell裡的資訊。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tf8lRKt36RYrlkOWgoVT3g.png" /><figcaption>tableView(_ tableView: UITableView, <strong>cellForRowAt</strong> indexPath: IndexPath) -&gt; UITableViewCell</figcaption></figure><p>6.以IBSegueAction 傳資料到下一頁(JayMV/WuBai)View Controller</p><p><em>(下方說明如何拉線）</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0NwuqSD9cv7hJYizDOX5YQ.png" /><figcaption>搭配 if let 讀取 indexPathForSelectedRow</figcaption></figure><h4>(JayMV/WuBai) View Controller</h4><p>點選歌曲後，在此頁面顯示歌曲MV (YouTube)，在頁面上加入WebKit View，並宣告儲存要顯示資料的屬性。</p><pre>var video: Jay!</pre><ol><li>此頁需import Webkit，並將WebKit View拉@IBOutlet</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y1CzNwrugKsZfA8WKIHGcg.png" /></figure><p>2. Table View Cell拉segue到WebKit View (要顯示的YouTube畫面)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MsHjEExC1hOMNvcMWEzGdw.png" /></figure><p>3. 從 segue 拉線產生<em>@IBSegueAction </em>(程式寫在起始頁)，產生下個畫面的controller，將儲存格的資料傳至下一頁</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BHfLY82Oz3OHCdsBwc4weg.png" /></figure><pre>@IBSegueAction func playMV(_ coder: NSCoder) -&gt; JayMVViewController? {<br>        if let section = tableView.indexPathForSelectedRow?.section,<br>            let row = tableView.indexPathForSelectedRow?.row{<br>            let controller = JayMVViewController(coder: coder)<br>            controller?.video = jayConcert[row]<br>            return controller<br>        }<br>        return nil<br>    }</pre><p>4. 使用WebKit View播放YouYube</p><pre>import UIKit<br>import WebKit<br><br>class JayMVViewController: UIViewController {<br>    <br>    var video: Jay!<br><br>    @IBOutlet weak var JWebView: WKWebView!<br>    <br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        <br>        //navigation title會顯示歌曲名稱<br>        navigationItem.title = video.songName<br>        <br>        if let url = URL(string: &quot;\(video.videoURL)&quot;){<br>            let request = URLRequest(url: url)<br>            JWebView.load(request)<br>        }<br>    }</pre><blockquote><strong><em>遇到的困難</em></strong></blockquote><p>表格的應用還不是很熟悉，時常需要看著Peter的文章才能想起該如何使用，尤其是<strong>傳遞資料</strong>與<strong>表格中顯示cell</strong>這兩部分，需要再多多琢磨才行!</p><blockquote><strong><em>GitHub</em></strong></blockquote><p><a href="https://github.com/alicelee812/TableView">GitHub - alicelee812/TableView</a></p><blockquote><strong><em>Reference</em></strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/51-%E5%AF%A6%E4%BD%9C-table-view-%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-8f52cc9d2bf1">#51 實作 Table View 的基本功能</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-ibsegueaction-%E5%82%B3%E8%B3%87%E6%96%99%E5%88%B0%E4%B8%8B%E5%80%8B%E9%A0%81%E9%9D%A2%E7%9A%84-controller-ios-13-%E6%96%B0%E5%8A%9F%E8%83%BD-50813ba3822">利用 IBSegueAction 傳資料到下個頁面的 controller — iOS 13 新功能</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%A6%82%E4%BD%95%E5%BE%9E-interface-builder-%E5%8A%A0%E5%85%A5-table-view-%E7%9A%84-header-footer-4dd2e0c76171">table view 的 header &amp; footer</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/dequeuereusablecell-%E5%82%B3%E5%85%A5-cell-reuse-id-%E7%9A%84%E5%B9%BE%E7%A8%AE%E5%AF%AB%E6%B3%95-1871c28d3772">dequeueReusableCell 傳入 cell reuse id 的幾種寫法</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/7-%E5%AF%A6%E4%BD%9C%E8%A1%A8%E6%A0%BC-auto-layout-%E9%A9%9A%E6%81%90%E7%B3%BB%E5%88%97%E9%9B%BB%E5%BD%B1-tableviewcontroller-with-dynamic-prototypes-319feeb72d9e">#7 實作表格｜驚恐系列電影_TableViewController with Dynamic Prototypes</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/episode-92-%E5%AF%A6%E4%BD%9C-table-view-%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-312e6cf3aabd">Episode 92 — 實作 Table View 基本功能</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34d3b39346a3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/3-table-view-%E4%BE%86%E8%81%BD%E5%9C%B0%E8%A1%A8%E6%9C%80%E5%BC%B7%E7%94%B7%E4%BA%BA%E5%94%B1%E6%AD%8C-34d3b39346a3">#3 Table View ｜來聽地表最強男人唱歌</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#2 Collection View ｜用照片牆過過出國的癮]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/2-collection-view-%E7%94%A8%E7%85%A7%E7%89%87%E7%89%86%E9%81%8E%E9%81%8E%E5%87%BA%E5%9C%8B%E7%9A%84%E7%99%AE-668be273c9a8?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/668be273c9a8</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Tue, 25 Oct 2022 15:13:25 GMT</pubDate>
            <atom:updated>2022-10-26T06:00:14.558Z</atom:updated>
            <content:encoded><![CDATA[<p>因為疫情關係已經好久沒有出國了，雖然最近出國旅遊解禁了，但尚未買好機票的我，就先用各國美麗的風景照片過過出國的癮吧!!!</p><blockquote><strong><em>學習目標</em></strong></blockquote><ol><li>Collection View Controller</li><li>新增自訂的Cocoa Touch Class &amp; Swift File類別</li><li>自訂Struct型別</li><li>拉<em>@IBOutlet</em> &amp; <em>@IBAction</em></li><li><em>@IBSegueAction </em>傳送資料</li><li>Navigation Controller</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlk_VIAO6m-sj97C3j2Ggg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPL2xEZ7bDpRkzRwv6o8_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*scYYnpmm9EBjUgw4tZXKuA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*AXQPc3YfXzCcHvJsPuj6dg.gif" /></figure><blockquote><strong>StoryBoard架構</strong></blockquote><ul><li>第一頁(Country)ViewCountroller：有五大洲的按鈕</li><li>第二頁(Travel)CollectionViewController：依據點選的按鈕，在第二頁顯示該洲的國家圖片</li><li>第三頁(BigPic)ViewCountroller：點選國家圖片後，在第三頁會顯示大張圖片</li></ul><h4><em>第一頁(Country) View Controller五大洲按鈕</em></h4><ul><li>新增Cocoa Touch Class檔案，自訂View Controller類別，並改好對應的Class</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*vDZAQ_s6reKRbqE7SyMq6A.png" /><figcaption>產生自訂的View Controller類別</figcaption></figure><ul><li>建立5個Button，設為Stack View並設定以下條件：<br>Axis: 以垂直排列<br>Alignment: Stack View以Fill填滿的方式對齊可設置的範圍<br>Distribution: 以Fill Equally的方式排列與設置尺寸，讓Stack View中的所有Button元件大小相等</li><li>拉Navigation Controller &amp; 設定Navigation Bar樣式</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*ByLRDq3ZFcMkfHU47PKRuQ.png" /><figcaption>建立5個Button</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*wBZCTKstdJIwltMWa4uqIQ.png" /><figcaption>Stack View條件</figcaption></figure><h4>第二頁(Country) Collection View Controller顯示照片牆</h4><ul><li>從UIKit中建立Collection View Controller (dataSource和delegate已連線)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y1u7TLctAgXtqLxZ-eKHWg.png" /></figure><ul><li>新增Cocoa Touch Class與Swift File檔案，分別建立CollectionViewController與Collection View Cell類別，改好對應的Class，並設置Cell ID (名稱與Collection View Cell一樣)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*vi3AfPxZWnK4BTEjZoVYVQ.png" /><figcaption>新增自訂的 collection view controller &amp; collection view cell 類別</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*zvVcf9tR9L2pPyeeQk1Bjg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*FD1pPcp2FuJ3iF209r2zbA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*JLfuu52LuilLNTuPcwQOQA.png" /><figcaption>改好對應的Class，並設置Cell ID</figcaption></figure><ul><li>在Collection View Cell中新增一個image view與label，Collection View的 Estimate Size 要設為 None，這裡不透過 auto layout 計算 cell 大小。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*jaCDQ6YETopudLafOQyz6w.png" /><figcaption>新增image view與label</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*l2YYB6aY_0WZArZuqN1Bdw.png" /><figcaption>Estimate Size 設為 None，不透過 auto layout 計算 cell 大小</figcaption></figure><h4>第三頁(BigPic) View Controller顯示大張圖片</h4><ul><li>新增Cocoa Touch Class檔案，自訂View Controller類別，並改好對應的Class</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*sbwzRHrQ3MdTAtAG7Fm-_A.png" /><figcaption>產生自訂的ViewController類別</figcaption></figure><ul><li>新增一個image view與label至view中，並設置constraints</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mZQL_Ymz0g7RihxPwMijtg.png" /><figcaption>新增image view與label，設置constraints</figcaption></figure><h4>在Assets中加入要顯示的圖片</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KXwD-ziewKNYK9I8dQcOJA.png" /><figcaption>Assets加照片</figcaption></figure><blockquote><strong><em>程式撰寫</em></strong></blockquote><h4>自訂型別(Travel)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PVZttevQ57gnIpZ9PzQjKg.png" /><figcaption>自訂型別儲存多個資料</figcaption></figure><h4>第一頁 Country View Controller</h4><ul><li>五個按鈕拉<em>@IBOutlet</em> &amp; <em>@IBAction</em>，並拉Segue到第二頁Collection View Controller，要記得幫Segue ID取名稱</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZriWoavHvQOQdvt3kLaxQ.png" /><figcaption>五個按鈕拉<em>@IBOutlet</em> &amp; <em>@IBAction</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtHj1rDaz635G1A23mejuw.png" /><figcaption>拉Segue到第二頁Collection View Controller，幫Segue ID取名稱</figcaption></figure><ul><li>從 segue 拉線產生<em>@IBSegueAction</em>，產生下個畫面的controller，將資<br>料存入下個畫面的屬性中</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*--UKr90gX8AimekKnF7_Dg.png" /><figcaption>從 segue 拉線產生<em>@IBSegueAction function</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w9JNf789qA0Fjv_QLZJ8kA.png" /><figcaption>將資料存入下個畫面的property</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a56cbd486838479f628c9a77dabad066/href">https://medium.com/media/a56cbd486838479f628c9a77dabad066/href</a></iframe><h4>第二頁 Country Collection View Controller</h4><ul><li>刪除或註解以下兩段程式</li></ul><pre>第一段<br>//private let reuseIdentifier = &quot;Cell&quot;</pre><pre>第二段<br>// self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)</pre><p>拉<em>@IBOutlet</em> 至Travel Collection View Cell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R520ucydSqjIvJ6EVI5kVQ.png" /><figcaption>拉@IBOutlet</figcaption></figure><ul><li>宣告陣列</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd5b36472731d7cda3fa1507f8399fb8/href">https://medium.com/media/bd5b36472731d7cda3fa1507f8399fb8/href</a></iframe><ul><li>各國圖片顯示於Cell中</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/072ba2de77cc1c5160fe07f7978a9eeb/href">https://medium.com/media/072ba2de77cc1c5160fe07f7978a9eeb/href</a></iframe><ul><li>拉segue到第三頁，並從 segue 拉線產生<em>@IBSegueAction</em>，可點選cell中的圖片，在第三頁顯示大張圖片與文字</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hl-5LrV1cSiBZOO3WkfzCg.png" /><figcaption>拉Segue到第三頁Big Pic View Controller，幫Segue ID取名稱</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hLuqYPU6r2O5OG53wScKqQ.png" /><figcaption>從 segue 拉線產生<em>@IBSegueAction function</em></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e56f0a9ede85de173f66c8f27fef76f/href">https://medium.com/media/7e56f0a9ede85de173f66c8f27fef76f/href</a></iframe><h4>第三頁 Big Pic View Controller</h4><ul><li>拉<em>@IBOutlet</em>與宣告儲存的圖片與文字</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0ae5427d0ae5aa4f07a6d95da03c655/href">https://medium.com/media/b0ae5427d0ae5aa4f07a6d95da03c655/href</a></iframe><blockquote><strong><em>遇到的困難</em></strong></blockquote><ol><li>segue拉線的目標要注意是否跟程式碼一致，在第二頁傳送資料到第三頁時因為不一致，求救了Peter才得到解答😅</li><li>IBSegueAction Function不熟悉，還需要多多多多練習！</li></ol><blockquote><strong>GitHub</strong></blockquote><p><a href="https://github.com/alicelee812/CollectionView">GitHub - alicelee812/CollectionView</a></p><blockquote><strong>Reference</strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/53-collection-view-%E7%85%A7%E7%89%87%E7%89%86-c03f156de0c">#53 照片牆(Grid Photo Wall)</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E4%BD%9C%E6%A5%AD-36%E7%85%A7%E7%89%87%E7%89%86grid-photo-wall-a3554f481c94">作業#36照片牆Grid Photo Wall</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-flow-layout-%E7%9A%84-collection-view-%E8%A3%BD%E4%BD%9C%E7%85%A7%E7%89%87%E7%89%86-e31dad55a7ac">利用 flow layout 的 collection view 製作照片牆</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=668be273c9a8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/2-collection-view-%E7%94%A8%E7%85%A7%E7%89%87%E7%89%86%E9%81%8E%E9%81%8E%E5%87%BA%E5%9C%8B%E7%9A%84%E7%99%AE-668be273c9a8">#2 Collection View ｜用照片牆過過出國的癮</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#1 Auto Layout ｜Figma 設計圖練習]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/1-auto-layout-figma-%E8%A8%AD%E8%A8%88%E5%9C%96%E7%B7%B4%E7%BF%92-3948333a1168?source=rss-70b2d2ddce1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3948333a1168</guid>
            <dc:creator><![CDATA[Alice Lee]]></dc:creator>
            <pubDate>Tue, 18 Oct 2022 13:28:23 GMT</pubDate>
            <atom:updated>2022-10-27T01:48:18.345Z</atom:updated>
            <content:encoded><![CDATA[<p>上課時，Peter説Auto Layout跟會不會寫程式沒有關係，就算程式還不熟悉，這裡也可能學得不錯，因此決定將第一篇文章獻給Auto Layout，給自己一點信心！ 但沒想到實作時，其實需要注意的地方比想像中還多！！！</p><blockquote><strong><em>學習目標</em></strong></blockquote><ol><li>Add New Constraints 設定上下左右間距、長寬尺寸、等寬等高、長寬比</li><li>Align對齊元件</li><li>元件拉線設間距（按住 option的差別）</li><li>點選物件 &gt; Show the Size inspector &gt; constraints，可以看到每個元件的設定條件，並且可做修改</li><li>Auto Layout公式:<strong> </strong><em>First Item = or ≤ or ≥ Second Item * Multiplier + Constant</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*1ena5SGghYvclWgVEFMj5w.png" /></figure><blockquote><strong><em>Figma設計圖＆模仿成果</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8QKwGG-t6nPdRVnQWA-Fow.png" /><figcaption><a href="https://www.figma.com/file/G8eC4lqAS6fFy6X3vWXSsk/Food-Delivery-App-(Community)?node-id=0%3A1"><strong>Figma設計圖</strong></a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v5td_CclDn-1VGA2AqLWfg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q2vi0XQvw_3tkZNPahiTXA.png" /><figcaption><strong>模仿成果</strong></figcaption></figure><blockquote><strong><em>不同機型的呈現</em></strong></blockquote><p>以12 Pro Max、13 Pro Max 與14 Pro Max 的模擬器呈現以下由左至右結果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*r70tDnUno-1XqYoxIAkeow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*VCef-SSwGHd5Pn3cmQcMQw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*GRzbSt9e6lWJATcyI70_Mg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*rR2zHN89tdufj3P1HhDs0A.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*nG_B7Qn21ynzQmWlNd81rg.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*I1FcP5KFM-86tXB4eMV4gw.gif" /></figure><blockquote><strong><em>遇到的困難</em></strong></blockquote><p>不知道為什麼有些機型的模擬器跑起來版面會跑掉，目前主要是螢幕尺寸較小的機型，還需要再研究研究🥹</p><blockquote><strong><em>GitHub</em></strong></blockquote><p><a href="https://github.com/alicelee812/AutoLayout">GitHub - alicelee812/AutoLayout</a></p><blockquote><strong>Reference</strong></blockquote><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/%E5%BE%9E-figma-%E8%A8%AD%E8%A8%88%E5%9C%96%E7%B7%B4%E7%BF%92-auto-layout-ef1d434962a6">從 Figma 設計圖練習 Auto Layout</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3948333a1168" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/1-auto-layout-figma-%E8%A8%AD%E8%A8%88%E5%9C%96%E7%B7%B4%E7%BF%92-3948333a1168">#1 Auto Layout ｜Figma 設計圖練習</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>