第五节:可选类型 optional

 //: Playground - noun: a place where people can play

 import UIKit

 /*
Swift学习第五节
可选类型 optional(可能是任何值) */
let possibleNumber = ""
var result = Int(possibleNumber)
print(result)
result.dynamicType
//上面就是一个可选类型的举例,possibleNumber开始是一个string,然后强转为Int,这里可能失败(可能转为Int,也可能是nil). var x:Int? //定义一个可选类型x,可能Int也可能其他.默认为nil
var address:String?
var successRate:Double? result = nil //可以为一个可选类型赋值为nil,但是普通类型不可以 //访问可选类型
if result != nil{
print(result!)
}
//print(x!) error;可选类型不能直接输出,必须先行判断 if let num = result{
print(result)
}else{
print("num为nil")

第六节:函数 函数参数

 //: Playground - noun: a place where people can play

 import UIKit
/*
swift学习第六节
函数 函数参数
func 方法名(参数名:参数类型){
方法体
}
*/
//写一个方法
func printName(){
print("my name is 少停")
}
printName()//调用 func ten(num:Int){
print("\(num * 10)")
}
ten() func mult(num1:Int,num2:Int){
print("\(num1*num2)")
}
mult(, num2: ) //num2 为外部参数名 func create(num3:Int,_ num4:Int){
print("\(num3*num4)") // _ 使用_代替外部参数名简化
}
create(, ) func make(NUM1 num1:Int,NUM2 num2:Int){
print("\(num1*num2)")
} //NUM1 NUM2 外部参数名,外部参数名只是方便理解
make(NUM1: , NUM2: ) func make1(NUM1 num1:Int = ,NUM2 num2:Int = ){ //为参数赋初始化
print("\(num1*num2)")
}
make1() func arySum(number:Double ...){//参数为Double类型的number,可以有很多个
var sum: Double =
for i in number{
sum += i
}
print("\(sum)")
}
arySum(,,,,)
arySum(,) //func sum(value : Int){
// value++
//} error:swift中函数参数分为常量参数和变量参数,函数参数默认是常量参数,不能修改. func sum(var value:Int){
value++;
}
var m = ;
sum(m);
m
//在函数内对一个参数进行修改后,在函数外部仍然不会改变 func summ(inout value:Int){//想在函数内修改参数后,在外部仍然有效需要使用inout
value++
}
var n =
summ(&n)
n

第七节:函数 函数返回值和类型

 //: Playground - noun: a place where people can play

 import UIKit

 /*
swift学习第七节
函数 函数返回值和类型 */
func sum(num1:Int,num2:Int) -> Int{ //返回一个返回值
return num1*num2;
}
var m = sum(, num2: ) func num(num1:Int,num2:Int) -> (Int,Int){ //返回两个返回值
return (num1,num2)
}
var table = num(, num2: )
table.
table. func num2(num1:Int,num2:Int) -> (n1:Int,n2:Int){ //返回两个返回值,且为返回值赋名
return (num1,num2)
}
var nn = num2(, num2: )
nn.n1
nn.n2 func string2Int(str:String) -> Int?{
return Int(str)
}
var n = string2Int("")
n.dynamicType var f1:(Int,Int) -> Int = sum
var f2:(Int,Int) -> (Int,Int) = num
var f3:String -> Int? = string2Int
//----------------------------------------------------------------
//把函数作为参数使用:execute为一个函数,这个函数有两个参数,一个参数是一个函数(参数为string,返回值是int),另外一个参数是string
func execute(fn:(String) -> Int?,fnParam:String){
fn(fnParam)
}
execute(f3, fnParam: "") //---------------------------------------------------------------- //func increment(n:Int) -> Int{
// return n+1
//}
//func decrement(n:Int) -> Int{
// return n-1
//}
////whichOne函数:有一个参数n,返回值是一个函数(参数是int,返回值也是int)
////func whichOne(n:Bool) -> ((Int) -> Int){
//// return n ? increment : decrement
////}
//typealias op = (Int) -> Int
//func whichOne(n:Bool) -> op{
// return n ? increment : decrement
//}
//var one = 1
//var one2Ten = whichOne(one < 10)
//while one < 10 {
// one = one2Ten(one)
//}
//上面的代码可以使用内嵌函数增强可读性
typealias op = (Int) -> Int
func whichOne(n:Bool) -> op{
func increment(n:Int) -> Int{
return n+
}
func decrement(n:Int) -> Int{
return n-
} return n ? increment : decrement
}
var one =
var one2Ten = whichOne(one < )
while one < {
one = one2Ten(one)
}

