<?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 51°30′26″N on Medium]]></title>
        <description><![CDATA[Stories by 51°30′26″N on Medium]]></description>
        <link>https://medium.com/@b612starnight?source=rss-75af55527d38------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*w7t5i4dufVp3M7wzpPS5IA.png</url>
            <title>Stories by 51°30′26″N on Medium</title>
            <link>https://medium.com/@b612starnight?source=rss-75af55527d38------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:40:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@b612starnight/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[#68 使用swiftUI創建RSS閱讀器  — H4X0R News]]></title>
            <link>https://medium.com/@b612starnight/68-%E4%BD%BF%E7%94%A8swiftui%E5%89%B5%E5%BB%BArss%E9%96%B1%E8%AE%80%E5%99%A8-h4x0r-news-770745344cef?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/770745344cef</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Mon, 29 May 2023 11:16:16 GMT</pubDate>
            <atom:updated>2023-05-29T11:17:21.754Z</atom:updated>
            <content:encoded><![CDATA[<h3>#68 使用swiftUI創建RSS閱讀器 — H4X0R News</h3><p>目的：使用swiftUI創建RSS閱讀器</p><p>1.使用List建立表單，並利用NavigationView設定title</p><pre>struct ContentView: View {<br>    var body: some View {<br>        NavigationView{<br>            List{<br>                Text(&quot;Hello1&quot;)<br>                Text(&quot;Hello2&quot;)<br>                Text(&quot;Hello3&quot;)<br>            }<br>            //設定title<br>            .navigationTitle(&quot;H4X0R NEWS&quot;)<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*g44-1R4a84sVcTl88JOwGg.png" /></figure><p>2.建立struct</p><p>Identifiable: 此協議允許列表能夠根據ID識別post對象的順序<br>此協議一定要有一個命名為id的字串</p><pre>//Identifiable: 此協議允許列表能夠根據ID識別post對象的順序<br>//此協議一定要有一個命名為id的字串<br>struct Post: Identifiable{<br>    let id: String<br>    let title: String<br>}</pre><p>3.根據struct設定陣列</p><pre>let posts = [<br>    Post(id: &quot;1&quot;, title: &quot;Hello&quot;),<br>    Post(id: &quot;2&quot;, title: &quot;Hello2&quot;),<br>    Post(id: &quot;3&quot;, title: &quot;Hello3&quot;)<br>]</pre><p>4.回到ContentView中，改寫表單為將陣列內容填入表單的方法</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tSD5EWNyLpbzfPh-_FF3MQ.png" /></figure><pre>struct ContentView: View {<br>    var body: some View {<br>        NavigationView{<br>            List(posts, rowContent: { post in<br>                Text(post.title)<br>            })<br>            <br>            //設定title<br>            .navigationTitle(&quot;H4X0R NEWS&quot;)<br>        }<br>    }<br>}</pre><p>5.串連api</p><p>(1)建立Models Group，新增NetworkManager、PostData兩個swift file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*I07E3FCmjCUc3K3Wz_fLwg.png" /></figure><p>(2)Hacker News網址：<a href="https://news.ycombinator.com/">https://news.ycombinator.com/</a></p><p>參考其對於api的敘述：<a href="https://hn.algolia.com/api">https://hn.algolia.com/api</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdr2_nzF0CyiPRFTMpfy6A.png" /></figure><p>All stories that are on the front/home page right now：<a href="http://hn.algolia.com/api/v1/search?tags=front_page">http://hn.algolia.com/api/v1/search?tags=front_page</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EC2ICLpXCQ_U2aqOzBCnLw.png" /></figure><p>(3)在PostData內建立struct，串連好需要的資料</p><pre>import Foundation<br><br>struct Results: Decodable{<br>    let hits: [Post]<br>}<br><br><br>//Identifiable: 此協議允許列表能夠根據id識別post對象的順序<br>//此協議一定要有一個命名為id的字串<br>struct Post: Decodable, Identifiable{<br>    //因為要建立名稱為id的變數才可識別，故使用computed property使其相等<br>    var id: String{<br>        return objectID<br>    }<br>    //識別每個Hacker News數據庫中每個帖子的編號<br>    let objectID:String<br>    let points:Int<br>    let title:String<br>    let url:String?<br>}</pre><p>(4)在NetworkManager內</p><p>加入ObservableObject協議：當 property 改變時，它將自動更新畫面，讓畫面能夠跟參數連動</p><pre>import Foundation<br><br>//ObservableObject: 當 property 改變時，它將自動更新畫面，讓畫面能夠跟參數連動<br>class NetworkManager: ObservableObject{</pre><p>(5)先建立var posts = [Post]()，前面需加@Published:<br>SwiftUI中非常好用的wrapper，它允許我們創建”可觀察的”object，當這些object發生更改時會自動宣布通知。SwiftUI將自動監控此類更改，並重新調用data的任何view的body屬性因為跟畫面有關，使用時要將其丟入async內</p><pre>import Foundation<br><br>//ObservableObject: 當 property 改變時，它將自動更新畫面，讓畫面能夠跟參數連動<br>class NetworkManager: ObservableObject{<br>    <br>    //@Published:SwiftUI中非常好用的wrapper，它允許我們創建”可觀察的”object，當這些object發生更改時會自動宣布通知。SwiftUI將自動監控此類更改，並重新調用data的任何view的body屬性因為跟畫面有關，使用時要將其丟入async內<br><br>   @Published var posts = [Post]()</pre><p>(6)建立fetchData()讀取資料</p><pre>    func fetchData(){<br>        if let url = URL(string: &quot;http://hn.algolia.com/api/v1/search?tags=front_page&quot;){<br>            let session = URLSession(configuration: .default)<br>            let task = session.dataTask(with: url) { data, response, error in<br>                if error == nil{<br>                    //建立JSON解碼器<br>                    let decoder = JSONDecoder()<br>                    if let safeData = data{<br>                        do{<br>                           let results = try decoder.decode(Results.self, from: safeData)<br>                            //因更新表單屬於畫面範疇，故需加至主佇列<br>                            DispatchQueue.main.async {<br>                                self.posts = results.hits<br>                            }    <br>                        }catch{<br>                            print(error)<br>                        }<br>                        <br>                    }<br>                    <br>                }<br>            }<br>            task.resume()<br>        }<br>    }<br>}</pre><p>6.回到ContentView中，刪除拿來測試的Post</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hWbNgu3yX1EQF1BY9H63dg.png" /></figure><p>7.建立networkManager = NetworkManager()物件，並在前面加入 @ObservedObject，使其與畫面即時連動</p><pre>struct ContentView: View {<br>    <br>    @ObservedObject var networkManager = NetworkManager()</pre><p><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/10-observedobject%E7%9A%84%E4%BD%BF%E7%94%A8-187eb99d86bb">#10 @ObservedObject的使用</a></p><p>8.改寫表單內容為參照networkManager.posts，rowContent內加入HStack，指定要加入的文字</p><pre>            List(networkManager.posts, rowContent: { post in<br>                HStack {<br>                    Text(String(post.points))<br>                    Text(post.title)<br>                }<br>            })</pre><p>9.設定onAppear，在畫面剛開始時讀取fetchData()</p><ul><li>onAppear與swift內viewDidLoad類似</li></ul><pre>    var body: some View {<br>        NavigationView{<br>            List(networkManager.posts, rowContent: { post in<br>                HStack {<br>                    Text(String(post.points))<br>                    Text(post.title)<br>                }<br>            })<br>            //設定title<br>            .navigationTitle(&quot;H4X0R NEWS&quot;)<br>        }<br>        //onAppear與swift內viewDidLoad類似<br>        .onAppear {<br>            self.networkManager.fetchData()<br>        }<br>    }</pre><p>10.顯示列表內的詳細訊息</p><p>(1)建立一個SwiftUI File，命名成DetailView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XFGaJpwTlhJiUh7JHM4cmQ.png" /></figure><p>(2)在DetailView內加入url，更新畫面時需填入url網址</p><pre>import SwiftUI<br><br><br>struct DetailView: View {<br>    <br>    let url: String?<br>    <br>    var body: some View {<br>        Text(&quot;Hello World&quot;)<br>    }<br>}<br><br>struct DetailView_Previews: PreviewProvider {<br>    static var previews: some View {<br>        DetailView(url: &quot;https://www.google.com&quot;)<br>    }<br>}</pre><p>(3)回到ContentView，加入NavigationLink:<br>此導航連接將在每個單元格右側創建一個按鈕。可以將每個NavigationLink想像成一個單元格，以前在單元格內容中的內容現在則成為NavigationLink的標籤</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hY40ocbeSPiN6vTZvfiWXg.png" /></figure><p>destination填入DetailView(url: post.url)</p><pre>    var body: some View {<br>        NavigationView{<br>            List(networkManager.posts, rowContent: { post in<br>                //NavigationLink:此導航連接將在每個單元格右側創建一個按鈕。可以將每個NavigationLink想像成一個單元格，以前在單元格內容中的內容現在則成為NavigationLink的標籤<br>                NavigationLink(destination: DetailView(url: post.url)) {<br>                    HStack {<br>                        Text(String(post.points))<br>                        Text(post.title)<br>                    }<br>                }<br>                <br>            })</pre><p>(4)新增一個swift File，因目前WebView尚無法在SwiftUI內使用，故需建立UIKit<br>命名WebView，導入:</p><pre>import Foundation<br>import WebKit<br>//導入SwiftUI<br>import SwiftUI</pre><p>(5)加入UIViewRepresentable協議：此協議可以創建一个代表UIKitView的SwiftUIView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YTHljb_eZP3IP9c4VE12jw.png" /></figure><pre>//UIViewRepresentable：此協議可以創建一个代表UIKitView的SwiftUIView<br>struct WebView: UIViewRepresentable {</pre><p>(6)建立urlString</p><pre>    let urlString: String?</pre><p>(7)服從makeUIView方法，將some UIView 改為WKWebView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CG9Rb82MgaDAV5vuTiM9QQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KQmXxbzmTCkZfKkB4z7MDw.png" /></figure><pre>    //將some UIView 改為WebView.UIViewType，再改為WKWebView<br>    func makeUIView(context: Context) -&gt; WKWebView {<br>        return WKWebView()<br>    }</pre><p>(8)服從updateUIView方法，這裡創建的UIViewType並非WebView，故需改成WKWebView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5sVBtY-GXL-POT840NBafg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RVWPdS0lRqsSlZwABoHWfg.png" /></figure><p>解析網頁方式：</p><pre>    //這裡創建的UIViewType並非WebView，故需改成WKWebView<br>    func updateUIView(_ uiView: WKWebView, context: Context) {<br>        if let safeString = urlString, let url = URL(string: safeString){<br>                let request = URLRequest(url: url)<br>                uiView.load(request)<br>        }<br>    }</pre><p>(9)回到DetailView，改寫body內容為WebView(urlString: url)</p><pre>struct DetailView: View {<br>    <br>    let url: String?<br>    <br>    var body: some View {<br>        WebView(urlString: url)<br>    }<br>}</pre><p>11.完整程式碼及畫面展示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*ir_PTvE-PXhPMkFbAHtliw.gif" /></figure><p>GitHub:</p><p><a href="https://github.com/Phrase0/H4X0R-News-SwiftUI">GitHub - Phrase0/H4X0R-News-SwiftUI</a></p><p>參考資料：</p><p><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/10-observedobject%E7%9A%84%E4%BD%BF%E7%94%A8-187eb99d86bb">#10 @ObservedObject的使用</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=770745344cef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#67 使用swiftUI創建有功能的app — Dicer]]></title>
            <link>https://medium.com/@b612starnight/67-%E4%BD%BF%E7%94%A8swiftui%E5%89%B5%E5%BB%BA%E6%9C%89%E5%8A%9F%E8%83%BD%E7%9A%84app-dicer-b3444414bb6b?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3444414bb6b</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Fri, 19 May 2023 15:56:03 GMT</pubDate>
            <atom:updated>2023-05-29T11:18:06.735Z</atom:updated>
            <content:encoded><![CDATA[<h3>#67 使用swiftUI創建有功能的app — Dicer</h3><p>目的：使用swiftUI創建有功能的app</p><p>1.將圖片匯入assets內</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WxkDsLL8a1tbRuayuTrKQg.png" /></figure><p>2.設定背景</p><pre>        ZStack {<br>            Image(&quot;background&quot;)<br>                .resizable()<br>                .edgesIgnoringSafeArea(.all)<br>        }</pre><p>3.加入圖片</p><pre>        ZStack {<br>            Image(&quot;background&quot;)<br>                .resizable()<br>                .edgesIgnoringSafeArea(.all)<br>            VStack {<br>                Image(&quot;diceeLogo&quot;)<br>                Image(&quot;dice1&quot;)<br>                    .resizable()<br>                    .aspectRatio(1, contentMode: .fit)<br>            }<br>        }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*xlIF2MppBeUsaRi_Z39KbQ.png" /></figure><p>4.因為要顯示兩顆骰子，故按command加左鍵，extract subview</p><p>(1)命名為DiceView</p><pre>struct ContentView: View {<br>    var body: some View {<br>        ZStack {<br>            Image(&quot;background&quot;)<br>                .resizable()<br>                .edgesIgnoringSafeArea(.all)<br>            VStack {<br>                Image(&quot;diceeLogo&quot;)<br>                DiceView()<br>            }<br>        }<br>    }<br>}<br><br>struct DiceView: View {<br>    var body: some View {<br>        Image(&quot;dice1&quot;)<br>            .resizable()<br>            .aspectRatio(1, contentMode: .fit)<br>    }<br>}</pre><p>(2)將骰子數字設為可代入數字的常數</p><pre>            VStack {<br>                Image(&quot;diceeLogo&quot;)<br>                DiceView(n: 2)<br>            }<br>        }<br>    }<br>}<br><br>struct DiceView: View {<br>    let n : Int<br>    var body: some View {<br>        Image(&quot;dice\(n)&quot;)<br>            .resizable()<br>            .aspectRatio(1, contentMode: .fit)<br>    }<br>}</pre><p>(3)加入HStack，設定兩個骰子<br>(4)加入padding設定邊界填充，並指定填充水平部分</p><pre>                HStack {<br>                    DiceView(n: 2)<br>                    DiceView(n: 2)<br>                }<br>                .padding(.horizontal)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*7pYeGe07B3V2XIuDWtV_PA.png" /></figure><p>(5)讓骰子的四邊都有填充：</p><p>在DiceView內加入padding()</p><pre>struct DiceView: View {<br>    let n : Int<br>    var body: some View {<br>        Image(&quot;dice\(n)&quot;)<br>            .resizable()<br>            .aspectRatio(1, contentMode: .fit)<br>            .padding()<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*B_ZwiDr8SWRnxsgrd4jLaA.png" /></figure><p>5.加入按鈕</p><p>(1)font：設定字的大小、粗體<br>(2)foregroundColor：設定文字顏色<br>(3)padding(.horizontal)：設定邊界填充，並指定填充水平部分<br>(4)background(Color.red)：設定背景色</p><p><a href="https://stackoverflow.com/questions/68672867/how-do-i-make-a-swiftui-buttons-text-larger-and-or-bold">How do I make a SwiftUi Button&#39;s text larger and/or bold</a></p><pre>                Button(&quot;Roll&quot;) {<br>                    //Action<br>                }<br>                .font(.system(size: 50, weight: .heavy, design: .default))<br>                .foregroundColor(.white)<br>                .padding(.horizontal)<br>                .background(Color.red)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*BZOTKT8_eHvH5bnqhFFkSw.png" /></figure><p>6.讓logo、骰子、按鈕三者隔出空間</p><p>加入Spacer():根據螢幕大小創建合適的空間量</p><pre>VStack {<br>                Image(&quot;diceeLogo&quot;)<br>                //Spacer():根據螢幕大小創建合適的空間量<br>                Spacer()<br>                HStack {<br>                    DiceView(n: 2)<br>                    DiceView(n: 2)<br>                }<br>                .padding(.horizontal)<br>                Spacer()<br>                Button(&quot;Roll&quot;) {<br>                    //Action<br>                }<br>                .font(.system(size: 50, weight: .heavy, design: .default))<br>                .foregroundColor(.white)<br>                .padding(.horizontal)<br>                .background(Color.red)<br>                Spacer()<br>            }</pre><p>7.按下按鈕後會隨機出現不同骰子</p><p>(1)建立變數</p><ul><li>@static：在swiftUI內時，struct要修改自己的屬性要加上@static關鍵字，如果該屬性發生變化, SwiftUI要知道屏幕上的哪些視圖也需要更新</li></ul><p><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/%E7%94%A8%E7%8B%80%E6%85%8B%E8%A8%AD%E8%A8%88-swiftui-%E7%95%AB%E9%9D%A2-%E8%AA%8D%E8%AD%98-state-property-binding-27fea6885ead">用狀態設計 SwiftUI 畫面 — 認識 State property</a></p><pre>struct ContentView: View {<br>    <br>    @State var leftDiceNumber = 1<br>    @State var rightDiceNumber = 1</pre><p>(2)在Button內加入random隨機數字</p><pre>Button(&quot;Roll&quot;) {<br>                    self.leftDiceNumber = Int.random(in: 1...6)<br>                    self.rightDiceNumber = Int.random(in: 1...6)<br>                    <br>                }</pre><p>8.完整程式碼及畫面展示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*31l53pqJNhNGHFSqoY1AYA.gif" /></figure><p>Github:</p><p><a href="https://github.com/Phrase0/Dicer-SwiftUI">GitHub - Phrase0/Dicer-SwiftUI</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3444414bb6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#66 使用swiftUI佈置複雜畫面 — MyCreditCard]]></title>
            <link>https://medium.com/@b612starnight/66-%E4%BD%BF%E7%94%A8swiftui%E4%BD%88%E7%BD%AE%E8%A4%87%E9%9B%9C%E7%95%AB%E9%9D%A2-mycreditcard-515639995663?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/515639995663</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Mon, 15 May 2023 14:54:24 GMT</pubDate>
            <atom:updated>2023-05-15T14:54:24.737Z</atom:updated>
            <content:encoded><![CDATA[<h3>#66 使用swiftUI佈置複雜畫面 — MyCreditCard</h3><p>目的：使用swiftUI佈置複雜畫面</p><p>1.新增背景</p><pre>struct ContentView: View {<br>    var body: some View {<br>        ZStack {<br>            Color(UIColor(red: 0.91, green: 0.30, blue: 0.24, alpha: 1))<br>                .edgesIgnoringSafeArea(.all)<br>            Text(&quot;Hello, world!&quot;)<br>                .padding()<br>        }<br>    }<br>}</pre><p>挑選顏色的方法</p><p><a href="https://flatuicolors.com/">Flat UI Colors 2 - 14 Color Palettes, 280 colors 🎨</a></p><p>將顏色轉換為程式可用之RGB數據</p><p><a href="https://www.uicolor.io/#/hex-to-ui">Convert HEX &amp; RGB colors to UIColor</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T333QIKUSyZ4PNcGT49nHw.png" /></figure><p>2.新增名片文字</p><p>(1)下載google字體Pacifico並安裝字體</p><p><a href="https://fonts.google.com/">Browse Fonts - Google Fonts</a></p><p>(2)建立新資料夾，將Pacifico.ttf檔置入</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*7Pw5MztItjzVrCqZ87bSYg.png" /></figure><p>勾選add to targets</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*gpIBsbYi6v7psp__uPTevw.png" /></figure><p>(3)自定義字體方法</p><p>Xcode13以下，在info.plist內加入Fonts provided by application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Cj4O71DypmkpVmNRrc4PA.png" /></figure><p>Xcode13之後，因info.plist已被取消，故使用下列方法</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZWBbkRAttQWOA90TRAbFZg.png" /></figure><p><a href="https://blog.zhheo.com/p/b0e287b9.html">https://blog.zhheo.com/p/b0e287b9.html</a></p><p>(4)在Text內新增字型、加粗、顏色</p><pre>            VStack {<br>                Text(&quot;Peiyun&quot;)<br>                    .font(.custom(&quot;Pacifico-Regular&quot;, size: 40))<br>                    .bold()<br>                    .foregroundColor(.white)<br>                Text(&quot;iOS Developer&quot;)<br>                    .font(.system(size: 25))<br>                    .foregroundColor(.white)<br>            }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*z5RMP4RHLYymAKgv4xhrjg.png" /></figure><p>3.新增頭貼</p><p>(1)將圖片置入asset內</p><p>(2)程式碼如下</p><p>overlay：覆蓋某物於圖片上</p><p>stroke：設為邊框</p><pre>VStack {<br>                Image(&quot;image&quot;).resizable().aspectRatio(contentMode: .fill)<br>                    .frame(width: 150, height: 150)<br>                    .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)<br>                    .overlay(<br>                        Circle().stroke(Color.white, lineWidth: 5))<br>                Text(&quot;Peiyun&quot;)<br>                    .font(.custom(&quot;Pacifico-Regular&quot;, size: 40))<br>                    .bold()<br>                    .foregroundColor(.white)<br>                Text(&quot;iOS Developer&quot;)<br>                    .font(.system(size: 25))<br>                    .foregroundColor(.white)<br><br>            }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*I81P31dhpWBJx7BmeMTGZA.png" /></figure><p>4.加入電話號碼</p><p>(1)Divider()：切割畫面</p><p>(2)加入RoundedRectangle</p><p>(3)在overlay中加入圖片及文字</p><pre>            VStack {<br>                Image(&quot;image&quot;).resizable().aspectRatio(contentMode: .fill)<br>                    .frame(width: 150, height: 150)<br>                    .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)<br>                    .overlay(<br>                        Circle().stroke(Color.white, lineWidth: 5))<br>                Text(&quot;Peiyun&quot;)<br>                    .font(.custom(&quot;Pacifico-Regular&quot;, size: 40))<br>                    .bold()<br>                    .foregroundColor(.white)<br>                Text(&quot;iOS Developer&quot;)<br>                    .font(.system(size: 25))<br>                    .foregroundColor(.white)<br>                Divider()<br>                RoundedRectangle(cornerRadius: 25)<br>                    .fill(.white)<br>                    .frame(height: 50)<br>                    .overlay(HStack {<br>                        Image(systemName: &quot;phone.fill&quot;).foregroundColor(.green)<br>                        Text(&quot;+886 912 345 678&quot;)<br>                    })<br>            }</pre><p>5.重複使用圓角矩形組件</p><p>(1)按command，點選EXtract Subview</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*dtG4QpmsWJd3TALcAi4ddg.png" /></figure><p>(2)點選完後設立名稱InfoView()</p><pre>                Divider()<br>                InfoView()</pre><p>(3)在下方會出現InfoView的struct，將可變動處設成常數</p><pre>struct InfoView: View {<br>    let text:String<br>    let image:String<br>    <br>    var body: some View {<br>        RoundedRectangle(cornerRadius: 25)<br>            .fill(.white)<br>            .frame(height: 50)<br>            .overlay(HStack {<br>                Image(systemName:image)<br>                    .foregroundColor(.green)<br>                Text(text)<br>            })<br>    }<br>}</pre><p>(4)新增file，勾選swiftUI View</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fsZ6-93WyC-sQWJMYV1ISA.png" /></figure><p>(5)將原先存在於swiftUI View中的struct SwiftUIView刪除</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dAnAtXif_Cz5TcjabTpElQ.png" /></figure><p>加入下列程式碼</p><p>previewLayout：預覽畫面</p><pre>import SwiftUI<br><br>struct InfoView: View {<br>    let text:String<br>    let image:String<br>    <br>    var body: some View {<br>        RoundedRectangle(cornerRadius: 25)<br>            .fill(.white)<br>            .frame(height: 50)<br>            .overlay(HStack {<br>                Image(systemName:image)<br>                    .foregroundColor(.green)<br>                Text(text)<br>            })<br>    }<br>}<br><br>struct SwiftUIView_Previews: PreviewProvider {<br>    static var previews: some View {<br>        InfoView(text: &quot;Hello&quot;, image: &quot;phone.fill&quot;)<br>            .previewLayout(.sizeThatFits)<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GyKnBby1ol8K-PJUr43ykA.png" /></figure><p>(6)回到Divider()下，帶入所需資料</p><pre>                Divider()<br>                InfoView(text: &quot;+886 912 345 678&quot;, image: &quot;phone.fill&quot;)<br>                InfoView(text: &quot;peiyun@mail.com&quot;, image: &quot;mail.fill&quot;)</pre><p>6.完整程式碼及畫面展示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*zreeEjwfRYvHYyKMcN03lA.png" /></figure><p>Github:</p><p><a href="https://github.com/Phrase0/MyCard">GitHub - Phrase0/MyCard</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=515639995663" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#65 使用swiftUI佈置畫面 — I am rich]]></title>
            <link>https://medium.com/@b612starnight/65-%E4%BD%BF%E7%94%A8swiftui%E4%BD%88%E7%BD%AE%E7%95%AB%E9%9D%A2-i-am-rich-9e4d899b73ae?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e4d899b73ae</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Thu, 11 May 2023 15:13:04 GMT</pubDate>
            <atom:updated>2023-05-14T09:54:42.361Z</atom:updated>
            <content:encoded><![CDATA[<h3>#65 使用swiftUI佈置畫面 — I am rich</h3><p>目的：使用swiftUI建立畫面</p><p>1.新建project，檔案類型勾選swiftUI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*NTHN1Ja0PQrzVes42lmu5A.png" /></figure><p>2.初始畫面如下</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s-h7bM7zFI2ZkDNmTCt9nQ.png" /></figure><p>3.文字部分修改如下</p><pre>                    Text(&quot;I Am Rich&quot;)<br>                    .font(.system(size: 40))<br>                    .fontWeight(.bold)<br>                    .foregroundColor(Color.white)</pre><p>也可靠右側欄做調整</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*90tTnwBrOaiWD9KLEVaQ4A.png" /></figure><p>4.新增ZStack，加入背景</p><p>ZStack：垂直往上疊加<br>VStack：垂直往下疊加<br>HStack：水平疊加</p><pre>        ZStack {<br>            //edgesIgnoringSafeArea：忽略安全區域<br>            Color(.systemTeal)<br>                .edgesIgnoringSafeArea(.all)<br>                Text(&quot;I Am Rich&quot;)<br>                    .font(.system(size: 40))<br>                    .fontWeight(.bold)<br>                    .foregroundColor(Color.white)<br>        }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1SC8bVtLzTOfkpD6TU9TSw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ly-mr0jfu-todx9MmLURyA.png" /></figure><p>5.加入圖片，並新增VStack，使文字和圖片垂直並排</p><p>點選command + 左鍵，選擇VStack後，加入圖片</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*36PNFT_azBI1koTUovJQDA.png" /></figure><pre> ZStack {<br>            //edgesIgnoringSafeArea：忽略安全區域<br>            Color(.systemTeal)<br>                .edgesIgnoringSafeArea(.all)<br>            <br>            VStack {<br>                Text(&quot;I Am Rich&quot;)<br>                    .font(.system(size: 40))<br>                    .fontWeight(.bold)<br>                    .foregroundColor(Color.white)<br>                //resizable()：圖像可调整大小<br>                //aspectRatio：修改縱橫比<br>                Image(&quot;diamond&quot;)<br>                    .resizable()<br>                    .aspectRatio(contentMode: .fit)<br>                    .frame(width: 200, height: 200, alignment: .center)<br>            }<br>        }</pre><p>6.完整程式碼及畫面展示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*NYekD8x6v_KKCcGPwBOlnQ.png" /></figure><p>Github:</p><p><a href="https://github.com/Phrase0/I-am-rich">GitHub - Phrase0/I-am-rich</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e4d899b73ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#65 使用Xcode做git版本控制]]></title>
            <link>https://medium.com/@b612starnight/65-%E4%BD%BF%E7%94%A8xcode%E5%81%9Agit%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6-926c5fc05940?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/926c5fc05940</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Fri, 05 May 2023 16:18:02 GMT</pubDate>
            <atom:updated>2023-05-05T16:18:02.077Z</atom:updated>
            <content:encoded><![CDATA[<p>目的：使用Xcode做git版本控制</p><p>1.第一次的版本上傳部分，可參考彼得潘的文章</p><p><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%B0%87-ios-app-%E4%BD%9C%E6%A5%AD%E4%B8%8A%E5%82%B3%E5%88%B0-github-135416069dfd">如何將 iOS App 作業上傳到 GitHub</a></p><p>2.Discard All Changes：在commit新版本前取消當前所有操作</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*2Z_l-P_llzyFy71VNrKH8A.png" /></figure><p>3.建立分支：在Repositories中找到要添加分支的節點，選擇New Branch from……</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kc_rqA3ZwTnxLRqGPd4g4g.png" /></figure><p>4.建立分支後，點選Check Out…，可切換至分支</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*2ZKTCXkNwieVpOiFgGVLoA.png" /></figure><p>5.完成分支後，若想合併至主支中，則點選右鍵，Merge…into…(此時的current需要切換到主支)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*Sz8UU2w2GJ-CGhCuSaTOhg.png" /></figure><p>6.上傳至Github</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*7NIbW7OfZ47voRgjKJSnWQ.png" /></figure><p>7.Source Control說明</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*QYnW7Y67trAEUTC6HI5QJg.png" /></figure><p>(1)commit：僅本地新建紀錄，未上傳至Github</p><p>若commit時要一併上傳Github，需勾選Push to remote</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v8690FljWkVARSjSlwEG5g.png" /></figure><p>(2)Push：推送至Github</p><p>(3)Pull：當Github內檔案有新增任何檔案，與本地檔案不相符時，須先點擊Pull，下載新增的檔案後，才可Push重新推送</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*xS8oPi6BynWEZrd3AI7nVA.png" /></figure><h4>使用fork與他人協作</h4><p>1.fork：複製一個遠程倉庫，並把它下載至自己的github中</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z0bpFZbf4lPSagqqJz9r4A.png" /></figure><p>2.將檔案修改後，若要回報原作者，則點選New pull request</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zrtdjLmogQ7IL2UsiBLGvA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8xdwpEJovMAXfWNmV32m8w.png" /></figure><p>3.此時在原作者的介面顯示：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FKM7LJbRa4g9m3Xi7F_Qw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aCRs1Jp23Df6ZS_drqjSug.png" /></figure><p>4.有一個人提送了修改要求，點擊查看差異</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-ziVA3mUo0yvbSMp28lUQ.png" /></figure><p>若同意修改，則勾選同意</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpNHn3VNSQzrwSf_oNmU1Q.png" /></figure><p>點選合併分支</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*TOmtKOCWDUCnWcPjUxrVFg.png" /></figure><p>此時的Insight畫面</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2-spJaE9bNvLfRHSgCkXiA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=926c5fc05940" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#64 使用github做git版本控制]]></title>
            <link>https://medium.com/@b612starnight/64-%E4%BD%BF%E7%94%A8github%E5%81%9Agit%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6-22343c7f59da?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/22343c7f59da</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Fri, 05 May 2023 14:20:29 GMT</pubDate>
            <atom:updated>2023-05-29T11:18:38.269Z</atom:updated>
            <content:encoded><![CDATA[<p>目的：使用github做git版本控制</p><p>分別創建主支與分支</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WrLp3CgDDLGALtdjTCOwww.png" /></figure><p>推送要求</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cz6gQbKZ7XkppezjkqzhYA.png" /></figure><p>比對差異後合併主分支</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JDf-5NTtcGxB1lyxpcuKXA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9bS3YUYkW6-ZFU8qzYugHg.png" /></figure><p>從insights可查看分支結構</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J5leKGGVMuTZ-i73a6rD5w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22343c7f59da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#61 使用終端機做git版本控制]]></title>
            <link>https://medium.com/@b612starnight/61-%E4%BD%BF%E7%94%A8%E7%B5%82%E7%AB%AF%E6%A9%9F%E5%81%9Agit%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6-711a39a635da?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/711a39a635da</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Thu, 04 May 2023 15:59:29 GMT</pubDate>
            <atom:updated>2023-05-04T15:59:29.788Z</atom:updated>
            <content:encoded><![CDATA[<h3>#63 使用終端機做git版本控制</h3><p>目的：使用終端機做git版本控制</p><ul><li>ls(list)：列出該位置的所有檔案與資料夾名稱</li><li>ls -a : 查看所有隱藏文件</li><li>cd&lt;directory&gt;/ ：更換目前所在的位置，移動到別處</li><li>cd .. ：回到上層資料夾</li><li>cd ~ ：回到根資料夾</li><li>mkdir ：創建新資料夾</li><li>touch &lt;directory&gt; : 建立文件</li></ul><h4><strong>本地版本控制</strong></h4><ul><li>git init : 初始化Git</li><li>git status : 查看暫存區的內容</li><li>git add &lt;directory&gt; : 添加到暫存區，並追蹤其中的更改</li><li>git add . : 添加當前資料夾內所有內容</li><li>git commit -m “&lt;message&gt;” : 初始提交</li><li>git log : 查看做了哪些提交</li><li>git diff &lt;directory&gt; : 查看當前版本與前一版不同的地方</li><li>git checkout &lt;directory&gt; : 回到前一個版本</li><li>git update -ref -d HEAD : 刪除所有版本</li></ul><p><a href="https://blog.csdn.net/CHAOS_ORDER/article/details/122655480">git删除第一个commit_git删除第一条_CHAOS_ORDER的博客-CSDN博客</a></p><p>步驟如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*otnFzbsUBShJAfzMzxEQcA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oy1qPmMfBZ6AxoXQzV_O4w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*G0eTQxzukKAq_bAyJ08sLA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*P38aMB_k8rbzcDh0O8jJmg.png" /></figure><h4><strong>遠端推送至github</strong></h4><ul><li>git remote add origin &lt;url&gt; : 創建遠端origin資料庫</li><li>git branch -M main : 主枝幹</li><li>git -u origin main : 使用u標誌，將本地資料庫推送至遠端資料庫</li><li>新版github上傳時要輸入帳號密碼，密碼為token</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yo9S9dDkgVB-_Wtr9Pwcg.png" /></figure><h4><strong>隱藏文件(如api、密碼等)</strong></h4><ul><li>touch .gitignore : 創建隱藏文件</li><li>git rm — -cached -r . ：使用r標誌，表示遞迴，刪除已儲存到暫存區的文件</li><li>open .gitignore，在.gitignore中加入不想放入的文件 ex:DS_Store</li><li># : 註釋</li><li>* .txt : 所有txt皆可被忽略</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*MZ_7kFTjE-G1yDGohlrr8w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*HKDwtGVhSYYwRtk0FPYFwA.png" /></figure><p>github上搜尋gitignore，有常用的需要被忽略不上傳的文件，下載swift檔案後，將內容複製至.gitignore上</p><p><a href="https://github.com/github/gitignore">GitHub - github/gitignore: A collection of useful .gitignore templates</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiqvyvBY_UcKgHr-Zzu-8A.png" /></figure><h4><strong>主支與合併分支</strong></h4><ul><li>git branch &lt;directory&gt; : 建立分支</li><li>git branch : 查看分支</li><li>git checkout &lt;directory&gt; : 切換到分支</li><li>git merge &lt;directory&gt; : 將分之內容合併到主支。此時會打開vim文本編輯器，可添加合併訊息(ex :q!)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vRMKYz5srvxcPh_xy16AMA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*jEmO9wS6ayd_xph8k4pRGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*IRKONK-bwGCgVbl7nKfkJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*KzyYMPAoH1t3F0wngY5EgA.png" /></figure><p>最後推送至：</p><ul><li>git branch -M main : 主支幹</li><li>git -u origin main : 使用u標誌，將本地資料庫推送至遠端資料庫</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=711a39a635da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#62 Flash Chat聊天對話 — 下]]></title>
            <link>https://medium.com/@b612starnight/62-flash-chat%E8%81%8A%E5%A4%A9%E5%B0%8D%E8%A9%B1-%E4%B8%8B-d935952228cc?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/d935952228cc</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Tue, 02 May 2023 05:51:10 GMT</pubDate>
            <atom:updated>2023-05-03T14:53:24.474Z</atom:updated>
            <content:encoded><![CDATA[<h3>#62 Flash Chat聊天對話(下)</h3><p>目的：利用 Firebase 實現 iOS App 的傳送對話功能</p><p>1.建立MessageCell 的Xib檔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZQ6NrKiBCcCwsP8xQuqfYA.png" /></figure><p>命名cell的名稱</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*a6ifFS1r3RIfFHd_JuE61g.png" /></figure><p>2.cell內佈局</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*VPYVYP62RPOHYbpGb72j5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*P-5et8QFcFOi7aVruCfI3Q.png" /></figure><p>3.在MessageCell內</p><pre>import UIKit<br><br>class MessageCell: UITableViewCell {<br>    <br>    @IBOutlet weak var messageBubble: UIView!<br>    @IBOutlet weak var label: UILabel!<br>    @IBOutlet weak var rightImageView: UIImageView!<br>    <br>    @IBOutlet weak var leftImageView: UIImageView!<br>    <br>    override func awakeFromNib() {<br>        super.awakeFromNib()<br>        // 導圓角<br>        messageBubble.layer.cornerRadius = messageBubble.frame.size.height / 4<br>    }<br><br>    override func setSelected(_ selected: Bool, animated: Bool) {<br>        super.setSelected(selected, animated: animated)<br><br>        // Configure the view for the selected state<br>    }</pre><p>4.建立Message struct</p><pre>import Foundation<br><br>struct Message{<br>    let sender:String // email<br>    let body:String // 內容<br>}</pre><p>5.在ChatViewController內導入firebase、創建Firestore數據庫、創建訊息空字串</p><pre>import UIKit<br>import FirebaseCore<br>import FirebaseFirestore<br>import FirebaseAuth<br><br>class ChatViewController: UIViewController {<br>    <br>    @IBOutlet weak var tableView: UITableView!<br>    @IBOutlet weak var messageTextfield: UITextField!<br>    <br>    //創建Firestore數據庫<br>    let db = Firestore.firestore()<br>    <br>    var messages:[Message] = []<br>    <br>    <br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        //隱藏back按鈕<br>        navigationItem.hidesBackButton = true<br>        //設定tableView dataSource<br>        tableView.dataSource = self<br>        <br>        //使用自定義設計的文件(自定義xib文件)的第一步是在viewDidLoad註冊他<br>        tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)<br><br>    }</pre><p>6.新增UITableViewDataSource</p><p>(1)列數為messages的數量</p><pre><br>//MARK: - UITableViewDataSource<br>extension ChatViewController: UITableViewDataSource{<br>    //有幾列<br>    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br>        return messages.count<br>    }</pre><p>(2)內容:</p><p>確認訊息發送者是否與當前登入的用戶相同：Auth.auth().currentUser?.email</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Db26GoOfoV2H4FVHOFt-KA.png" /></figure><pre>    //表格中的內容<br>    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {<br>        let message = messages[indexPath.row]<br>        let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath) as! MessageCell<br>        cell.label.text = message.body<br>        <br>        //檢查訊息發送者是否與當前登入的用戶相同<br>        if message.sender == Auth.auth().currentUser?.email{<br>            cell.leftImageView.isHidden = true<br>            cell.rightImageView.isHidden = false<br>            cell.messageBubble.backgroundColor = UIColor(named: K.BrandColors.lightPurple)<br>            cell.label.textColor = UIColor(named: K.BrandColors.purple)<br>        }else{<br>            cell.leftImageView.isHidden = false<br>            cell.rightImageView.isHidden = true<br>            cell.messageBubble.backgroundColor = UIColor(named: K.BrandColors.purple)<br>            cell.label.textColor = UIColor(named: K.BrandColors.lightPurple)<br>        }<br>        <br>        return cell<br>    }<br>}</pre><p>7.按下送出按鈕後，上傳資料</p><p><a href="https://firebase.google.com/docs/firestore/security/get-started?authuser=0">Get started with Cloud Firestore Security Rules | Firebase</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9bAqUAdRZZ-hnctJ9HVSSw.png" /></figure><p>上傳的資料包含email、內容、時間</p><pre>    //送出按鈕<br>    @IBAction func sendPressed(_ sender: UIButton) {<br>        <br>        if messageTextfield.text != &quot;&quot;{<br>            <br>            if let messageBody = messageTextfield.text, let messageSender = Auth.auth().currentUser?.email{<br>                //            K.FStore.collectionName = &quot;messages&quot;<br>                //            K.FStore.senderField = &quot;sender&quot;<br>                //            K.FStore.bodyField = &quot;body&quot;<br>                //            K.FStore.dateField = &quot;date&quot;<br>                db.collection(K.FStore.collectionName).addDocument(data:[<br>                    //上傳的資料<br>                    K.FStore.senderField: messageSender,<br>                    K.FStore.bodyField: messageBody,<br>                    K.FStore.dateField: Date().timeIntervalSince1970<br>                ]) { error in<br>                    if let error = error{<br>                        print(&quot;There was an issue saving data to firestore, \(error)&quot;)<br>                    }else{<br>                        print(&quot;Successfully saved data&quot;)<br>                        DispatchQueue.main.async {<br>                            self.messageTextfield.text = &quot;&quot;<br>                        }<br>                    }<br>                }<br>            }<br>        }<br>        <br>    }</pre><p>8.新增取得資料的function</p><p>(1)addSnapshotListener: 當每一次發生改變時，讀取快照</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jkp47L-HkzqE3vGqIop_MA.png" /></figure><p>(2)以order(by: …)排序時間</p><p><a href="https://firebase.google.com/docs/firestore/query-data/order-limit-data?authuser=0">Order and limit data with Cloud Firestore | Firebase</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8TZDO3zg1FYDmaSo_xfVgA.png" /></figure><p>(3)querySnapshot:訪問該查詢的快照對象，並獲取其中包含的數據</p><p><a href="https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/QueryDocumentSnapshot?authuser=0">FirebaseFirestore Framework Reference</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bTAugNl5PQOW9FpPbinLPA.png" /></figure><pre>    func loadMessages(){<br>        //取得資料<br>        //addSnapshotListener: each time the contents change, another call updates the document snapshot<br>        //order(by: String): 排序<br>        db.collection(K.FStore.collectionName).order(by: K.FStore.dateField).addSnapshotListener { [self] (querySnapshot, error) in<br>            <br>            //清空messages資料<br>            self.messages = []<br>            <br>            if let error = error {<br>                print(&quot;There was an issue retrieving data from Firestore, \(error)&quot;)<br>            } else {<br>                if let snapshotDocuments = querySnapshot?.documents{<br>                    for doc in snapshotDocuments{<br>                        //data() -&gt; [String:Any]<br>                        let data = doc.data()<br>                        if let messageSender = data[K.FStore.senderField] as? String, let messageBody = data[K.FStore.bodyField] as? String{<br>                            let newMessage = Message(sender: messageSender, body: messageBody)<br>                            self.messages.append(newMessage)<br>                            <br>                            //呼叫tableView<br>                            DispatchQueue.main.async {<br>                                self.tableView.reloadData()<br>                                //創建要滾動到的行<br>                                let indexPath = IndexPath(row: self.messages.count - 1, section: 0)<br>                                self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)<br>                            }<br>                        }<br>                    }<br>                }<br>            }<br>        }<br>    }</pre><p>(4)在viewDidLoad()中新增</p><pre>    override func viewDidLoad() {<br>        super.viewDidLoad()<br><br>        //提取數據庫中所有當前數據<br>        loadMessages()<br>    }</pre><p>9.logOut按鈕</p><pre>    //logOut按鍵<br>    @IBAction func logOutPressed(_ sender: UIBarButtonItem) {<br>        do {<br>            try Auth.auth().signOut()<br>            //回到根畫面<br>            navigationController?.popToRootViewController(animated: true)<br>            <br>        } catch let signOutError as NSError {<br>            print(&quot;Error signing out: %@&quot;, signOutError)<br>        }<br>    }<br>}</pre><p>10.新增IQKeyboardManagerSwift的swift manager</p><p><a href="https://cocoapods.org/pods/IQKeyboardManagerSwift">IQKeyboardManagerSwift</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*1zP2z0GxPnTQBCTsSX1MFA.png" /></figure><p>加入網址：</p><p><a href="https://github.com/hackiftekhar/IQKeyboardManager/wiki/Properties-&amp;-Functions">https://github.com/hackiftekhar/IQKeyboardManager/wiki/Properties-&amp;-Functions</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JTL0_N6BCwKdr-zXatCLNA.png" /></figure><pre>import IQKeyboardManagerSwift<br>      <br>@UIApplicationMain<br>class AppDelegate: UIResponder, UIApplicationDelegate {<br><br><br><br>    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {<br>        <br>        FirebaseApp.configure()<br>        <br>        let db = Firestore.firestore()<br>        <br>        print(db)<br>        <br>        //使有鍵盤時，畫面自動上升<br>        IQKeyboardManager.shared.enable = true<br>        //不要有鍵盤的送出按鈕<br>        IQKeyboardManager.shared.enableAutoToolbar = false<br>        //點擊任何一個地方，鍵盤消失<br>        IQKeyboardManager.shared.shouldResignOnTouchOutside = true<br>        <br>        <br>        return true<br>    }</pre><p>11.全部功能修改完成後，將可讀寫的功能改為只有登入者可讀寫</p><p><a href="https://firebase.google.com/docs/firestore/security/get-started">Get started with Cloud Firestore Security Rules | Firebase</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hWtq1BDGFQstdJCLFcHDHQ.png" /></figure><p>before</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o-V0aLSCrz1t11RS-cM5dQ.png" /></figure><p>after</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jjCI7ztEzx5JOxUGTtiI8g.png" /></figure><p>12.畫面展示：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*CPAjAQJaDp029prS4mVyUA.gif" /></figure><p>GitHub:</p><p><a href="https://github.com/Phrase0/Flash-Chat">GitHub - Phrase0/Flash-Chat</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d935952228cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#61 Flash Chat聊天對話 — 上]]></title>
            <link>https://medium.com/@b612starnight/61-flash-chat%E8%81%8A%E5%A4%A9%E5%B0%8D%E8%A9%B1-%E4%B8%8A-1e7af81d1e7?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e7af81d1e7</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Tue, 02 May 2023 05:51:05 GMT</pubDate>
            <atom:updated>2023-05-02T05:54:45.088Z</atom:updated>
            <content:encoded><![CDATA[<h3>#61 Flash Chat聊天對話 (上)</h3><p>目的：利用 Firebase 實現 iOS App 的登入註冊功能</p><p>1.先下載開源專案Flash Chat的starter檔案</p><p><a href="https://github.com/appbrewery/Flash-Chat-iOS13">https://github.com/appbrewery/Flash-Chat-iOS13</a></p><p>2.參考下方文件，建立fireBase資料庫</p><p><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%A8%AD%E5%AE%9A-ios-app-%E7%9A%84-firebase-%E5%8A%9F%E8%83%BD-afcaaabe00f2">設定 iOS App 的 Firebase 功能和安裝 Firebase 套件</a></p><p>在macbook m1版本時，可能發生無法安裝的錯誤</p><ul><li><a href="https://stackoverflow.com/questions/70287369/dont-forget-to-include-the-crash-report-log-file-under-diagnosticreports-direct">Don&#39;t forget to include the Crash Report log file under DiagnosticReports directory in bug reports</a></li><li><a href="https://stackoverflow.com/questions/67443265/error-regarding-undefined-method-map-for-nilnilclass-for-flutter-app-cocoap">Error Regarding undefined method `map&#39; for nil:NilClass for Flutter App / CocoaPod Error</a></li></ul><p>請改以這兩行進行安裝</p><pre>sudo arch -x86_64 gem install ffi<br>arch -x86_64 pod install</pre><p>3.安裝完後，Podfile內的程式碼</p><pre>platform :ios, &#39;13.0&#39;<br><br>target &#39;Flash Chat iOS13&#39; do<br>  use_frameworks!<br><br>  # Pods for Flash Chat iOS13<br>  <br>  pod &#39;FirebaseAuth&#39;<br>  pod &#39;FirebaseFirestore&#39;<br><br>end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6_YSJTvhlTWcmGN-13UD-A.png" /></figure><p>4.設定pods內 generate position- dependent code改為No</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yBfWNYU_nqr74LnMC5eckw.png" /></figure><p>5.建立Constants，將需要使用的名稱建檔，以避免日後輸入時發生錯誤</p><pre>struct K {<br>    static let appName = &quot;⚡️FlashChat&quot;<br>    static let cellIdentifier = &quot;ReusableCell&quot;<br>    static let cellNibName = &quot;MessageCell&quot;<br>    static let registerSegue = &quot;RegisterToChat&quot;<br>    static let loginSegue = &quot;LoginToChat&quot;<br>    <br>    struct BrandColors {<br>        static let purple = &quot;BrandPurple&quot;<br>        static let lightPurple = &quot;BrandLightPurple&quot;<br>        static let blue = &quot;BrandBlue&quot;<br>        static let lighBlue = &quot;BrandLightBlue&quot;<br>    }<br>    <br>    struct FStore {<br>        static let collectionName = &quot;messages&quot;<br>        static let senderField = &quot;sender&quot;<br>        static let bodyField = &quot;body&quot;<br>        static let dateField = &quot;date&quot;<br>    }<br>}</pre><p>6.參考Firebase文件，在AppDelegate中加入</p><p><a href="https://firebase.google.com/docs/firestore/quickstart?authuser=0#swift">Get started with Cloud Firestore | Firebase</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bmeJOLIcuVd0LjlKCZ1WGw.png" /></figure><pre>import UIKit<br>import FirebaseCore<br>import FirebaseFirestore<br>import FirebaseAuth<br>import IQKeyboardManagerSwift<br>      <br>@UIApplicationMain<br>class AppDelegate: UIResponder, UIApplicationDelegate {<br><br><br>    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {<br>        <br>        FirebaseApp.configure()<br>        <br>        let db = Firestore.firestore()<br>        <br>        print(db)<br>    <br>        return true<br>    }</pre><p>7.起始畫面WelcomeViewController中加入迴圈，讓文字能夠一個一個顯示</p><pre>import UIKit<br><br>class WelcomeViewController: UIViewController {<br>    <br>    @IBOutlet weak var titleLabel: UILabel!<br>    <br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        //讓文字一個一個顯示<br>        titleLabel.text = &quot;&quot;<br>        var charIndex = 0.0<br>        let titleText = K.appName<br>        <br>        <br>        for letter in titleText{<br>            Timer.scheduledTimer(withTimeInterval: 0.1 * charIndex, repeats: false) { timer in<br>                self.titleLabel.text?.append(letter)<br>            }<br>            charIndex += 1<br>        }<br>    }<br>}</pre><p>8.在RegisterViewController內加入註冊帳號方式</p><p><a href="https://firebase.google.com/docs/auth/ios/password-auth?authuser=0">Authenticate with Firebase using Password-Based Accounts on Apple Platforms | Firebase Authentication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fJkz-tpQ2NqkmhvGt2i-Qg.png" /></figure><pre>import UIKit<br>import FirebaseCore<br>import FirebaseFirestore<br>import FirebaseAuth<br><br><br>class RegisterViewController: UIViewController {<br><br>    @IBOutlet weak var emailTextfield: UITextField!<br>    @IBOutlet weak var passwordTextfield: UITextField!<br>    <br>    @IBAction func registerPressed(_ sender: UIButton) {<br>        <br>        if let email = emailTextfield.text, let password = passwordTextfield.text{<br>            //authResult:用戶成功註冊後的帳戶數據<br>            Auth.auth().createUser(withEmail: email, password: password) { authResult, error in<br>                if let e = error{<br>                    //localizedDescription:以使用者的語言顯示錯誤<br>                    print(e.localizedDescription)<br>                }else{<br>                    //跳轉到ChatViewController<br>                    self.performSegue(withIdentifier: K.registerSegue, sender: self)<br>                }             <br>            }<br>        }<br>    }<br><br>}</pre><p>9.在LoginViewController內加入登入帳號方式</p><p><a href="https://firebase.google.com/docs/auth/ios/password-auth?authuser=0">Authenticate with Firebase using Password-Based Accounts on Apple Platforms | Firebase Authentication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oGkbXSqg48Z_p7kxKQZCYg.png" /></figure><pre>import UIKit<br>import FirebaseCore<br>import FirebaseFirestore<br>import FirebaseAuth<br><br><br>class LoginViewController: UIViewController {<br>    <br>    @IBOutlet weak var emailTextfield: UITextField!<br>    @IBOutlet weak var passwordTextfield: UITextField!<br>    <br>    <br>    @IBAction func loginPressed(_ sender: UIButton) {<br>        <br>        if let email = emailTextfield.text, let password = passwordTextfield.text{<br>            <br>            Auth.auth().signIn(withEmail: email, password: password) { authResult, error in<br>                if let e = error{<br>                    //localizedDescription:以使用者的語言顯示錯誤<br>                    print(e.localizedDescription)<br>                }else{<br>                    //跳轉到ChatViewController<br>                    self.performSegue(withIdentifier: K.loginSegue, sender: self)<br>                }<br>            }<br>        }<br>    }<br>    <br>}</pre><p>參考：</p><ul><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/%E9%96%8B%E7%99%BC-firebase-cloud-firestore-%E7%9A%84-ios-app-fe38d6ad70a7">開發 Firebase Cloud Firestore 的 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/%E5%88%A9%E7%94%A8-firebase-%E5%AF%A6%E7%8F%BE-ios-app-%E7%9A%84%E7%99%BB%E5%85%A5%E5%8A%9F%E8%83%BD-cf1795ccec00">利用 Firebase 實現 iOS App 的登入註冊功能</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e7af81d1e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#60 比特幣換算app]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/60-%E6%AF%94%E7%89%B9%E5%B9%A3%E6%8F%9B%E7%AE%97app-69bbeb75fd14?source=rss-75af55527d38------2</link>
            <guid isPermaLink="false">https://medium.com/p/69bbeb75fd14</guid>
            <dc:creator><![CDATA[51°30′26″N]]></dc:creator>
            <pubDate>Fri, 17 Feb 2023 10:04:12 GMT</pubDate>
            <atom:updated>2023-02-17T14:37:11.602Z</atom:updated>
            <content:encoded><![CDATA[<p>目的：讀取不同幣值的比特幣匯率</p><p><strong>1.建立畫面</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*WquweSTe_0astSdpeA-WYw.png" /></figure><p><strong>2.建立架構</strong></p><p>Model:<br>(1)CoinData：解析JSON的struct<br>(2)CoinManager：解析api</p><p>View:<br>Main：主畫面</p><p>Controller:<br>WeatherViewController：與畫面相關的程式</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*CWmsl-VHPvEvRHWjz-IslA.png" /></figure><p><strong>3.在ViewController中連結label</strong></p><pre>import UIKit<br><br>class ViewController: UIViewController {<br>    <br>    <br>    @IBOutlet weak var bitcoinLabel: UILabel!<br>    @IBOutlet weak var currencyLabel: UILabel!<br>    @IBOutlet weak var currencyPicker: UIPickerView!</pre><h4>part1.delegate</h4><p><strong>4.實作UIPickerViewDataSource &amp; delegate方法</strong><br>*coinManager.getCoinPrice(for: selectCurrency)等下會寫</p><pre>//MARK: - UIPickerViewDataSource &amp; delegate<br>extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate{<br>    <br>    func numberOfComponents(in pickerView: UIPickerView) -&gt; Int {<br>        return 1<br>    }<br>    <br>    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {<br>        return coinManager.currencyArray.count<br>    }<br>    <br>    //pickerView的標題<br>    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {<br>        return coinManager.currencyArray[row]<br>    }<br>    <br>    //選中時需要做什麼<br>    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {<br>        //取得是哪一種幣值<br>        let selectCurrency = coinManager.currencyArray[row]<br>        coinManager.getCoinPrice(for: selectCurrency)<br>    }<br>}</pre><p><strong>5.比特幣api</strong></p><p><a href="https://www.coinapi.io/">All Cryptocurrency APIs in One Place</a></p><p>在exchange rates裡有詳解url網址組成方式</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WMkIbj0c8reMqjTpc6B9tA.png" /></figure><p><strong>6.在CoinData中串接好需要的資料</strong></p><pre>import Foundation<br><br>struct CoinData: Codable {<br>    let rate: Double<br>}</pre><p><strong>7.在CoinManager中加入URL網址與幣值的array</strong></p><pre>struct CoinManager {<br>    <br>    var delegate:CoinManagerDelegate?<br>    <br>    let baseURL = &quot;https://rest.coinapi.io/v1/exchangerate/BTC&quot;<br>    let apiKey = &quot;your api key&quot;<br>    <br>    let currencyArray = [&quot;AUD&quot;, &quot;BRL&quot;,&quot;CAD&quot;,&quot;CNY&quot;,&quot;EUR&quot;,&quot;GBP&quot;,&quot;HKD&quot;,&quot;IDR&quot;,&quot;ILS&quot;,&quot;INR&quot;,&quot;JPY&quot;,&quot;MXN&quot;,&quot;NOK&quot;,&quot;NZD&quot;,&quot;PLN&quot;,&quot;RON&quot;,&quot;RUB&quot;,&quot;SEK&quot;,&quot;SGD&quot;,&quot;USD&quot;,&quot;ZAR&quot;]</pre><p><strong>8.建立CoinManagerDelegate的protocol(等下在WeatherViewController也會用到)</strong></p><pre>protocol CoinManagerDelegate {<br>    func didUpdateCoinManager(price:String, currency:String)<br>    //將錯誤管理器中傳遞出去<br>    func didFailWithError(error: Error)<br>}</pre><p><strong>9.從WeatherModel中解析JSON</strong></p><pre>    func parseJSON(_ data:Data)-&gt;Double?{<br>        //建立解碼器<br>        let decoder = JSONDecoder()<br>        do{<br>            //從CoinData分解資料<br>            let decodedData = try decoder.decode(CoinData.self, from: data)<br>            //取得匯率<br>            let lastPrice = decodedData.rate<br>            <br>            return lastPrice<br>        }catch{<br>            delegate?.didFailWithError(error: error)<br>            return nil<br>        }<br>    }</pre><p><strong>10.串接api</strong></p><p>*使用delegate處理error &amp; didUpdateCoinManager</p><pre>    func getCoinPrice(for currency:String){<br>        let urlString = &quot;\(baseURL)/\(currency)?apikey=\(apiKey)&quot;<br>        <br>        if let url = URL(string: urlString){<br>            let session = URLSession(configuration: .default)<br>            let task = session.dataTask(with: url) {<br>                data, response, error in<br>                if let error = error {<br>                    self.delegate?.didFailWithError(error: error)<br>                    return<br>                }<br>                if let safeData = data {<br>                    if let bitcoinPrice = self.parseJSON(safeData){<br>                        let priceString = String(format: &quot;%.2f&quot;, bitcoinPrice)<br>                        self.delegate?.didUpdateCoinManager(price: priceString, currency: currency)<br>                    }<br>                }<br>            }<br>            task.resume()<br>        }<br>        <br>    }</pre><h4>part2.delegate</h4><p><strong>11.回到ViewController</strong></p><p>(1)建立CoinManager()物件</p><pre>var coinManager = CoinManager()</pre><p>(2)在viewDidLoad()中加入coinManager.delegate = self</p><pre>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        currencyPicker.dataSource = self<br>        currencyPicker.delegate = self<br>        coinManager.delegate = self</pre><p>(3)實作方法，從CoinManagerDelegate連結資料</p><pre>//MARK: - CoinManagerDelegate<br>extension ViewController:CoinManagerDelegate{<br><br>    func didUpdateCoinManager(price:String, currency:String){<br>        <br>        DispatchQueue.main.async {<br>            self.bitcoinLabel.text = price<br>            self.currencyLabel.text = currency<br>        }<br>    }<br>    //將錯誤管理器中傳遞出去<br>    func didFailWithError(error: Error) {<br>        print(error)<br>    }<br>}</pre><p><strong>12.完整程式碼及畫面展示</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*V6IAy8OfHQWHZp-pDkPRww.gif" /></figure><p><a href="https://github.com/Phrase0/ByteCoin">GitHub - Phrase0/ByteCoin</a></p><p>參考：</p><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/180-%E9%81%A9%E5%90%88%E6%96%B0%E6%89%8B%E7%B7%B4%E7%BF%92%E4%B8%B2%E6%8E%A5%E7%9A%84-rest-api-ef027d4fc066">#180 適合新手練習串接的 REST API</a></li><li><a href="https://docs.coinapi.io/market-data/rest-api/exchange-rates">Exchange rates | REST API | Market Data API - CoinAPI.io Documentation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69bbeb75fd14" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/60-%E6%AF%94%E7%89%B9%E5%B9%A3%E6%8F%9B%E7%AE%97app-69bbeb75fd14">#60 比特幣換算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>
    </channel>
</rss>