关于参数类型,在以前的编程过程中,很多时间都忽视了形参与实参的区别。通过这两天的学习,算是捡回了漏掉的知识。
在swift中,参数有形参和实参之分,形参即只能在函数内部调用的参数,默认是不能修改的,如果想要修改就需要在参数前添加var声明。
但这样的声明过后,仍旧不会改变实参的值,这样就要用到inout了,传递给inout的参数类型必须是var类型的,不能是let类型或者字面类型,(字面类型是在swift中常提的一个术语,个人认为就是赋值语句,也不能修改)而且在传递过程中,要用传值符号“&”进行传递。
上面呢,主要是个人在学习过程中的一点概括性的东西。具体的内容,就直接看代码了,而且代码里的注释也是相当清晰地\(^o^)/~
func sayHello(name:String)->String { return "Hello "+name+" !"; } sayHello("wangyi") println(sayHello("wangyi")) //统计元音字母 和辅音字母的数量 func count(string:String)->(vowels: Int,consonants:Int,others:Int){ var vowels=0,consonants=0,others=0 for character in string{ switch String(character).lowercaseString{ case "a","e","i","o","u": vowels++ case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z": consonants++ default: others++ } } return (vowels,consonants,others) } println(count("hello world")) let total=count("some arbitrary string ") println("\(total.vowels) .vowels \(total.consonants) .consonants \(total.others) others") //外部参数名称 //添加外部形参名称后,在调用函数时,阅读起来不至于茫然,知道每个参数代表什么意思 func someFunction(externalParameterName localParamerName :Int) { println("localParamerName:\(localParamerName)"); } someFunction(externalParameterName:5) //外部形参速记 //在形参前添加“#”,即可快速替代外部形参名称,系统会自动补充与本地形参名称相同的外部形参 func constainsCharacter(#string: String,#characterToFind:Character)->Bool { var result=false for character in string { if character == characterToFind{ result = true } else{ result = false } } return result } let constainsAVee=constainsCharacter(string: "aardvark", characterToFind: "k") println("constainsAvee:\(constainsAVee)") //默认形参值 func join(string s1:String, toString s2:String, withJoiner joiner:String=" ")->String { return s1+joiner+s2 } join(string:"hello",toString:"world" ,withJoiner:"-") //当有默认形参值后,在函数调用过程中,如果不给默认形参值赋值,则使用默认形参值 println(join(string: "hello", toString: "world")) //调用时,则显示实参所代表的值 println(join(string: "hello", toString: "world", withJoiner: "--")) //有默认值的形参,系统会自动添加与本地形参名一样的外部形参名 func joinWithOutExterenParamerter(s1:String,s2:String,joiner:String=" ") { //形参默认是常量let型,赋值会导致编译错误 // s1="asdf" println("\(s1)+\(joiner)+\(s2)") } joinWithOutExterenParamerter("hello","world",joiner:"~~~~") //可变形参 //传递至可变形参的值在函数主体内是以适当类型的数组存在的 func arithmeticMean(numbers:Double...)->Double { var total:Double=0 for number in numbers { total+=number } //在函数体内就作为名为 numbers 类型为 Double[]的常量数组 return total/Double(numbers.count ) } println( arithmeticMean(1,2,3,4,5)) //常量形参和变量形参 //形参默认是常量,在做域内修改的话,会导致错误 //如果要对形参的值进行修改,可以将其声明为变量类型 //变量形参是变量,并给函数一个可修改的形参值副本 并不会导致传递过来的形参值发生改变 func alignRight(var string:String,count:Int ,pad:Character)->String { let amountToPad=count-countElements(string) for _ in 1...amountToPad{ string=pad+string } println("string:\(string)") return string } let originalString = "hello" let paddedString=alignRight(originalString,10,"-") //形参的值不会改变 改变的只是副本 println("originString:\(originalString)") println("paddedString:\(paddedString)") //in-out形参 //改变形参后,想在函数调用后继续保持形参值得改变,在变量前添加in-out即可 //实参必须为变量,因为常量和字面量不能修改 //在进行参数传递过程中,需要在实参前添加“&”符号 func swapTwoInts(inout a:Int,inout b:Int) { let temporaryA=a; a=b; b=temporaryA } var someInt=3,anotherInt=5 swapTwoInts(&someInt, &anotherInt) println("someInt:\(someInt) anotherInt:\(anotherInt)")
其实仔细阅读《The Swift Programming Language》一书的同学都知道,都是书上的例子,如果有那块看的还不是太清楚的话,可以继续探讨学习哦\(^o^)/~
令附上demo:http://download.csdn.net/detail/jidiao/7512153
Swift学习笔记(二)参数类型,布布扣,bubuko.com
时间: 2024-10-13 22:32:42