swift基础教程笔记

http://www.imooc.com/learn/127 

《玩儿转swift》 慕课网教程笔记,自己根据2.1的语法做了更新。

I.

1.通过playground来学习、熟悉swift语言的特性。

2.元组  声明时可以每一个元素有名字,之后可以用.访问

var t=(x:1,y:2,z:"hi")  // var t : (Int,Int,String)
var (a,_,_)=t    // _ 匿名
print(t.0)    // or t.x

3.可选类型 

用法: .toInt 返回可以是nil

强制类型转换不能为可选型。

可选型的解包:  把可选型转化为肯定型,加!号即可。

if let x=name {}也可用来解包

II.

1. Int.min

2.a===b  or  a!==b  类型比较

3.Nil Coalescing Operator    a ?? b  注意空格

4.范围运算符 a...b  a..<b

5.for循环内部,index是常量

III.

1.String返回长度:   .characters.count    好复杂。

2.插值"()"

3.    .hasPrefix     .hasSuffix  

.capitalizedString

.uppercaseString    .lowercaseString

4.trim    

5.split    .componentsSeparatedByString

6.join     .join

7.Range & String.Index

    .rangeOfString    可选类型 som 23..<25

    .startIndex   .endIndex 

    .startIndex.advancedBy(7)
用range取subString、insert、remove、replace等操作
 
IV.
1. Array  只能存一种类型?
声明类型Array<String> or [String]
初始化空表:(1)var array=[Int]()    (2) var array: [Int]=[] 
清空:  array=[]
赋值:  array=[Int](count:10,repeatedValue:8)
操作:  .count  .isEmpty  .append  += [x]  .insert  .removeAtIndex(0)  .removeLast()  .removeRange    array[2...4]=["hi"]  .sort(<)
遍历:(1)for in  (2)0..<array.count  (3)enumerate(array)      
 
2.Dictionary 
声明类型Dictionary <String,Int> or [String:Int]
初始化空表:(1)var dictionary =[Int:String]()    (2) var array: [Int:String]=[] 
清空:  dictionary =[:]
操作:  .count  .isEmpty .updateValue  .removeValueForKey  
dictionary[x]返回的是可选类型(无则返回nil)    删除value就是设为nil
遍历: (1)for(key,value)in    (2) .keys  (3).values                  // 强制类型转换Array(dictionary.keys)   
 
V.
判断不需要加(),代码段必须加{}
1.循环结构
(1)for in   (2)for  ;  ;  ;  (3) while     (4) do while
2.选择结构
(1)if else  (2)switch 不需要写break,相同分支用逗号在同一case里
 switch case:     
(1)case (1,1)、(_,1)、(-2...2,-2...2)、value binding  case (let x,1)
(2)case let(x,y) where x==y:
(3)空则显式break
(4)fallthrough       default
3.break、continue可以指定跳出哪一层循环
mainLoop: for t in m{
    for o in t{
         if o==1{
              break mainLoop;         
        }
    }     
}    

VI.func

1.func sayHi ( name:String?) -> String{}      

(1)返回为空,可不写返回,也可写返回  Void 或 ( )

(2)返回多个返回值,可返回一个元组

(3)传入、返回可以是可选型  (灵活应用 ?、!、??、nil)

2.参数
(1)外部参数名+内部参数名, #name 同时表示内外部参数名 
(2)有默认值的参数必有外参名(默认与内参名相同); 没有默认值的参数必须放在前面,按顺序传入
(3)可变参数类型  Int...    // 使用时args看做array
(4)参数默认是常量参数。变量参数,声明参数名前加var。
(5)传引用,声明参数时参数名前加inout,且调用传参数时参数前加&。(注意和c语言的区别)
3.函数看做变量,类型是( , )-> ( ),如 (Int,Int)-> Int     // 更好的实现逻辑
(1)把函数作为另一个函数的参数,如sorted函数。
(2)函数作为另一个函数的返回值。
(3)函数嵌套。  // 封装
 
VII.闭包      //类似python lambda
 1.基本
{ (parameters) -> returnType in
    statements
} 

2.简化

(1)类型推断

{a,b in return a>b}

(2)一句话可省略return

(3)省略参数名。  {$0> $1}

3.trailing closure   

4.capture values

5.函数和闭包是引用类型。

func calcTotalmiles( todayMiles:Int )-> ()->Int  {
    var totalMiles =0 
    return { totalMiles += todayMiles;    return totalMiles;}
}

var dailyTwoMiles = calcTotalmiles(2)

VIII.枚举

enum Plant{
    case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
} 

1.已知类型,省略写法  .Earth

2.原始值     

enum Planet: Int {
    case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
} 

 Month(rawValue:12) 可选类型

 
 
 
 
 
 
原文地址:https://www.cnblogs.com/aezero/p/4965991.html