第八节:Closures

 //: Playground - noun: a place where people can play

 import UIKit
/*
swift学习第八节
Closures */
let ten =
var addClosure: (Int,Int) -> Int = {(a:Int,b:Int) -> Int in
return a + b
}
addClosure(,)
addClosure = { a, b in return a + b}
addClosure = {a, b in a + b} //Single expression closure
addClosure = {$ + $}

源码下载:http://download.csdn.net/detail/shaoting19910730/9463646

https://github.com/pheromone/swift2

swift系统学习第二章的更多相关文章

  1. swift系统学习第一章

    第一节:变量,常量,类型推断,字符,字符串 //swift学习第一节 /* 变量 常量 类型推断 字符 字符串 */ import UIKit //变量 var str = "swift&q ...

  2. oracle学习 第二章 限制性查询和数据的排序 ——03

    这里.我们接着上一小节2.6留下的问题:假设要查询的字符串中含有"_"或"%".又该如何处理呢? 開始今天的学习. 2.7  怎样使用转义(escape)操作符 ...

  3. Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触

    第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...

  4. Java基础知识二次学习-- 第二章 基础语法与递归补充

    第二章 基础语法与递归补充   时间:2017年4月24日10:39:18 章节:02章_01节,02章_02节 视频长度:49:21 + 15:45 内容:标识符,关键字与数据类型 心得:由字母,下 ...

  5. Asp.Net MVC4 + Oracle + EasyUI 学习 第二章

    Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...

  6. Ruby学习-第二章

    第二章 类继承,属性,类变量 1.如何声明一个子类 class Treasure < Thing 这样Thing类中的属性name,description都被Treasure继承 2.以下三种方 ...

  7. C#高级编程 (第六版) 学习 第二章:C#基础

    第二章 基础 1,helloworld示例: helloworld.cs using System; using System.Collections.Generic; using System.Li ...

  8. swift系统学习第三章

    第九节:结构体-sturt //: Playground - noun: a place where people can play import UIKit /* swift学习第九节 结构体:st ...

  9. Struts2框架学习第二章——Struts2下的HelloWorld

    本章要点 —  Struts 2的下载和安装 — 纯手工创建一个Web应用 — 纯手工创建一个Struts 2应用 — 实现Struts 2的Action — 配置Struts 2的Action — ...

随机推荐

  1. python中的popen和subprocess

    import os from subprocess import Popen, PIPE res = os.popen('xx.exe E:\\test\\file1 E:\\test\\file2' ...

  2. android 模拟器 使用键盘的配置

    1 打开 android Manageer , 克隆一个设备 2.

  3. JS回调函数(callback)

    在使用Jquery的时候,用到Callback(),回调函数的概念.而且很多. 比如: $.ajax({ url:"test.json", type: "GET" ...

  4. DB、ETL、DW、OLAP、DM、BI关系结构图

    DB.ETL.DW.OLAP.DM.BI关系结构图 在此大概用口水话简单叙述一下他们几个概念: (1)DB/Database/数据库——这里一般指的就是OLTP数据库,在线事物数据库,用来支持生产的, ...

  5. Metro UI(Win 8风格)页面设计小记

    一.Metro风格菜单——简单 HTML <div class="pagina "> <div class="linha"> <d ...

  6. 利用VBoxManage对虚拟机格式vdi、vmdk、vhd进行互转

      虚拟机顾名思义就是虚拟出来的机器(virtual machine),虚拟化技术也是时下IT界最热门的技术,因其能更加有效利用硬件资源,整合IT应用,降低TCO,节能环保等,说白了就是一台硬件上够强 ...

  7. 3大主流NoSQL数据库性能对比测试报告

    近日,知名独立基准测评机构Bankmark,针对目前市面上主流的NoSQL数据库SequoiaDB.MongoDB以及Cassandra三款NoSQL数据库产品做了性能对比测试并发布测试报告.在所有的 ...

  8. php连接ftp的研究,自带ftp函数 | fsockopen | curl实现ftp的连接

    持续更新中..............

  9. [Js]面向对象的选项卡实例

    中间过渡环节:把面向过程的程序,改写成面向对象的形式 <html xmlns="http://www.w3.org/1999/xhtml"><head>&l ...

  10. flash builder的编译缓存

    C:\Users\Administrator\AppData\Roaming 因为我的一个项目是手机.浏览器都支持的项目,所以我经常删除项目然后修改成别的类型: 可能是这个原因,导致我的程序或者加载的 ...