使用var关键字定义变量,可以修改
使用let关键字定义常量,不可修改
提示:在Swift开发中,通常先定义常量let,只有必须要修改的时候,才定义成var,这样会更安全。
2.类型转换
Swift是一个对类型要求及其严格的语言,任何数据类型之间,都不能隐式转换。如果要在不同的类型之间进行计算,必须转换格式
解决方案一:强制转换
提示:Swift在上面定义i,j常量的时候自动推断了类型,Swift中默认的整数格式是Int(long 64位),默认的小数格式是Double(而OC中默认的小数格式是CGFloat)。
解决方案二:定义的时候指明类型
3.分支语句
在C语言中,if有一个特点,“非零即真”。但是在Swift中,没有这个概念,在分支语句中,必须准确地指明条件的真假。
Swift中的条件语句有两个特点:
1>条件不需要括号
2>必须要有 {}
4. 循环
循环的主要用法有以下四种:
5.字符串
Swift中默认的字符串类型为String,是一个结构体,所以它的效率更高
<ol class="linenums list-paddingleft-2" style="margin:0px; width:803.6875px; padding-left:30px; color:rgb(30,52,123)"><li class="L0" style="clear:both; color:rgb(190,190,197); line-height:18px; padding-left:12px"><code class="language-cpp" style="margin:0px; padding:0px; border:none; font-family:Consolas,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="kwd" style="color:#1e347b;">import</span><span class="pln" style="color:#48484c;"> </span><span class="typ" style="color:teal;">UIKit</span></code></li><li class="L1" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"></code> </li><li class="L2" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">var str </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"Hello World"</span></code></li><li class="L3" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"></code> </li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 快速遍历字符串</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="kwd" style="color:#1e347b;">for</span><span class="pln" style="color:#48484c;"> c in str </span><span class="pun" style="color:#93a1a1;">{</span></code></li><li class="L6" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;"> println</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">c</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L7" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pun" style="color:#93a1a1;">}</span></code></li><li class="L8" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"></code> </li><li class="L9" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 字符串之间拼接</span></code></li><li class="L0" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">var str1 </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> str </span><span class="pun" style="color:#93a1a1;">+</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">",你好"</span></code></li><li class="L1" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">let i </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="lit" style="color:#195f91;">100</span></code></li><li class="L3" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">let view </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="typ" style="color:teal;">UIView</span><span class="pun" style="color:#93a1a1;">()</span></code></li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">view</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">backgroundColor </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="typ" style="color:teal;">UIColor</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">redColor</span><span class="pun" style="color:#93a1a1;">()</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"></code> </li><li class="L6" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 变量拼接进字符串</span></code></li><li class="L7" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">println</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">str1 </span><span class="pun" style="color:#93a1a1;">+</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"(i) aaa (view.backgroundColor)"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L8" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">//字符串格式化</span></code></li><li class="L0" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">let str2 </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="typ" style="color:teal;">String</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">format</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"%02d:%02d:%02d"</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> arguments</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">[</span><span class="lit" style="color:#195f91;">1</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> </span><span class="lit" style="color:#195f91;">5</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> </span><span class="lit" style="color:#195f91;">10</span><span class="pun" style="color:#93a1a1;">])</span></code></li><li class="L1" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 在Swift中,如果字符串要结合range一起使用,建议先转成NSString</span></code></li><li class="L3" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 因为 swift 的字符串和range一起使用很蛋疼</span></code></li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">let myString</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> </span><span class="typ" style="color:teal;">NSString</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"hello"</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">myString</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">substringWithRange</span><span class="pun" style="color:#93a1a1;">(</span><span class="typ" style="color:teal;">NSMakeRange</span><span class="pun" style="color:#93a1a1;">(</span><span class="lit" style="color:#195f91;">1</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> </span><span class="lit" style="color:#195f91;">4</span><span class="pun" style="color:#93a1a1;">))</span></code></li></ol>
字符串的快速遍历
字符串拼接
6.数组
添加元素</span></code></li><li class="L7" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">array4</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">append</span><span class="pun" style="color:#93a1a1;">(</span><span class="str" style="color:#dd1144;">"hello"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L8" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">array4</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">append</span><span class="pun" style="color:#93a1a1;">(</span><span class="str" style="color:#dd1144;">"world"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L9" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 数组容量</span></code></li><li class="L1" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">println</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">array4</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">capacity</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L2" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// keepCapacity表示是否保持容量,如果为true当再次追加元素的时候,会在原有容量的基础上乘以2,如果是false,则是当前容量*2</span></code></li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">array4</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">removeAll</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">keepCapacity</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> </span><span class="kwd" style="color:#1e347b;">true</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="kwd" style="color:#1e347b;">for</span><span class="pln" style="color:#48484c;"> i in </span><span class="lit" style="color:#195f91;">0.</span><span class="pun" style="color:#93a1a1;">..</span><span class="lit" style="color:#195f91;">5</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">{</span></code></li><li class="L7" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;"> array4</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">append</span><span class="pun" style="color:#93a1a1;">(</span><span class="str" style="color:#dd1144;">"(i)"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L8" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;"> println</span><span class="pun" style="color:#93a1a1;">(</span><span class="str" style="color:#dd1144;">"(array4): (array4.capacity)"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L9" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pun" style="color:#93a1a1;">}</span></code></li></ol>
7.字典
常见的字典使用如下:
内容</span></code></li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">dict1</span><span class="pun" style="color:#93a1a1;">[</span><span class="str" style="color:#dd1144;">"name"</span><span class="pun" style="color:#93a1a1;">]</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"laolin"</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">dict1</span><span class="pun" style="color:#93a1a1;">[</span><span class="str" style="color:#dd1144;">"age"</span><span class="pun" style="color:#93a1a1;">]</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="lit" style="color:#195f91;">88</span></code></li><li class="L6" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">//再次设置会覆盖</span></code></li><li class="L8" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">dict1</span><span class="pun" style="color:#93a1a1;">[</span><span class="str" style="color:#dd1144;">"name"</span><span class="pun" style="color:#93a1a1;">]</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"abc"</span></code></li><li class="L9" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">// 删除</span></code></li><li class="L1" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">dict1</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">removeValueForKey</span><span class="pun" style="color:#93a1a1;">(</span><span class="str" style="color:#dd1144;">"age"</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L2" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="com" style="color:#93a1a1;">//合并字典</span></code></li><li class="L4" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;">var dict2 </span><span class="pun" style="color:#93a1a1;">=</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">[</span><span class="str" style="color:#dd1144;">"title"</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> </span><span class="str" style="color:#dd1144;">"boss"</span><span class="pun" style="color:#93a1a1;">]</span></code></li><li class="L5" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="kwd" style="color:#1e347b;">for</span><span class="pln" style="color:#48484c;"> </span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">k</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> v</span><span class="pun" style="color:#93a1a1;">)</span><span class="pln" style="color:#48484c;"> in dict2 </span><span class="pun" style="color:#93a1a1;">{</span></code></li><li class="L6" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pln" style="color:#48484c;"> dict1</span><span class="pun" style="color:#93a1a1;">.</span><span class="pln" style="color:#48484c;">updateValue</span><span class="pun" style="color:#93a1a1;">(</span><span class="pln" style="color:#48484c;">v</span><span class="pun" style="color:#93a1a1;">,</span><span class="pln" style="color:#48484c;"> forKey</span><span class="pun" style="color:#93a1a1;">:</span><span class="pln" style="color:#48484c;"> k</span><span class="pun" style="color:#93a1a1;">)</span></code></li><li class="L7" style="clear:both; color:rgb(190,monospace; word-wrap:break-word; max-width:100%; background-color:transparent"><span class="pun" style="color:#93a1a1;">}</span></code></li></ol>
http://blog.csdn.net/qq441571205/article/details/45606323
原文链接:https://www.f2er.com/swift/324607.html