<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Swift &#8211; Larry的午茶時光</title>
	<atom:link href="https://blog.yuyansoftware.com.tw/tag/swift/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yuyansoftware.com.tw</link>
	<description></description>
	<lastBuildDate>Thu, 28 May 2026 09:12:40 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://blog.yuyansoftware.com.tw/wp-content/uploads/2022/10/favicon-45x45.png</url>
	<title>Swift &#8211; Larry的午茶時光</title>
	<link>https://blog.yuyansoftware.com.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Swift (3): about Optional</title>
		<link>https://blog.yuyansoftware.com.tw/2015/11/swift-optional/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Sun, 01 Nov 2015 03:26:00 +0000</pubDate>
				<category><![CDATA[好文記錄與分享]]></category>
		<category><![CDATA[軟體開發隨筆]]></category>
		<category><![CDATA[Swift]]></category>
		<guid isPermaLink="false">http://test234.yuyansoftware.com.tw/2015/11/01/swift-3-about-optional/</guid>

					<description><![CDATA[Marin Todorov&#39;s article:&#160;Swift Optionals: When &#8230; ]]></description>
										<content:encoded><![CDATA[<p><span style="font-size: large;">Marin Todorov&#39;s article:&nbsp;Swift Optionals: When to use if let, when ? and !, when as? and as</span><br />
<a href="http://www.touch-code-magazine.com/swift-optionals-use-let/" target="_blank" rel="noopener noreferrer"><span style="font-size: large;">http://www.touch-code-magazine.com/swift-optionals-use-let/</span></a><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;"><b>The &#8220;class?property&#8221; Syntax</b></span></p>
<pre>controller.navigationController?.pushViewController(myViewController, animated: true)<span style="font-size: large;">
</span></pre>
<p><span style="font-size: large;">如果 controller.navigationController 不為 nil, 執行 function. 如果 controller.navigationController 為 nil, 整行程式碼不執行。他可以替換成：</span></p>
<pre>if controller.navigationController != nil {
    controller.navigationController.pushViewController(myViewController, animated: true)
}</pre>
<p><span style="font-size: large;"><br />
</span><span style="font-size: large;"><b>The &#8220;if let&#8221; Syntax  </b></span></p>
<pre>if let nav = controller.navigationController {
    nav.pushViewController(myViewController, animated: true)
} else {
    //show an alert ot something else
}</pre>
<p><span style="font-size: large;">&#8220;if let&#8221; 的語法隱含了 &#8220;and if nav != nil&#8221;. 當你有多行程式碼需要 unwrap, use &#8220;if let&#8221;.</span></p>
<p><span style="font-size: large;"><b>The &#8220;as?&#8221; Syntax </b></span></p>
<pre>let myVC = controller.presentedViewController as? MyViewController
myVC?.lastUpdated = NSDate()<span style="font-size: large;">
</span></pre>
<p><span style="font-size: large;">&#8220;as?&#8221; 有點像 C++ 的 dynamic cast, 以上面例子來說，myVC 有可能是 nil. 正因如此，&#8221;as?&#8221; 底下通常要接 &#8220;class?property&#8221;. 另外如果用 &#8220;as&#8221; 而不是 &#8220;as?&#8221; (沒有問號)，則是 C++ 的 static cast，上面文章連結也有提到你要 101% 確定可以 cast 才用 &#8220;as&#8221; 去 cast. </span></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Swift (2): about the Navigation</title>
		<link>https://blog.yuyansoftware.com.tw/2015/10/swift-navigation/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Thu, 22 Oct 2015 07:35:00 +0000</pubDate>
				<category><![CDATA[軟體開發隨筆]]></category>
		<category><![CDATA[Swift]]></category>
		<guid isPermaLink="false">http://test234.yuyansoftware.com.tw/2015/10/22/swift-2-about-the-navigation/</guid>

					<description><![CDATA[當你將一個 Navigation Controller 加入到 View (或是 View Controlle &#8230; ]]></description>
										<content:encoded><![CDATA[<p><span style="font-size: large;">當你將一個 Navigation Controller 加入到 View (或是 View Controller, 效果一樣)，storyboard 上會新增一個 Navigation Controller Scene, 該 View 的同一層 (以 Document Outline 的架構來說) 會有一個 Navigation Item (也可以稱之為 navigation bar). 新增的 Navigation Controller 會與 View Controller 以 pic1 連結，官方文件稱之 &#8220;The (table) view controller is the navigation controller&#39;s root view controller.&#8221; </span><br />
<span style="font-size: large;"><br />
</span></p>
<table align="center" cellpadding="0" cellspacing="0" style="margin-left: auto; margin-right: auto; text-align: center;">
<tbody>
<tr>
<td style="text-align: center;"><a href="http://1.bp.blogspot.com/-1EBex0eqnpI/ViXKkY1v4OI/AAAAAAAAAEI/rWPS7rBb2X4/s1600/pic1.png" style="margin-left: auto; margin-right: auto;"><span style="font-size: large;"><img decoding="async" border="0" src="https://1.bp.blogspot.com/-1EBex0eqnpI/ViXKkY1v4OI/AAAAAAAAAEI/rWPS7rBb2X4/s1600/pic1.png"></span></a></td>
</tr>
<tr>
<td style="text-align: center;"><span style="font-size: large;">pic1</span></td>
</tr>
</tbody>
</table>
<p><span style="font-size: large;">拖拉一個 &#8220;Bar Button Item&#8221; 到 navigation bar, 在右邊 attributes inspector 可調整 &#8220;Bar Button Item&#8221; 為 Add, Edit Done, Cancel 等一般我們在 App 上方看到的。Control-drag 剛剛新增的 bar button 到你要到的 view controller, 兩個 view controller 會用 pic2 (或是 pic3) 做連結，切換過去的 view controller 左上角會自然出現 Back.</span><br />
<span style="font-size: large;"><br />
</span></p>
<table align="center" cellpadding="0" cellspacing="0" style="margin-left: auto; margin-right: auto; text-align: center;">
<tbody>
<tr>
<td><a href="http://4.bp.blogspot.com/-XD7TaOctnWQ/ViXKnpqnrBI/AAAAAAAAAEQ/5FTzqDRVqa8/s1600/pic2.png" style="margin-left: auto; margin-right: auto;"><span style="font-size: large;"><img decoding="async" border="0" src="https://4.bp.blogspot.com/-XD7TaOctnWQ/ViXKnpqnrBI/AAAAAAAAAEQ/5FTzqDRVqa8/s1600/pic2.png"></span></a><br />
<span style="font-size: large;">pic2</span></td>
</tr>
<tr>
<td style="text-align: center;"><a href="http://3.bp.blogspot.com/-HmqCtWSr0Q4/ViiOUVzxY-I/AAAAAAAAAEg/WjLnaKveTo0/s1600/pic3.png" style="margin-left: 1em; margin-right: 1em;"><span style="font-size: large;"><img decoding="async" border="0" src="https://3.bp.blogspot.com/-HmqCtWSr0Q4/ViiOUVzxY-I/AAAAAAAAAEg/WjLnaKveTo0/s1600/pic3.png"></span></a><br />
<span style="font-size: large;">pic3</span></td>
</tr>
</tbody>
</table>
<p><span style="font-size: large;">pic1, 2, 3 在蘋果官方文件稱之為 &#8220;segue&#8221; (發音類似 Segway, 順道一提，Segway 被中國的 Ninebot 買了下來，雷軍是 Ninebot 的股東，所以 10/19 在小米電視3的發佈會上雷軍也發佈了 Ninebot 的產品，這樣應該有助於記憶 segue <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span><br />
<span style="font-size: large;"></span><br />
<a name="more"></a><span style="font-size: large;">Segue 有幾種類型：push, modal, 等 (Xcode7是叫 show, present modally), 目前的理解是一般在 App 中切換頁面是用 push, modal 比較像之前 Win32 modal dialog 的概念，像是 add new item 就可以用 modal.</span><br />
<span style="font-size: large;">上面提到 push 切換過去的 view controller 左上角自然出現 back, 但是 modal 切換過去的 view controller 是完全空白的 (所以要 dismiss 自己)</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">About the Unwind Segue.</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">// In your origin view controller</span><br />
<span style="font-size: large;">override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {</span><br />
<span style="font-size: large;">}&nbsp;</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">// In your destination view controller</span><br />
<span style="font-size: large;">@IBAction func unwindToYourDestinationView(sender: UIStoryboardSegue) {</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">將 storyboard origin view 的 Save button 拉到 origin view 上方的 Exit item, 選擇 unwindToYourDestinationView. 這樣按 Save 時就會觸發上方兩個 function (將 modal view 設定的資料傳回)。</span><span style="font-size: large;">Cancel 的部分簡單藉由 Action handler, 在其中調用 dismissViewControllerAnimated 即可。</span></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Swift (1): Swift 的 data structure</title>
		<link>https://blog.yuyansoftware.com.tw/2015/10/swift-data-structure/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Wed, 21 Oct 2015 03:16:00 +0000</pubDate>
				<category><![CDATA[軟體開發隨筆]]></category>
		<category><![CDATA[Swift]]></category>
		<guid isPermaLink="false">http://test234.yuyansoftware.com.tw/2015/10/21/swift-1-swift-%e7%9a%84-data-structure/</guid>

					<description><![CDATA[https://developer.apple.com/library/prerelease/ios/docu &#8230; ]]></description>
										<content:encoded><![CDATA[<p><span style="font-size: large;"><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105" target="_blank" rel="noopener noreferrer">https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105</a></span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">其實 Swift 與 Python 的 data structures 是類似的，Swift 有: Array, Set, and Dictionary.</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">(以下 sample code 都是出自於上方蘋果教程文件)</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;"><b>Array</b></span><br />
<span style="font-size: large;">initializer syntax:</span></p>
<pre>var someInts = [Int]()
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
</pre>
<p><span style="font-size: large;">array literal (shorthand):</span></p>
<pre>var shoppingList: [String] = ["Eggs", "Milk"]
var shoppingList = ["Eggs", "Milk"]
</pre>
<p><span style="font-size: large;">用 [0], [1], &#8230; subscript syntax (蘋果官方文件取的名詞) 來存取 member.</span><br />
<span style="font-size: large;"></span><br />
<a name="more"></a><span style="font-size: large;"><b>Set</b></span><br />
<span style="font-size: large;">initializer syntax:</span></p>
<pre>var letters = Set&lt;Character&gt;()</pre>
<p><span style="font-size: large;">array literal (對，他不是叫 set literal):</span></p>
<pre>var favoriteGenres: Set&lt;String&gt; = ["Rock", "Classical", "Hip hop"]
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
</pre>
<p><span style="font-size: large;">(沒有指定 type Set 就會被當作 Arrary)</span></p>
<p><span style="font-size: large;">Set 不能用 subscript syntax 來存取 member，合理，因為 Set 沒有順序性。用 mySet.contains(item) 來判斷 item 是否存在於 mySet.</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;"><b>Dictionary</b></span><br />
<span style="font-size: large;">initializer syntax:</span></p>
<pre>var namesOfIntegers = [Int: String]()
</pre>
<p><span style="font-size: large;">dictionary literal:</span></p>
<pre>var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
</pre>
<p><span style="font-size: large;">存取用 subscript syntax。</span><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">跟 Python 比起來，Swift collections 都是中括號。</span><span style="font-size: large;">Swift 也有 Tuple (小括號)，用於 loop 的參數值，function 的 input/output 值等等，但不屬於 collection.</span></p>
<div><span style="font-size: large;"><br />
</span><span style="font-size: large;">蘋果官方 Swift 的教程 (go through an example)</span><br />
<a href="https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/" target="_blank" rel="noopener noreferrer"><span style="font-size: large;">https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/</span></a><br />
<span style="font-size: large;"><br />
</span><span style="font-size: large;">蘋果官方的 App Distribution Guide</span><br />
<a href="https://developer.apple.com/library/prerelease/ios/documentation/IDEs/Conceptual/AppDistributionGuide/Introduction/Introduction.html" target="_blank" rel="noopener noreferrer"><span style="font-size: large;">https://developer.apple.com/library/prerelease/ios/documentation/IDEs/Conceptual/AppDistributionGuide/Introduction/Introduction.html</span></a></p>
</div>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
