<?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 Yaya Chen on Medium]]></title>
        <description><![CDATA[Stories by Yaya Chen on Medium]]></description>
        <link>https://medium.com/@yayachen?source=rss-9961884ac6a4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0mXU8m_KmPuwtdLW3zADzA.jpeg</url>
            <title>Stories by Yaya Chen on Medium</title>
            <link>https://medium.com/@yayachen?source=rss-9961884ac6a4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:37:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yayachen/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]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%96%AE%E5%AD%97app-d479284c074e?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d479284c074e</guid>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Tue, 13 Dec 2016 06:22:55 GMT</pubDate>
            <atom:updated>2016-12-15T14:11:27.340Z</atom:updated>
            <content:encoded><![CDATA[<p>倒數第二堂課的課堂實作 ，回家給APP穿新衣並完善其功能 。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*lNDDBCN2Q-gcgs_vOmJA3Q.gif" /></figure><p>先把peter提供的各個單字txt檔丟入專案資料夾中 ，因為缺了X的檔案，所以我自己隨便編寫了一個放進去 。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*8YxhwB91pTZ4Ya5yMuTGbw.png" /></figure><p>然後建立三個class檔案分別為 ：字母按鈕列表頁的mainViewController 、英文頁面的EnglishViewController跟中文頁面的ChineseViewController 。</p><p>拉出三個View Controller頁面：字母按鈕列表頁 、英文頁 、中文頁 ，並在&quot;字母按鈕列表頁&quot;跟&quot;中文頁&quot;前面插入Navigation Controller。</p><p>接著將26個字母按鈕拉segue(show)連線到EnglishViewController。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jkmiHvJHkIuGgn56hBidVg.png" /></figure><p>EnglishViewController(英文頁面)右上角的&quot;中文&quot;按鈕 ，拉segue(Present Modally)到中文頁前面的Navigation Controller，並將Transition選為&quot;Flip Horizontal&quot; 製造翻牌的動態效果。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*KoE8XtksQN-uW402xh0uPw.jpeg" /></figure><p>一切就緒後，便可以開始寫程式惹!</p><p>首先在mainViewController(字母按鈕列表頁)中 ，寫一個用來傳值的prepare() 。把按鈕上的文字傳到EnglishViewController(英文頁面)作為navigation bar上面的title。</p><pre>override func prepare(for segue: UIStoryboardSegue, sender: Any?){ <br>        let but = sender as! UIButton<br>        let controller = segue.destination as!<br>        EnglishViewController<br>        controller.alphabet = but.currentTitle!<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*verYQZGUq1nwnefuMFmqSg.png" /></figure><p>EnglishViewController(英文頁面)中，先宣告alphabet 、lineArray 、array、vocabularIndex四個變數。其中vocabularIndex是用來索引陣列中的資料。</p><pre>var alphabet:String = &quot;&quot; //navigation bar title上顯示的字<br>var lineArray = [&quot;&quot;] <br>var array = [&quot;&quot;]<br>var vocabularIndex = 0 //第幾筆資料</pre><p>將單字跟句子的兩個Label拉IBOutlet ，再將&quot;前一筆資料&quot;跟&quot;後一筆資料&quot;的按鈕拉IBAction。</p><pre>@IBOutlet weak var word: UILabel! //英文單字<br>@IBOutlet weak var sentenceLabel: UILabel! //英文句子<br>@IBAction func prevBtn(_ sender: Any) {} //前一筆資料的按鈕<br>@IBAction func nextBtn(_ sender: Any) {} //後一筆資料的按鈕</pre><p>在viewDidLoad()中，寫入</p><pre>self.navigationItem.title = alphabet<br>        <br>        let path = Bundle.main.path(forResource: alphabet, ofType: &quot;txt&quot;)<br>        <br>        let content = try! String(contentsOfFile: path!, encoding:String.Encoding.utf16) //接收路徑跟編碼<br>        <br>        array = content.components(separatedBy: &quot;\n&quot;)//依斷行切割資料<br>        <br>        show()</pre><p>(1)self.navigationItem.title = alphabet 是讓navigation bar的title顯示為前一頁(mainViewController)傳來的按鈕上的英文字</p><p>(2)let path = Bundle.main.path(forResource: alphabet, ofType: “txt”)，設定檔案的路徑，由前一頁傳來的alphabet值來決定呼叫哪個txt檔</p><p>(3)let content = try! String(contentsOfFile: path!, encoding:String.Encoding.utf16)，取得檔案裡的資料</p><p>(4)array = content.components(separatedBy: “\n”) ，將txt中的資料用斷行來分隔，並將分隔後的每一行資料放入array陣列中 。由於txt檔中的資料長成下面這樣 ：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*i8Snxb8fNKp24tchAxAZvg.png" /></figure><p>每行由 <strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子 </strong>組成，所以用斷行的方式將資料區隔開後，array的長相就會變成：[ <strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子,</strong> <strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子,</strong> <strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子</strong>]</p><p>之後再執行show()這個方法</p><pre>func show(){<br>        let line = array[vocabularIndex]<br>        lineArray = line.components(separatedBy: &quot;\t&quot;)//依空白切割資料<br>        let vocabulary = lineArray[0]<br>        let sentence = lineArray[2]<br>        word.text = vocabulary<br>        sentenceLabel.text = sentence<br>    }</pre><p>(1)一開始宣告var vocabularIndex = 0初始值為0 ，所以let line = array[0]，可以取出array陣列中的第一筆資料[<strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子</strong>]，也就是txt檔中的第一行資料 。</p><p>(2)接著 lineArray = line.components(separatedBy: “\t”)，用空格為依據來分隔資料。剛剛使用let line = array[0]取出了[<strong>英文單詞(空格)中文單詞(空格)英文句子(空格)中文句子</strong>]這筆資料後，用空格進行分隔後存入lineArray陣列後，就會變成[<strong>英文單詞,中文單詞,英文句子,中文句子</strong>]。</p><p>(3)let vocabulary = lineArray[0] =<strong>英文單詞</strong><br> let sentence = lineArray[2]=<strong>英文句子</strong></p><p>(4)將vocabulary跟 sentence的值指派給word跟sentenceLabel這兩個label做顯示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*HrKveseWv_g3SO9XTl0Yog.png" /></figure><p>然後回頭來處理prevBtn跟nextBtn這兩個按鈕的IBAction</p><pre>@IBAction func prevBtn(_ sender: Any) {<br>        vocabularIndex -= 1<br>        if vocabularIndex &lt; 0 {<br>            vocabularIndex = array.count-1<br>            //如果已經在第一行了，就秀最後一行<br>        }<br>        show()<br>    }<br>    <br>@IBAction func nextBtn(_ sender: Any) {<br>        vocabularIndex += 1<br>        if vocabularIndex &gt; array.count-1 {<br>            vocabularIndex = 0<br>            //如果已經在第一行了，就秀第一行<br>        }<br>        show()<br>    }</pre><p>按&quot;前一筆資料&quot;按鈕時，陣列索引值-1(vocabularIndex -= 1)，讀取前一筆資料。按&quot;後一筆資料&quot;按鈕時，陣列索引值+1(vocabularIndex+= 1)，讀取後一筆資料。</p><p>如果目前畫面中讀取的是第一行資料array[0]，按下&quot;前一筆資料&quot;的按鈕，就秀出txt檔中的最後一筆資料。</p><p>如何得知txt檔中有幾行，可以使用<strong>array.count</strong>這個方法來得知陣列中的元素數。例如array[]中有100筆資料，array.count就會=100，那要讀出最後一筆資料的寫法就是array[99]。因此當array[0]，0-1&lt;0， 就執行vocabularIndex = array.count-1來讀取最後一筆資料。</p><p>換言之當畫面中已經秀出最後一筆資料時，按了後一筆資料的按鈕，就將vocabularIndex值歸0，秀出第一筆資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*wszSJcJ_eL1XFbeYxo2CPQ.png" /></figure><p>再使用prepare()，將資料傳到下一頁(ChineseViewController)。因為EnglishViewController跟ChineseViewController中間夾了一個Navigation Controller，所以需要轉手一下。然後再將linearray陣列[<strong>英文單詞,中文單詞,英文句子,中文句子</strong>]中的lineArray[1]=中文單詞跟lineArray[3]=中文句子，還有用於顯示navigation title的alphabet值傳到ChineseViewController。</p><pre>override func prepare(for segue: UIStoryboardSegue, sender: Any?){<br>        <br>        let navController = segue.destination as! UINavigationController<br>        let chiController = navController.topViewController as! ChineseViewController<br>        <br>        chiController.vocabulary = lineArray[1]<br>        chiController.sentence = lineArray[3]<br>        chiController.alphabet = alphabet<br>        <br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*ek637MM3G8pt-73VXjRzNA.png" /></figure><p>ChineseViewController中，先宣告vocabulary 、sentence、alphabet三個變數來接值。然後將顯示&quot;中文單詞&quot;跟&quot;中文句子&quot;的label拉IBOutlet連線。</p><p>再從navigation bar上的&quot;英文&quot;按鈕拉IBAction，使用&quot; self.dismiss(animated: true, completion:nil)&quot;的方法返回英文頁。</p><pre>@IBAction func showEnglish(_ sender: Any) {<br>        self.dismiss(animated: true, completion:nil)<br>    } </pre><p>viewDidLoad()中，處理一下接值與顯示，就終於告一段落惹!</p><pre>word.text = vocabulary<br>sentenceLabel.text = sentence<br>self.navigationItem.title = alphabet</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*dUvIM4MXyTiD85Okxc8n2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QzMfhHjDa9DMUHJ3mc58Wg.jpeg" /></figure><p>這個月，姊姊從加拿大帶小外甥回台探親 ，下了班就只想跟寶包玩 ，把swift丟一邊!! 小寶包真得好可愛，不能只有我看到 。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*1ie4exV4aGDUTfhDSxqicA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e3ff7f66410308aabdaff16d7543286/href">https://medium.com/media/6e3ff7f66410308aabdaff16d7543286/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/938922c0487761a84fc67bf2e3adbcfb/href">https://medium.com/media/938922c0487761a84fc67bf2e3adbcfb/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c5b0f6b4fe5de4f72275ae305f74294/href">https://medium.com/media/5c5b0f6b4fe5de4f72275ae305f74294/href</a></iframe><p><a href="https://github.com/nthuyaya/161213vocabulary">GitHub - nthuyaya/161213vocabulary</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d479284c074e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%96%AE%E5%AD%97app-d479284c074e">單字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[一段鴨子學飛的旅程]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E4%B8%80%E6%AE%B5%E9%B4%A8%E5%AD%90%E5%AD%B8%E9%A3%9B%E7%9A%84%E6%97%85%E7%A8%8B-cd3acf368c19?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd3acf368c19</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Thu, 08 Dec 2016 09:42:20 GMT</pubDate>
            <atom:updated>2016-12-22T05:26:30.530Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Zr3nSkaRpTcakujTB2mejQ.gif" /></figure><p>之前曾入入續續接觸過程式 ，但礙於邏輯腦細胞大量缺乏 ，不管學java 、c++或是action script ，總是輕觸皮毛便無疾而終 。</p><p>後來看到身邊一些工程師朋友開始很潮地學objective-c，心中總想著，IOS APP 程式，是個鴨不可及的世界，我還是滑滑手機開心開心就好。</p><p>踏入社會後，一直以平面設計及網頁設計為己職，總覺得掌握了RWD 的網頁技術就能好好地活著 。但日子一天天地過去，常常會莫名地煩躁起來，覺得如果不再去多學一些東西，終究會被後浪踩死在沙灘上。</p><p>本來想去學現今很流行的After Effect動畫</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*X0wdNS1c4cDzAmYs3IeGZQ.gif" /><figcaption><a href="https://dribbble.com/shots/3135412-Animated-scroll-of-Pokemom">https://dribbble.com/shots/3135412-Animated-scroll-of-Pokemom</a></figcaption></figure><p>報名了課程，卻因人數不足而沒能開成 。藍受香菇之餘，無意在facebook的<a href="https://www.facebook.com/groups/TaiwanUIUXDesigners/">TaiwanUIUXDesigners社團</a>中，看見了peter宣傳的<a href="http://learniosfromzero.strikingly.com/">文組班IOS課程廣告</a></p><blockquote><strong>專為文組生量身訂做，從零開始學程式，<br>開開心心寫 App !<br>只要 10 個星期，每週一個晚上，<br>即可學會程式設計的核心概念，<br>成為酷炫的 iOS App 開發魔法師 !</strong></blockquote><p>看了這段介紹，便決定先把After Effect動畫拋到一旁，立刻報名了peter的文組班IOS APP程式課。</p><p>還記得報名成功後，peter列出了12條課前準備項目，當時覺得有事嗎?上個課而已，竟然要先註冊一堆有的沒的帳號，這不叫擾民甚麼才叫擾民?</p><p>後來發現，這擾民還擾得頗有價值，光光trello跟medium兩個平台~就讓人有種走在時代尖端的感覺。</p><p>深深覺得利用medium來寫作業是一件很酷炫的事，不僅可以做為自己的學習歷程，還可以當成備忘錄來使用。班上同學的作業看不夠，還可以把爪牙伸向隔壁班同學跟前幾屆學長姐的作業呢 ！</p><p>在此不忘感謝常常被我偷看作業的隔壁班同學 ：<a href="https://medium.com/@zoeytsou"><strong>Zoey</strong></a> 、<a href="https://medium.com/@z1235678"><strong>0+</strong></a> 、<a href="https://medium.com/@leoj72955"><strong>暐潔</strong></a>，他們極具創意又講解仔細的教學文章，益我良多。</p><p>記得peter的課程宣傳下方，很多人留言推薦他的課程十足幽默風趣。當時我不信，覺得多半是工程師自我感覺良好的宅式笑話。結果上了課後，竟然不爭氣地笑惹 ！他的上課方式果真十分有趣，喜歡在投影片上放入一些不是重點的重點、逼別人當眾承認一堆有愧於心的答案、三不五時慫恿同學開喉歌唱(上課時看peter跟hiro鬥嘴成為了一種享受)，最重要的，他總是能用一些簡單風趣的例子，讓你理解難以消化的程式概念，這點不推不行。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PC1dpbrIhTOsEnZIhdwBgA.jpeg" /></figure><p>peter回答問題的速度也是一整個驚人，不管多晚敲他，總是能立刻給你一個滿意的答案。有時不禁懷疑他到底是小飛俠還是蝙蝠俠，都不用睡覺的。</p><p>不僅如此，有次我因為出國玩樂而缺課一堂，Peter還說，如果有需要，他隨時可以幫我補上，真是人美心善良(疑!?)</p><p>如果你問我，一個沒有程式基礎，邏輯概念很差的人，來修讀彼得潘的文組生課程，究竟適不適合。我只能說，越到課程後段，吸收的速度會越來越慢，感受到的挫折也會越來越大。但是但是但是，如果每次課後都能如實練習上課所教的東西，不管多忙都花時間去完成作業。那麼當你順利寫出作業時，產生的成就感絕對會遠遠大於上課聽不懂的挫折感。而且那時，才算是把上課所學之事真真切切地吸收為自己所用。<strong>寫作業很重要！寫作業很重要！寫作業很重要！</strong>因為太過重要，所以要說三次。很多人覺得上課聽懂就好，寫不寫作業是其次，但以我自身經歷來說，如果不寫作業加深印象，下次上課絕對忘個精光。</p><p>最後還是要感謝peter帶我走進這個本以為鴨不可及的swift世界，讓我有機會讓不會舞動的設計元素在IOS APP的大平台上跳躍。推薦給所有跟我一樣邏輯概念很差，金魚系記憶力低，認為這輩子注定與程式絕緣的文組生們，勇敢踏出去，跟著小飛俠一起飛翔，收穫絕對超乎你的想像。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3FHAIl2lRkCLy9BBiEgYaA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd3acf368c19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E4%B8%80%E6%AE%B5%E9%B4%A8%E5%AD%90%E5%AD%B8%E9%A3%9B%E7%9A%84%E6%97%85%E7%A8%8B-cd3acf368c19">一段鴨子學飛的旅程</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[多頁面App間的資料傳遞(2)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%A4%9A%E9%A0%81%E9%9D%A2app%E9%96%93%E7%9A%84%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-2-15ee72412bfa?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/15ee72412bfa</guid>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Wed, 07 Dec 2016 17:15:20 GMT</pubDate>
            <atom:updated>2016-12-08T14:58:08.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*dbKmrBvOsdQfTLvGBnHVvA.gif" /></figure><p>自從做了多頁面App間的資料傳遞(1)後</p><p>就一直想把修改與回傳的功能也加進去</p><p>但實在太困難惹～困擾了我好多天～一直解不出來</p><p>看了好幾次peter的講義～再反覆參考隔壁班同學的作業</p><p>上班一有空閒就纏著IOS工程師勇勇問問題</p><p>終於把這煩人的玩意給解決惹！！</p><p>主題是聖誕歌曲</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*USUj5m746wbgmT0U-IZDcQ.png" /></figure><p>首先先建立分別指派給三個頁面的class</p><p>SongViewController(第一頁)</p><p>DetailViewController(第二頁)</p><p>EditViewController(第三頁)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*oIDU2Coev46AdqhtvajlsA.png" /></figure><p>然後從SongViewController(第一頁)拉一條Controller to Controller</p><p>的segue(show)到DetailViewController(第二頁)</p><p>並幫此條segue的Identifier取名為goDetail</p><p>在SongViewController(第一頁)中放入5個按鈕時</p><p>同時設定它們的Restoration ID～五個按鈕依序為01234</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*sOuGb6Gec3kzeZGNwrUXzQ.png" /></figure><p>接著在SongViewController(第一頁)中建立五個按鈕的IBOutlet連線</p><p>以及一條代表Controller to Controller的IBAction連線</p><pre>@IBOutlet weak var song1Button: UIButton!<br>@IBOutlet weak var song2Button: UIButton!<br>@IBOutlet weak var song3Button: UIButton!<br>@IBOutlet weak var song4Button: UIButton!<br>@IBOutlet weak var song5Button: UIButton!</pre><pre>@IBAction func goDetail(_ sender: UIButton) {</pre><pre>self.performSegue(withIdentifier: &quot;goDetail&quot;, sender: sender)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*xqOB6yEJmpFEW-joMYcQtA.png" /></figure><p>依舊在SongViewController(第一頁)中</p><p>宣告一個名為Song包含order~name~singer~lyrics四個屬性的struct</p><pre>struct Song {<br>var order = 0<br>var name = &quot;&quot;<br>var singer = &quot;&quot;<br>var lyrics = &quot;&quot;<br>}</pre><p>宣告一個存取歌曲歌名～歌手跟歌詞的陣列songData</p><pre>var songData = [</pre><pre>Song(order: 0, name : &quot;Last Christmas&quot;, singer : &quot;Wham&quot;, lyrics : &quot;Last Christmas\nI gave you my heart\nBut the very next day\nyou gave it away\nThis year to save me from tears\nI&#39;ll give it to someone special&quot;),</pre><pre>Song(order: 1, name : &quot;Santa Tell Me&quot;, singer : &quot;Ariana Grande&quot;, lyrics : &quot;Santa, tell me if you&#39;re really there?\nDon&#39;t make me fall in love again if he won&#39;t be here\nNext year\nSanta, tell me if he really cares?&quot;),</pre><pre>Song(order: 2, name : &quot;Mistletoe&quot;, singer : &quot;Justin Bieber&quot;, lyrics : &quot;With you,shawty with you\nWith you,shawty with you\nWith you,under the mistletoe&quot;),</pre><pre>Song(order: 3, name : &quot;Silent Night&quot;, singer : &quot;Kelly Clarkson&quot;, lyrics : &quot;Silent night, holy night!\nAll is calm, all is bright.\nRound yon Virgin,\nMother and Child.\nHoly infant so tender and mild,\nSleep in heavenly peace,\nSleep in heavenly peace&quot;),</pre><pre>Song(order: 4, name : &quot;Joy To The World&quot;, singer : &quot;Pentatonix&quot;, lyrics : &quot;Joy to the World , the Lord is come!\nLet earth receive her King;\nLet every heart prepare Him room,\nAnd Heaven and nature sing,\nAnd Heaven and nature sing,\nAnd Heaven, and Heaven,\nand nature sing.&quot;)</pre><pre>]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*gA_PkWOgySmn1LvPhJsYtg.png" /></figure><p>利用prepare傳值，一個把值準備好傳過去的概念</p><pre>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {</pre><pre>let button = sender as! UIButton</pre><pre>//取得button的Restoration ID</pre><pre>let idx = Int(button.restorationIdentifier!) ?? 0</pre><pre>let controller = segue.destination as! DetailViewController</pre><pre>//從songData裡面取出當對應的歌曲傳到下一頁</pre><pre>controller.song = songData[idx]</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*byHtTAiIWINLZHIvzQCwxw.png" /></figure><p>來到DetailViewController(第二頁)</p><p>建立歌名～歌手跟歌詞三個label的連線</p><pre>@IBOutlet weak var songLabel: UILabel!<br>@IBOutlet weak var singerLabel: UILabel!<br>@IBOutlet weak var lyricTextView: UITextView!</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*CGwQWAnBXK1KlbPeYWGVow.png" /></figure><p>在viewDidLoad()中寫下</p><pre>songLabel.text = song.name<br>singerLabel.text = song.singer<br>lyricTextView.text = song.lyrics</pre><p>來顯示從SongViewController(第一頁)傳來的值</p><p>一樣利用prepare來把值傳給EditViewController(第三頁)</p><pre>var song : Song = Song()</pre><pre>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {<br>let controller = segue.destination as! EditViewController<br>controller.song = song</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*-RF58oGp64q6AyN4wTS30A.png" /></figure><p>別忘了要把DetailViewController(第二頁)右上角的「編輯」按鈕</p><p>拉一條segue(show)到EditViewController(第三頁)</p><p>現在來看看最後一頁吧！EditViewController</p><p>一樣拉出三個Textfield的IBOutlet連線</p><pre>@IBOutlet weak var songLabel: UITextField!<br>@IBOutlet weak var singerLabel: UITextField!<br>@IBOutlet weak var lyricTextView: UITextView!</pre><p>然後從右上角的「完成」按鈕～拉一條IBAction連線到程式區</p><p>當使用者編輯完～按下「完成」按鈕後</p><p>發送修改通知出去，這邊使用Notification的方法來傳值</p><pre>@IBAction func finished(_ sender: Any) {</pre><pre>let notificationName = Notification.Name(&quot;songUpdated&quot;)</pre><pre>//取畫面上的值<br>song.name = songLabel.text!<br>song.singer = singerLabel.text!<br>song.lyrics = lyricTextView.text!</pre><pre>//發送通知<br>NotificationCenter.default.post(name: notificationName, object: nil, userInfo: [&quot;song&quot;:self.song]) //傳一個名稱叫”song”的song值過去</pre><pre>//回到前一頁<br>self.navigationController?.popViewController(animated: true)</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*Y99MnZok-FJjU_tFuEP0sQ.png" /></figure><p>然後回到DetailViewController(第二頁)</p><p>在viewDidLoad()中寫下申請接受通知的code</p><p>當收到通知時～執行songUpdated這個方法</p><pre>let notificationName = Notification.Name(&quot;songUpdated&quot;)</pre><pre>NotificationCenter.default.addObserver(self, selector: #selector(songUpdated(noti:)), name: notificationName, object: nil)</pre><pre>updateInfo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*kaIUu1uYmZjwgEjGvH4krg.png" /></figure><p>然後寫出一個更新頁面資訊的函式updateInfo()</p><pre>func updateInfo(){<br>songLabel.text = song.name<br>singerLabel.text = song.singer<br>lyricTextView.text = song.lyrics<br>}</pre><p>以及當收到通知時要執行的函式songUpdated()</p><pre>func songUpdated(noti:Notification) {<br>song = noti.userInfo![&quot;song&quot;] as! Song<br>updateInfo()<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*qYmJRbCMf0s73RxyqT9oXw.png" /></figure><p>第一頁也是</p><p>在viewDidLoad()中申請接受通知</p><pre>let notificationName = Notification.Name(&quot;songUpdated&quot;)</pre><pre>NotificationCenter.default.addObserver(self, selector: #selector(songUpdated(noti:)), name: notificationName, object: nil)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*Lr9AqF5NCYsxo9DoFyG6cw.png" /></figure><p>比照第二頁的作業</p><p>寫出一個更新頁面資訊的函式updateInfo()</p><pre>func updateInfo(){</pre><pre>song1Button.setTitle(songData[0].name, for: UIControlState.normal)<br>song2Button.setTitle(songData[1].name, for: UIControlState.normal)<br>song3Button.setTitle(songData[2].name, for: UIControlState.normal)<br>song4Button.setTitle(songData[3].name, for: UIControlState.normal)<br>song5Button.setTitle(songData[4].name, for: UIControlState.normal)</pre><pre>}</pre><p>以及當收到通知時要執行的函式songUpdated()</p><pre>func songUpdated(noti:Notification) {</pre><pre>let song = noti.userInfo![&quot;song&quot;] as! Song</pre><pre>songData[song.order] = song</pre><pre>//比對修改的按鈕去更新</pre><pre>if song.order == 0{</pre><pre>song1Button.setTitle(song.name, for: UIControlState.normal)</pre><pre>}else if song.order == 1{</pre><pre>song2Button.setTitle(song.name, for: UIControlState.normal)</pre><pre>}else if song.order == 2{</pre><pre>song3Button.setTitle(song.name, for: UIControlState.normal)</pre><pre>}else if song.order == 3{</pre><pre>song4Button.setTitle(song.name, for: UIControlState.normal)</pre><pre>}else if song.order == 4{</pre><pre>song5Button.setTitle(song.name, for: UIControlState.normal)</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*Q3VDsT2M9WRiLEoj0YbLLg.png" /></figure><p>如此便完成了</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4622341e537f88e07eb5f4b007c54c5f/href">https://medium.com/media/4622341e537f88e07eb5f4b007c54c5f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1888bf6e81fef7da85c8312c0ecedff6/href">https://medium.com/media/1888bf6e81fef7da85c8312c0ecedff6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2e1e97f7ee296531ccc526bf564f705/href">https://medium.com/media/f2e1e97f7ee296531ccc526bf564f705/href</a></iframe><p><a href="https://github.com/nthuyaya/161207christmassong">nthuyaya/161207christmassong</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15ee72412bfa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%A4%9A%E9%A0%81%E9%9D%A2app%E9%96%93%E7%9A%84%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-2-15ee72412bfa">多頁面App間的資料傳遞(2)</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[陣列-問答題(PTT鄉民實測)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E9%99%A3%E5%88%97-%E5%95%8F%E7%AD%94%E9%A1%8C-ptt%E9%84%89%E6%B0%91%E5%AF%A6%E6%B8%AC-c7318e9bf915?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7318e9bf915</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Thu, 01 Dec 2016 08:19:06 GMT</pubDate>
            <atom:updated>2016-12-01T10:39:42.974Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>畫面上顯示2個 label，一個問題 label，一個答案 label。2個 button，一個 ”下一題 ”button，一個 ”顯示答案 ” button。全部10個問題，問題會循環。一開始答案 label空白，點選 ”顯示答案 ”button後才會顯示答案。</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*HE-ht9kXeFLG2iqXHHsGww.gif" /></figure><p>這次的作業要練習 Array(陣列) 和Dictionary 的結合~我的問答題是”PTT鄉民實測”</p><p>首先~先建立@IBOutlet連線</p><pre>@IBOutlet weak var countLabel: UILabel! //第幾題<br>@IBOutlet weak var questionLabel: UILabel! //題目<br>@IBOutlet weak var answerLabel: UILabel! //答案<br>@IBOutlet weak var fox1: UIImageView! //未答的狐狸圖<br>@IBOutlet weak var fox2: UIImageView! //回答問題的狐狸圖</pre><p>再來建立一個跟class很類似的struct</p><p>裡面宣告 question跟 answer兩個變數~初始值都讓它們放空</p><pre>struct Qna{<br>var question = &quot;&quot;<br>var answer = &quot;&quot;<br>}</pre><p>接著列出一個存取題目跟答案的陣列~並宣告變數count的初始值為0</p><p>讓題目可以從陣列中的第一個值開始讀取</p><pre>var count = 0</pre><pre>var qnaArray = [Qna(question: &quot;3樓的樓上是幾樓？&quot;, answer: &quot;2樓&quot;),<br>Qna(question: &quot;看到哪三個數字需下跪？&quot;, answer: &quot;118&quot;),<br>Qna(question: &quot;年過30保有童貞的男性為？&quot;, answer: &quot;魔法師&quot;),<br>Qna(question: &quot;PTT戰神是？&quot;, answer: &quot;蘇美&quot;),<br>Qna(question: &quot;obov是哪個教派的教主?&quot;, answer: &quot;母豬教&quot;),<br>Qna(question: &quot;8+9=?&quot;, answer: &quot;八家將&quot;),<br>Qna(question: &quot;2沒壞,猜一字？&quot;, answer: &quot;噓&quot;),<br>Qna(question: &quot;PTT女板又名？&quot;, answer: &quot;灑花板&quot;),<br>Qna(question: &quot;Z&gt;B是什麼意思？&quot;, answer: &quot;利大於弊&quot;),<br>Qna(question: &quot;鄉民爆料文的起始句是？&quot;, answer: &quot;我夢到&quot;)]</pre><p>上面是比較進階的寫法</p><p>也可以寫成下面這樣</p><pre>var qnaArray = [[&quot;question&quot;:&quot;3樓的樓上是幾樓？&quot;, &quot;answer&quot;:&quot;2樓&quot;],</pre><pre>[&quot;question&quot;:&quot;看到哪三個數字需下跪？&quot;, &quot;answer&quot;:&quot;118&quot;]]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*-oYeXH3V8V9wFyUFZcMVBA.png" /></figure><p>接著是SHOW ANSWER跟NEXT QUESTION兩個按鈕</p><p>用@IBAction連個線</p><p>秀答案的時候~將[有對話框的狐狸圖]顯示~[沒對話框的狐狸圖]隱藏</p><p>以及顯示呈現答案用的Label</p><pre>//秀答案<br>@IBAction func answerBtn(_ sender: Any) {<br>fox1.isHidden = true<br>fox2.isHidden = false<br>answerLabel.isHidden = false<br>}</pre><p>按下NEXT QUESTION的按鈕時</p><p>(1)讀取陣列資料的順位+1~</p><p>當讀取超過第10筆資料時~從頭開始~將count歸0</p><p>(2)執行nextQuestion()函式</p><p>(3)將[有對話框的狐狸圖]隱藏~並顯示[沒對話框的狐狸圖]</p><pre>//下一題<br>@IBAction func nextBtn(_ sender: Any) {</pre><pre>count += 1<br>if count == 10{<br>count = 0<br>}<br>nextQuestion()<br>fox1.isHidden = false<br>fox2.isHidden = true<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*ASD6f9luIqaKutIcFCEZlg.png" /></figure><p>nextQuestion()函式裡要寫些什麼呢?</p><p>(1)questionLabel.text = qnaArray[count].question</p><p>從陣列中取出題目~把值指派給questionLabel</p><p>(2)answerLabel.text = qnaArray[count].answer</p><p>從陣列中取出答案~把值指派給answerLabel</p><p>(3)顯示題號(因為陣列順序是從編號0開始,所以count+1就會是現在的題號)</p><p>(4)用 answerLabel.isHidden = true 來隱藏上一題的答案</p><pre>func nextQuestion(){</pre><pre>questionLabel.text = qnaArray[count].question<br>answerLabel.text = qnaArray[count].answer</pre><pre>countLabel.text = &quot;\(count+1)&quot;<br>answerLabel.isHidden = true</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*wevjokUCu4HsbWUBLbp49w.png" /></figure><p>完工!!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a8c639c2faaa043146b806a1faa05a/href">https://medium.com/media/10a8c639c2faaa043146b806a1faa05a/href</a></iframe><p><a href="https://github.com/nthuyaya/161201pttqna">nthuyaya/161201pttqna</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7318e9bf915" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E9%99%A3%E5%88%97-%E5%95%8F%E7%AD%94%E9%A1%8C-ptt%E9%84%89%E6%B0%91%E5%AF%A6%E6%B8%AC-c7318e9bf915">陣列-問答題(PTT鄉民實測)</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[多頁面App間的資料傳遞(1)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%A4%9A%E9%A0%81%E9%9D%A2app%E5%92%8C%E9%A0%81%E9%9D%A2%E9%96%93%E7%9A%84%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-1-5b97e713561b?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b97e713561b</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Thu, 24 Nov 2016 07:45:28 GMT</pubDate>
            <atom:updated>2016-11-24T08:43:14.554Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>2個頁面。 頁面1:顯示曾經愛過情人的名字button ，假設有三個情人 (或三隻小豬，三部電影)。頁面2:顯示她(他)的名字，星座，照片。</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*pkymn2m572ZkqN3v3W-ABQ.gif" /></figure><p>這次的作業是簡易版的多頁面App和頁面間的資料傳遞</p><p>我想要做飲料店家的選擇</p><p>在第一頁點選飲料店名後~將飲料店的名字傳到第二頁</p><p>然後第二頁會依店名的不同顯現出不一樣的招牌飲品跟圖片</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*xNAT4KYZjBeMdAqJIPCgKQ.png" /></figure><p>還沒學swift程式之前</p><p>如果要做這種三個按鈕導向三個不同的內容的app</p><p>至少要畫4頁~</p><p>現在稍稍學會頁面間資料傳遞的技巧後~只要做2頁就行惹</p><p>建置UI前~先生出兩個新的controller類別</p><p>分別是drinkViewController跟detailViewController</p><p>然後分別指派給第一頁(drinkViewController)跟第二頁(detailViewController)</p><p>在第一頁放入3個按鈕</p><p>用segue(show)的方式將三個按鈕都拉到第二頁</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*3S9jfgsGbe6T1QhuSL1tfw.png" /></figure><p>接著在第二頁放上一張背景圖~顯示店名的label跟招牌飲品的label</p><p>然後將三個物件都用@IBOutlet的方式</p><p>在第二頁(detailViewController)的code區塊中建立連線</p><pre>@IBOutlet weak var namelabel: UILabel! //飲料店名</pre><pre>@IBOutlet weak var bgImageview: UIImageView! //背景圖</pre><pre>@IBOutlet weak var signaturelabel: UILabel! //招牌飲品</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*B3IUjmFVXaOQ9mUHFP3VHQ.png" /></figure><p>回到第一頁(drinkViewController)</p><p>在code區塊中寫入一個func prepare()函式</p><p>因為要根據使用者點選的按鈕上面的文字，來判斷要傳送什麼資料給下一個Controller，所以用sender as! UIButton的方式，將sender的值強制轉型成UIButton的型態， 這樣就能使用UIButton的currentTitle方法，取得使用者點選的按鈕上面的文字</p><p>同樣因為要存取detailViewController裡面name這個屬性，所以把segue.destination強制轉型成detailViewController(UIViewController中沒有這個name這個屬性，如果沒有進行轉型就會出錯)</p><pre>override func prepare(for segue: UIStoryboardSegue, sender:</pre><pre>Any?) {</pre><pre>let button = sender as! UIButton //把sender傳遞過來的資料強制轉型成UIButton的型態</pre><pre>let controller = segue.destination as!<br>detailViewController </pre><pre>//把segue.destination強制轉型成detailViewController</pre><pre>controller.name = button.currentTitle //把按鈕上的名字傳過去</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*KI8voFDHM-Dl6uVZ2vXJkg.png" /></figure><p>來到第二頁</p><p>先宣告一個變數name</p><p>當第一頁傳來的按鈕上面的值是”茶湯會”時</p><p>就顯示茶湯會所屬背景圖跟招牌飲品為”芋香翡翠拿鐵”</p><p>依此類推</p><p>就可以依照不同的按鈕值切換不同的內容畫面惹!</p><pre>var name: String!</pre><pre>override func viewDidLoad() {</pre><pre>super.viewDidLoad()</pre><pre>if name == &quot;茶湯會&quot; {</pre><pre>bgImageview.image = UIImage(named: &quot;bg1&quot;)</pre><pre>signaturelabel.text = &quot;芋香翡翠拿鐵&quot;</pre><pre>} else if name == &quot;迷客夏&quot; {</pre><pre>bgImageview.image = UIImage(named: &quot;bg2&quot;)</pre><pre>signaturelabel.text = &quot;大甲芋頭鮮奶&quot;</pre><pre>} else{</pre><pre>bgImageview.image = UIImage(named: &quot;bg3&quot;)</pre><pre>signaturelabel.text = &quot;珍珠抹茶拿鐵&quot;</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*GCDemJuNBYCZ8pA1G_5V3g.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42581fc73f07a53c38891f01722d369c/href">https://medium.com/media/42581fc73f07a53c38891f01722d369c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/500bd4f377c2a22c40b902d8b5f30d54/href">https://medium.com/media/500bd4f377c2a22c40b902d8b5f30d54/href</a></iframe><p><a href="https://github.com/nthuyaya/161124drinkchoose">nthuyaya/161124drinkchoose</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b97e713561b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%A4%9A%E9%A0%81%E9%9D%A2app%E5%92%8C%E9%A0%81%E9%9D%A2%E9%96%93%E7%9A%84%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-1-5b97e713561b">多頁面App間的資料傳遞(1)</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[猜數字]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E7%8C%9C%E6%95%B8%E5%AD%97-c0df89a56f23?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0df89a56f23</guid>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Wed, 23 Nov 2016 03:49:22 GMT</pubDate>
            <atom:updated>2016-11-23T03:49:22.320Z</atom:updated>
            <content:encoded><![CDATA[<p>上週彼得潘出國當爽人~課程暫停一次</p><p>很怕swift的記憶會隨著彼得潘一起遠去</p><p>只好看看其他班的同學都在玩些什麼~自己也來動手做看看</p><p>最後決定練習猜數字的遊戲</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*cSvVQqfnGodQAWnDoqtIHA.gif" /></figure><p>遊戲概念很簡單~就是要猜猜看星球上有幾朵玫瑰</p><p>只有6次機會~數字範圍是0-50</p><p>每猜一次~住在星球上的小狐狸都會給你一些提示</p><p>6次機會中猜中~小狐狸會送你一朵玫瑰~</p><p>機會用完還猜不中小狐狸則會送你兩行淚</p><p>正確答案用亂數產生 ~才能讓玩家可以無限重玩</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KuVZ7MKzl7iOUqRkeqDYTw.jpeg" /></figure><p>先把UI上的物件跟swift code連一連</p><p>其中的upperBound跟lowerBound兩個變數</p><p>是在玩家每猜完一次後~給予的最新範圍的溫馨提示</p><p>不然像我這種金魚腦的人~每次猜完都會忘記剛剛猜過什麼</p><pre>@IBOutlet weak var inputNumber: UITextField! //玩家輸入的數字</pre><pre>@IBOutlet weak var hint: UILabel! //溫馨提示</pre><pre>@IBOutlet weak var guessTime: UILabel!  //可猜的次數</pre><pre>@IBOutlet weak var rose: UIImageView!  //答對的獎勵 (先隱藏)</pre><pre>@IBOutlet weak var tear: UIImageView!  //答錯的淚水 (先隱藏)</pre><pre>var answer = 0 //答案初始值</pre><pre>var chance = 6 //生命初始值</pre><pre>var upperBound = 50 //最大範圍提示</pre><pre>var lowerBound = 0  //最小範圍提示</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*SIYuZQXaItYL3TV7b3ZvBg.png" /></figure><p>然後在 override func viewDidLoad()裡面</p><p>加上一個會自動產生0-50之間亂數的函式</p><p>就是每次遊戲啟動後~會自動產生的正確答案</p><pre>answer = Int(arc4random_uniform(50))</pre><p>現在來處理GO這個button~主要由四個條件構成</p><p>(1)如果玩家沒有輸入值<strong> <em>if inputNumber.text == “”</em></strong></p><p>不給猜~並叫他輸入</p><p>(2)如果玩家輸入的值不是數字<strong><em> if Int(inputNumber.text!) == nil</em></strong></p><p>不給猜~並叫他要輸入數字</p><p>(3)如果玩家生命值還沒用完 <strong><em>if chance &gt; 0</em></strong></p><p>那就執行check()這個主要用來驗證玩家是否猜中的函式</p><p>(4)如果玩家生命值用完了~就不給他猜了</p><p>並將原先隱藏起來的哭哭圖打開~哭給他看</p><pre>@IBAction func gobtn(_ sender: UIButton) {</pre><pre>inputNumber.resignFirstResponder() //收鍵盤<br></pre><pre>if inputNumber.text == &quot;&quot; {</pre><pre>hint.text=&quot;小狐狸正等著你給牠一個數字&quot; //使用者沒輸入<br></pre><pre>}else if Int(inputNumber.text!) == nil {</pre><pre>hint.text=&quot;只能輸入數字喔!&quot;  //使用者輸入的不是數字<br></pre><pre>}else if chance &gt; 0{</pre><pre>let i = Int(inputNumber.text!)!</pre><pre>check(test: i)<br></pre><pre>}else{</pre><pre>hint.text = &quot;小狐狸哭哭 沒機會了&quot;</pre><pre>tear.isHidden = false</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*J9jpL1ZdvQwbSZgWES5JUg.png" /></figure><p>在寫check()這個函式之前</p><p>先寫一個提示玩家最新範圍的函式 getBoundaryText()</p><pre>func getBoundaryText() -&gt; String {</pre><pre>return &quot;(\(lowerBound)~\(upperBound))&quot;</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*j8v0z1vo5F4YLRzzRVDX9A.png" /></figure><p>現在可以來處理這個主要運算的check()了</p><p>當前面的go button按下時</p><p>如果玩家生命值還大於0的時候~就會執行這個函式</p><pre>if chance &gt; 0{</pre><pre>let i = Int(inputNumber.text!)!</pre><pre>check(test: i)</pre><p>把玩家數入的值轉成整數傳給i這個常數</p><p>然後把這個i值傳給check(test:Int)來進行驗證</p><p>以下是check()的內容</p><pre>func check(test:Int){</pre><pre>if test &gt; 50{</pre><pre>hint.text = &quot;猜的數字超出範圍嚕!\(getBoundaryText())&quot;<br>chance -= 1<br>guessTime.text = String(chance)<br></pre><pre>}else if test &gt; answer {</pre><pre>upperBound = test - 1<br>hint.text = &quot;沒有這麼多朵\(getBoundaryText())&quot;<br>chance -= 1<br>guessTime.text = String(chance)<br></pre><pre>}else if test &lt; answer {</pre><pre>lowerBound = test + 1<br>hint.text = &quot;太少朵了吧!\(getBoundaryText())&quot;<br>chance -= 1<br>guessTime.text = String(chance)</pre><pre><br>}else {</pre><pre>hint.text = &quot;答對了!就是\(answer)朵 送你一朵玫瑰&quot;<br>rose.isHidden = false</pre><pre> }</pre><pre>}</pre><p>(1)當玩家輸入的值&gt;50的時候</p><p>告知玩家猜的數字超出範圍~並執行getBoundaryText()秀出最新範圍</p><p>同時生命值-1~然後印出玩家剩餘可猜次數</p><p>(2)當玩家輸入的值&gt;正確答案的時候</p><p>把玩家輸入的值-1做為最新範圍的最大值upperBound</p><p>告知玩家猜太多了~並執行getBoundaryText()秀出最新範圍</p><p>同時生命值-1~然後印出玩家剩餘可猜次數</p><p>(3)當玩家輸入的值&lt;正確答案的時候</p><p>把玩家輸入的值+1做為最新範圍的最小值upperBound</p><p>告知玩家猜太少了~並執行getBoundaryText()秀出最新範圍</p><p>同時生命值-1~然後印出玩家剩餘可猜次數</p><p>(4)當玩家輸入的值=正確答案的時候</p><p>恭喜它~並將原先隱藏的玫瑰圖開啟做為答對獎勵</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*nhQ3JhdjaIlU886TIjk5ow.png" /></figure><p>最後要弄的當然就是play again的按鈕啦!</p><p>這邊比較簡單~當玩家按下play again的按鈕時</p><p>將提示改為預設的”放膽去猜!!”</p><p>數字輸入欄改為預設值0</p><p>生命值:6</p><p>最新範圍提示最大值:50</p><p>最新範圍提示最小值:0</p><p>然後重新亂數產生一個0-50的數字做為答案</p><p>顯示可猜次數~並將玫瑰圖跟淚水都重新隱藏起來</p><p>就大功告成惹!</p><pre>@IBAction func playagain(_ sender: UIButton) {</pre><pre>hint.text = &quot;放膽去猜!!&quot;<br>inputNumber.text = &quot;0&quot;<br>chance = 6<br>upperBound = 50<br>lowerBound = 0<br>answer = Int(arc4random_uniform(50))<br>guessTime.text = String(chance)<br>rose.isHidden = true<br>tear.isHidden = true</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*Licn9-MPZKBQ92WWE0khIw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d3cf33e4e41c7cca39f18ed5489cbce/href">https://medium.com/media/5d3cf33e4e41c7cca39f18ed5489cbce/href</a></iframe><p><a href="https://github.com/nthuyaya/161123roseguess">nthuyaya/161123roseguess</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0df89a56f23" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E7%8C%9C%E6%95%B8%E5%AD%97-c0df89a56f23">猜數字</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[小費計算App]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%B0%8F%E8%B2%BB%E8%A8%88%E7%AE%97app-4be286aec8c0?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4be286aec8c0</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Tue, 15 Nov 2016 12:01:59 GMT</pubDate>
            <atom:updated>2016-12-13T06:49:11.010Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*vE2DOhxBmugR_27w9wm8GQ.gif" /></figure><p>先把@IBOutlet拉一拉</p><pre>@IBOutlet weak var money: UITextField!  //輸入金額</pre><pre>@IBOutlet weak var tip: UITextField!  //輸入小費</pre><pre>@IBOutlet weak var result: UILabel!  //小費計算結果</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*bLWq0BJCy1MDR6lJT0QBwg.png" /></figure><p>再來關於金額跟小費的輸入欄</p><p>先設定初始值為0</p><p>再將Clear Button的值改成Is always visible</p><p>讓輸入框中有ⓧ的清除的功能</p><p>最後把Keyboard Type改成Number Pad</p><p>讓使用者只能輸入數字，避免造成輸入文字導致程式出錯</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*o8QbU3vNVu34oL7Xot1V1Q.png" /></figure><p>計算按鈕的部份</p><p>除了加入防止使用者沒有輸入任何數字的檢查機制</p><pre>if money.text == &quot;&quot; || tip.text == &quot;&quot; {</pre><pre>result.text=&quot;ಠ╭╮ಠ&quot;</pre><pre>}</pre><pre>else{</pre><pre>result.text = String(Double(money.text!)! * Double(tip.text!)! / 100)</pre><pre>}</pre><p>還多加了按下計算按鈕時</p><p>收起鍵盤的功能</p><p>方法是呼叫resignFirstResponder() 這個函式</p><pre>@IBAction func calculate(_ sender: UIButton) {<br>money.resignFirstResponder()<br>tip.resignFirstResponder()<br>}</pre><pre>func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool{<br>money.resignFirstResponder()<br>tip.resignFirstResponder()</pre><pre>return true<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*_T_1gNfIGPV-b_9zk48i_A.png" /></figure><p>另外也上網搜尋了一下</p><p>按畫面中任意鍵就能將鍵盤收起的方法</p><p>就是加入這兩個函式</p><pre>func hideKeyboardWhenTappedAround() {</pre><pre>let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard))</pre><pre>view.addGestureRecognizer(tap)</pre><pre>}</pre><pre>func dismissKeyboard() {</pre><pre>view.endEditing(true)</pre><pre>}</pre><p>以及在</p><p>override func viewDidLoad() {super.viewDidLoad()} 裡面</p><p>加入一條</p><pre>self.hideKeyboardWhenTappedAround()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*5Zf9rutYy-cjfJxwknUhxA.png" /></figure><p>就大功告成了</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db9ebdd5e97a4e6a2fdb612d1fa726ea/href">https://medium.com/media/db9ebdd5e97a4e6a2fdb612d1fa726ea/href</a></iframe><p><a href="https://github.com/nthuyaya/161115tipcalculator">nthuyaya/161115tipcalculator</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4be286aec8c0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%B0%8F%E8%B2%BB%E8%A8%88%E7%AE%97app-4be286aec8c0">小費計算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[鴨鴨 The One App]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E9%B4%A8%E9%B4%A8-the-one-app-8405efa06f04?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8405efa06f04</guid>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Fri, 11 Nov 2016 02:43:06 GMT</pubDate>
            <atom:updated>2016-11-11T04:21:06.030Z</atom:updated>
            <content:encoded><![CDATA[<p>終於要把APP介面跟swift code結合在一起惹</p><p>因為在課堂上練習擇偶條件</p><p>所以就直接來做一個 鴨鴨 The One 測試的APP吧!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*efurTzOpgDbk4aKwbR1_NA.gif" /></figure><p>首先要建立一個新的類別</p><p>建立方式為[File] → [New] → [File]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*d04M2QWFHwcyGky9bmI7vQ.png" /></figure><p>然後選擇[Cocoa Touch Class]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*9UE_6vTEI6KznqWc7pSwsQ.png" /></figure><p>幫新的類別命名</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*bl1Lq41P9SZqmm2xCd4Xqg.png" /></figure><p>之後將該個Controller的類別從預設的UIViewController改成自己新增的那個</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*NGMOpuAOqMm6uVfBCId3_A.png" /></figure><p>設定完成後就可以開始製作APP了</p><p>先把畫好的物件一一擺放上去</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*7YWR9ZjKeCfQxV_KeVHoQA.png" /></figure><p>[星座] 使用<strong> TextField</strong></p><p>[性別] 使用 <strong>SegmentedControl</strong></p><p>[愛棒球] 使用 <strong>Switch</strong></p><p>[鴨生日] 使用 <strong>Silder</strong>，(後面放置一個用來顯示日期的<strong>label</strong>)</p><p>[送出] 使用 <strong>Button</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*pKkrPwzHNH0wJH2NQMcLTQ.jpeg" /></figure><p>然後我在最下面放了兩張結果圖，兩張圖位置重疊</p><p>會依結果的不同顯示其中一張</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*HX0UzFpqrHtljZJ8qIBlhw.png" /></figure><p>之後先將生日slider後面的label跟兩張結果圖勾選<strong>Hidden</strong>，隱藏起來</p><p>畫面排好後，就可以開始來寫程式啦!</p><p>為了方便將UI畫面跟code連結在一起</p><p>右上方的呈現方式選擇[兩個圈圈]的那個</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/103/1*DWidcpL_S9kxx1FXZ3LV-g.jpeg" /></figure><p>這樣UI編輯畫面就會跟code編輯畫面同時呈現</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NwE7h1J8ndrA-qgyiZltzA.png" /></figure><p>把UI畫面中的物件與code連結在一起的方式</p><p>就是從欲連結的物件身上按住Control鍵 ，拖拉到程式區放開</p><p>connection選擇<strong>[Outlet]</strong>，取物件名稱</p><p>然後就會在程式區出現<strong>@IBOutlet</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*fM6B6eEo9x8Rh65iV55AuQ.png" /></figure><p>依序把[星座]的 TextField 、性別的SegmentedControl、愛棒球的Switch、顯示鴨鴨生日日期的lebel跟兩張結果圖都拉進程式區</p><pre>@IBOutlet weak var <strong>zodiac</strong>: UITextField! //星座</pre><pre>@IBOutlet weak var <strong>gender</strong>: UISegmentedControl! //性別</pre><pre>@IBOutlet weak var <strong>lovebaseball</strong>: UISwitch! //愛棒球</pre><pre>@IBOutlet weak var <strong>yayabirthday</strong>: UILabel! //鴨鴨生日日期顯示</pre><pre>@IBOutlet weak var <strong>answerimgyes</strong>: UIImageView! //就是你的圖</pre><pre>@IBOutlet weak var <strong>answerimgno</strong>: UIImageView! //不是你的圖</pre><p>畫面就會變成下面這樣</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*WYgCFSZr-Iqeb14_Hmi5Hw.jpeg" /></figure><p>然後再把會觸發事件的[鴨鴨生日slider]跟[送出按鈕]也拉進去</p><p>只是這邊的connection要選擇<strong>[IBAction]</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*fxH_-o0Ykgp42oKg492b6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*s-o-ugC7E_JbUbn-dssYJw.png" /></figure><p>程式區就會出現<strong>@IBAction</strong>這種東西</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*MRg5mFfedjF4EDKn9xRh-Q.jpeg" /></figure><p>先來設定一下鴨鴨生日的slider</p><p>將初始值設為15，最小值1，最大值31</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*K2rjIWmRw82VOUfz7tRTbg.png" /></figure><p>程式部份，先宣告一個用來存取日期的變數yayabdayvalue</p><p>然後把@IBAction func birthday的形態改成<strong>UISlider</strong></p><p>其實應該一開始建立IBAction的時候就要將Type選成UISlider</p><p>利用<strong>yayabirthday.isHidden = false</strong>，將原本隱藏的日期label顯示</p><pre>var yayabdayvalue:Int = 1</pre><pre><strong>@IBAction func birthday(_ sender: UISlider) {</strong></pre><pre><strong>yayabirthday.isHidden = false </strong>//將原本隱藏的日期label顯示</pre><pre><strong>yayabdayvalue = Int(sender.value) </strong>//存取Slider回傳的值</pre><pre><strong>yayabirthday.text = &quot;12/\(yayabdayvalue)&quot; </strong></pre><pre>//將yayabirthday目前移動到的數字轉為字串顯示出來</pre><pre><strong>}</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*QsMGTeLo9XD-SxJSlVhnEw.png" /></figure><p>接著處理[送出按鈕]部份，將形態改成<strong>AnyObject</strong></p><p>宣告一個常數zodiacsign來存取星座輸入的值</p><p>只有當[星座為Gemini]且[性別為男]且[愛棒球]且[知道鴨鴨生日是26號]</p><p>的時候，就會顯示[就是你]的圖</p><p>反之則顯示[不是你]的圖</p><pre>@IBAction func sendbtn(_ sender: AnyObject) {</pre><pre>let zodiacsign:String = zodiac.text!</pre><pre>if zodiacsign == &quot;Gemini&quot; &amp;&amp; gender.selectedSegmentIndex == 0 &amp;&amp; lovebaseball.isOn == true &amp;&amp; yayabdayvalue == 26 {<br>answerimgyes.isHidden = false //顯示[就是你]的圖<br>answerimgno.isHidden = true //隱藏[不是你]的圖<br>}</pre><pre>else {<br>answerimgyes.isHidden = true //隱藏[就是你]的圖<br>answerimgno.isHidden = false //顯示[不是你]的圖<br>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*rsZN1g08INnk_hoKoHymdg.png" /></figure><p>APP差不多完工了呢!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uo5KceDUzNtzgNSmuSHnfg.jpeg" /></figure><p>後記:</p><p>布丁哥一直叫我要多加一個條件項目</p><p>就是在只有輸入他的名字時，才會顯示[就是你]的圖 XDD</p><p><a href="https://github.com/nthuyaya/161111yayatheone">nthuyaya/161111yayatheone</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LPfuBJUZvQQWy7Cy5BM78Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8405efa06f04" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E9%B4%A8%E9%B4%A8-the-one-app-8405efa06f04">鴨鴨 The One 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[計算格子的總合]]></title>
            <link>https://medium.com/@yayachen/%E6%95%B8%E6%A0%BC%E6%A0%BC-aeefff172d8e?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/aeefff172d8e</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Wed, 09 Nov 2016 06:05:16 GMT</pubDate>
            <atom:updated>2016-12-09T03:01:56.198Z</atom:updated>
            <content:encoded><![CDATA[<p>討人厭的數學之旅即將展開</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*2UctKu_-52JrWXalYCAyog.png" /></figure><p>首先先請高斯先生，拉馬努金跟班艾佛列克幫我加持一下</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*arp70apkueh-WFlcznzJDA.jpeg" /></figure><blockquote><strong>(1)算全部格子的總合</strong></blockquote><p>先取兩個常數c跟r ，c代表(直的)行 column ，r代表 (橫的)列row</p><pre>var sum = 0</pre><pre>for r in 0...7<br>{<br> for c in 0...7{<br> sum += r*c<br> }<br>}</pre><pre>print(sum)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*Q6VbmjrOVTZsVdviJcd8CA.jpeg" /><figcaption>784</figcaption></figure><blockquote><strong>(2)奇數行的數字總合</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*azm_dx4_pauFvvZCjJSg1g.jpeg" /></figure><pre>var sum=0</pre><pre>for r in 0...7<br>{<br> for c in 0...7{<br>   if c%2 == 1{<br>    sum += r*c<br>   }<br> }<br>}</pre><pre>print(sum)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*A48IDL-I6HB_ipiGiBYbew.jpeg" /><figcaption>448</figcaption></figure><blockquote><strong>(3)所有格子的總合，除了列數&gt;=行數的格子</strong></blockquote><p>翻譯成是列數&lt;行數的格子的總合</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*vtT6NGeHrOH5MHoyl3fZ7g.jpeg" /></figure><pre>var sum=0</pre><pre>for r in 0...7<br>{<br> for c in 0...7{<br>   if r&lt;c {<br>    sum += r*c<br>   }<br> }<br>}</pre><pre>print(sum)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*--Ic3cQaFQgkWG2cl_rACw.jpeg" /><figcaption>322</figcaption></figure><p>如果使用圖像思考法</p><p>就會發現這題其實就是計算整個矩陣右上半部的總合(排除0的那排)</p><p>所以也可以直接寫成這樣</p><pre>var sum = 0<br>for r in 1...6 {<br>    for c in (r+1)...7 {<br>        sum += r*c<br>    }<br>}<br>print(sum)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*j3urMkIh_SrXJJNh20nVfQ.jpeg" /><figcaption>答案也是322</figcaption></figure><blockquote><strong>(4)定義 function，接受3個參數，起始值，最大值和決定數字倍數的number， 回傳運算結果 比方起始值3，最大值98，決定數字倍數的number為5時，(只包含5的倍數) 運算結果為 5 + 10 + 15 + ….. + 95</strong></blockquote><pre>func  addNumber(startN:Int,MaxN:Int,timesN:Int) -&gt; Int{</pre><pre>var sum = 0<br>for i in startN...MaxN{<br>   if i%timesN == 0 {<br>    sum += i<br>   }<br>  }<br>return sum</pre><pre>}</pre><pre>var result = addNumber(startN:3,MaxN:98,timesN:5)</pre><pre>print(result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*NnNj6xppCdCm-T1kpvBU4g.jpeg" /><figcaption>950</figcaption></figure><p>如果用另一種更直接的寫法，直接先找出最接近初始值的倍數數字</p><p>再成倍相加到最接近最大值得倍數數字，也就可以得出一樣的結果</p><p>假設初始值是3，那最接近3的5的倍數就是3+(5-3%5)=5</p><pre>func addNumber(startN:Int, MaxN:Int, timesN:Int) -&gt; Int {<br>    var target = startN + (timesN - startN % timesN)<br>    var sum = 0<br>    while(target &lt;= MaxN) {<br>        sum += target<br>        target += timesN<br>    }<br>    return sum<br>}</pre><pre>var result = addNumber(startN: 3,MaxN: 98,timesN: 5)<br>print(result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*1xjhRp7WsVJCx17ZRtaS7w.jpeg" /><figcaption>答案也是950</figcaption></figure><blockquote><strong>(5)定義 function，接受3個參數，起始值，最大值和決定數字倍數的number， 回傳運算結果 比方起始值3，最大值11，決定數字倍數的number為5時，(不包含5的倍數) 運算結果為 3 + 4 + 6 + 7 + 8 + 9 + 11</strong></blockquote><pre>func  addNumber(startN:Int,MaxN:Int,timesN:Int) -&gt; Int{<br>var sum = 0<br>for i in startN...MaxN{<br>   if i%timesN != 0 {<br>    sum += i<br>   }<br>  }<br>return sum<br>}<br>var result = addNumber(startN:3,MaxN:11,timesN:5)</pre><pre>print(result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*6qaPQQvYiEU0NyzRpokClg.jpeg" /><figcaption>48</figcaption></figure><blockquote><strong>(6)奇數行的數字總合, 定義function，接受2個參數，分別代表行數和列數</strong></blockquote><pre>func  addNumber(column:Int,row:Int) -&gt; Int{</pre><pre>var sum = 0<br>for r in 0...column{<br>for c in 0...row{ <br>    if c%2 == 1{<br>      sum += r*c<br>     }<br>    } <br>   }</pre><pre>return sum<br>}</pre><pre>var result = addNumber(column:7,row:7)</pre><pre>print(result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*ERZVmaGvlLVyw1yJtl5H0Q.jpeg" /><figcaption>448</figcaption></figure><blockquote><strong>(7)所有格子的總合，除了列數&gt;=行數的格子, 定義function，接受2個參數，分別代表行數和列數</strong></blockquote><pre>func  addNumber(column:Int,row:Int) -&gt; Int{<br>var sum = 0<br>for r in 0...column{</pre><pre>for c in 0...row{ <br>    if r&lt;c{<br>      sum += r*c<br>     }<br>    }<br>  }</pre><pre>return sum<br>}</pre><pre>var result = addNumber(column:7,row:7)</pre><pre>print(result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*TWVQIoRKVzlHmwUgaI3t1Q.jpeg" /><figcaption>322</figcaption></figure><blockquote><strong>(8)定義一個 function，接受一個參數代表華式溫度 ， 回到攝式溫度</strong></blockquote><p>華氏 = 攝氏*(9/5)+32 <br>攝氏 = (華氏-32)*5/9</p><pre>func tempConversion(fahrenheit:Double) -&gt; Double{<br> return (fahrenheit-32)*5/9<br>}</pre><pre>var cTemp = tempConversion(fahrenheit:95)</pre><pre>print(cTemp)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*unRT6-0aCnwRw_-M6_AnGg.jpeg" /><figcaption>華氏95度=攝氏35度</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aeefff172d8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[無法隋堂的練習]]></title>
            <link>https://medium.com/@yayachen/%E7%84%A1%E6%B3%95%E9%9A%8B%E5%A0%82%E7%9A%84%E7%B7%B4%E7%BF%92-9d21cdde97bb?source=rss-9961884ac6a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d21cdde97bb</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Yaya Chen]]></dc:creator>
            <pubDate>Tue, 01 Nov 2016 13:17:33 GMT</pubDate>
            <atom:updated>2016-11-01T13:17:33.136Z</atom:updated>
            <content:encoded><![CDATA[<p>因為這週無法上課</p><p>只好先練習一下彼得潘PPT裡的隋堂作業</p><p>以免下週去上課的時候看不見其他同學的車尾燈</p><blockquote>1加到1000，只加偶數，用 for in &amp; if</blockquote><pre>var sum = 0</pre><pre>for i in 1...1000{<br>if (i % 2 == 0) {<br> sum += i<br>}<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*MW5QiLk9LuRxBTpcKQWvvg.png" /></figure><blockquote>將數字1的平方，2的平方相加，一路加到99的平方</blockquote><blockquote>(1)搭配 for in (2)搭配 while</blockquote><pre>var sum = 0</pre><pre>for i in 1...99{<br>    sum += i*i <br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*V7K3w7zByVfSIZ4Jp83GOg.png" /></figure><pre>var sum = 0<br>var i = 1</pre><pre>while i&lt;=99{<br>   sum += i*i<br>   i += 1<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*OFBGT9W_489CMEDXgB-fKQ.png" /></figure><blockquote>計算2的平方，加4的平方，一路加到100的平方 (偶數的平方)</blockquote><blockquote>(1) 搭配 if &amp; for in (2) 搭配 while</blockquote><pre>var sum = 0</pre><pre>for i in 2...100{<br>  if (i%2 == 0){<br>      sum += i*i<br>      }    <br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*kEiNF5Cw8DBtc0TL3EmmEQ.png" /></figure><pre>var sum = 0<br>var i = 2</pre><pre>while i&lt;=100{<br>    sum += i*i<br>    i += 2<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*gV2iSL5GpWmVihArIGxUEw.png" /></figure><blockquote>關於 external name 跟 internal name的小筆記</blockquote><blockquote>每個參數都將自動產生和 internal name一樣的 external name</blockquote><blockquote>swift呼叫 function的時候只能填 external name</blockquote><blockquote>例如 eat(name:”milk” , price: 300) ←這邊的name跟price是 external name</blockquote><blockquote>而要取消自動產生的 external name的方式</blockquote><blockquote>就是定義 function的時候 ，在 internal name前面加入 _ 這個符號</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*Xcy1YNta03LRzf-oy2th5A.png" /><figcaption>第一行的name是 internal name ，第三行的name是自動產生的 external name</figcaption></figure><blockquote>定義function，接受3個參數，起始值，最大值和每次累加值， 回傳運算結果</blockquote><blockquote>起始值設10，最大值設20，每次累加值設3</blockquote><pre>func addToNumber(startNumber:Int, endNumber:Int, step:Int) -&gt; Int {<br>    var sum = 0<br>    var i = startNumber<br>    <br>    while(i &lt;= endNumber){<br>        sum += i<br>        i += step<br>    }</pre><pre>return sum<br>}</pre><pre>var result = addToNumber(startNumber:10, endNumber:20, step:3)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*l7TtMNlwR6_aI7cEp85RxA.png" /><figcaption>10 + 13 + 16 + 19 = 58</figcaption></figure><blockquote>定義寶寶類別，包含名字，星座，體重，身高，年齡等屬性</blockquote><blockquote>生了 n 個寶寶 (可以是真的，也可以是幻想的)</blockquote><blockquote>設定每個寶寶的名字，星座，體重，身高，年齡</blockquote><pre>class Baby{<br>    var name = &quot;Duffy&quot;<br>    var age = 3<br>    var zodiacsign = &quot;雙子&quot;<br>    var height = 100<br>    var weight  = 20<br>}</pre><pre>var BabyS = Baby()<br>BabyS.name = &quot;ShellieMay&quot;<br>BabyS.age = 1<br>BabyS.zodiacsign = &quot;魔羯&quot;<br>BabyS.height = 80<br>BabyS.weight  = 15</pre><pre>var BabyG = Baby()<br>BabyG.name = &quot;Gelatoni&quot;<br>BabyG.age = 2<br>BabyG.zodiacsign = &quot;射手&quot;<br>BabyG.height = 85<br>BabyG.weight  = 18</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*6HA3m9DS5xltJjxkzui4Zw.png" /><figcaption>Duffy~ShellieMay~Gelatoni三寶寶</figcaption></figure><blockquote>在前面練習定義的寶寶類別裡定義 function</blockquote><blockquote>建立寶寶物件並呼叫 function</blockquote><p>就用function來計算寶寶的bmi值好惹！</p><p>BMI值計算公式: BMI = 體重(公斤) / 身高2(公尺2)</p><pre>class Baby{<br>    func countBMI(height:Double , weight:Double){<br>        var bmi = height/weight*weight<br>        print(bmi)<br>    }<br>}</pre><pre>var BabyDuffy = Baby()<br>BabyDuffy.countBMI(height:20.0 , weight:1.0)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*CDY0cYXiG_YmME41HFHldA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d21cdde97bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>