[ios] Swift Basis
2015-08-07 09:51
477 查看
swift
//——————– string—————————
//——————-variable———————–
//——————-tuple元组——————————
//元组: N个任意类型的数据组成(N >= 0)
//———————switch———————
//——————function——————————
//—————外部参数名——————————
//——————–常量参数和变量参数—————————
//常量参数: 参数在函数内不可改变,一般参数默认用let,所以不能改变
//变量参数: 用var改变默认的let就能改变了
//———————-输入/输出参数—————————
//在函数内部改变传进来的参数(与c语言指针原理相似)
//——————– string—————————
// 1. String() // 2. "\()" var age = 10 var hand = 2 var num = 12.444 var num2: Float = 12.0 var floatStr = String(stringInterpolationSegment: num2) var c = "\(num)" //var k = String() var str = "i have " + String(hand) + " hands, \(age) years old" ; println(str)
//——————-variable———————–
//specific the type var num3:Int = 10 let f:Float = 3.14 let f2:Float32 = 3.14 let money = 1 //modulo 8 % 2.5 //loop // a...b = a..< (b+1) = [a,b] // 1...4 = [1,4] for i in 1...4{ println(i) } // if no need i ,use "_"to skip and save time for _ in 1...4{ println("***") } for j in 1..<5{ println(j) } let x = Int8.max let y = Int8.min let ux = UInt8.max let uy = UInt8.min UINT8_MAX UINT16_MAX UINT32_MAX
//——————-tuple元组——————————
//元组: N个任意类型的数据组成(N >= 0)
let position2 = (10.5,20,30,"string") var position = (x : 10.5 , y : 20) // 1. access position.x position.y // 2. access position.0 position.1 // 3. assign // 4. error //position.0 = "str" position.0 = 12 position // 1. specify the tuple type var person : (Int,String) = (23,"tianhang") // 在明确指定元素类型的情况下不能加上元素名称 //下面的语句是错误的: //var p : (Int , String) = (age: 23 , name : "rose") // 2 . use more than one variable to receive tuple data var (i, j) = (10 , 20) var point = (i , j) // user "_" to skip a element var (_ , b) = point
//———————switch———————
// switch , no need break //but there should be a executable code in each case let grade = "B" switch grade{ case "A": println("A") case "B": println("B") //Mandatory for default default: println("C") } let score = 95 switch score/10 { case 9: println("*") case 8, 7, 6: println("*") default: println("*") } // use "..." scope switch score{ case 90...100 : println("A") case 60...89: println("B") default: println("D") } // match tuple by switch // judge where is the point let pt = (1,1) switch pt { case (0,0): println("in origin"); case (_,0): println("in x-axis") case(2,_): println("in y-axis") case(-2...2,-2...2): println("inside the rectangle") default: println("outside the rectangle") } // case , value binding, no need default let pt2 = (0,0) switch pt2{ case (let x ,0): println("\(x)") case (0 ,let y): println("\(y)") case let (x, y): println("\(x),\(y)") } // add where switch pt2{ case (let x ,0): println("\(x)") case (0 ,let y): println("\(y)") case let (x, y) where x == y : println("\(x),\(y)") case let (x, y) where x == -y : println("\(x),\(y)") default: println("") } // fallthrough //执行玩一个case,接着执行下一个case switch pt2{ case (0 ,0): println("") fallthrough case (0 ,1): println("") default: println("") }
//——————function——————————
// no return value func printStr(){} // no parameter return Int func printStr2()->Int{return 1 } // return more than one value , use tuple func getValue()->(Int,Int){return (1,1)} // use the return tiple clverly func find(id:Int)->(name:String ,age:Int ){return ("william",25)} var people = find(12) // use the parameter name (name and age) println("name=\(people.name),age=\(people.age)")
//调用find(12),我们一眼看不出来12是什么意思,所以下文就会用到外部参数明来提醒自己,某个参数是什么意思
//—————外部参数名——————————
// add external parameter name to make the code easily to read func addStudent(name:String,stu_age age:Int,stu_no no:Int){} // notice : if the functon has external parameter name , you must // add the external parameter name when you call the function! addStudent("william", stu_age: 23, stu_no: 12345) // simplify the external parameter name // add # , which means parameter name is also // external parameter name func addStudent2(name:String,#age:Int,#no:Int){} addStudent2("william", age: 23, no: 123455) //default parameter value , notice the automatical // external parameter name ->"age" func addStudent3(name:String,age:Int = 10){} addStudent3("william", age: 30) // use to skip the external parameter name func addStudent4(name:String, _ age:Int = 10){} addStudent4("william", 30)
//——————–常量参数和变量参数—————————
//常量参数: 参数在函数内不可改变,一般参数默认用let,所以不能改变
//变量参数: 用var改变默认的let就能改变了
//func changeNum(num:Int){ num = 4} error func changeNum(var num:Int){num = 4} // correct
//———————-输入/输出参数—————————
//在函数内部改变传进来的参数(与c语言指针原理相似)
func change(inout num : Int){ num = 10} var a = 200 change(&a) a func swap(inout x :Int, inout y:Int){ let temp = x; x = y; y = temp ; } func swap2(inout x :Int, inout y:Int){ x = x*y y = x/y x = x/y } func swap3(inout x :Int, inout y:Int){ x = x+y y = x-y x = x-y } var para1 = 10; var para2 = 20; swap(¶1, ¶2) swap2(¶1, ¶2) swap3(¶1, ¶2) para1 para2 2^1
相关文章推荐
- Apple Swift学习教程
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载
- Swift中的Access Control权限控制介绍
- Swift类型创建之自定义一个类型详解
- Swift教程之控制流详解
- Swift中初始化方法的顺序介绍
- Swift里的值类型与引用类型区别和使用
- Swift调用Objective-C编写的API实例