import Foundation
func sayhello(name:String) ->String {
let greeting = "hello" + name + "!"
return greeting
}
println(sayhello("anna"))
func minusresult(start: Int,end :Int) ->Int {
return end - start
}
println(minusresult(1,10))
//无返回值
func saygoodbye(name: String) {
println("goodbye,\(name)")
}
println(saygoodbye("dave"))
func count(string: String) -> (vs:Int,cs:Int,os: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":
++consonants
default:
++others
}
}
return (vowels,consonants,others)
}
let total = count("some arbitry")
println("\(total.vs) vowels and \(total.cs) consonants")
/*
func join(s1: String,s2: String,joiner:String) ->String {
return s1 + joiner + s2
}
println(join("hello","world",","))
*/
//每一个都有两个名字 比如 string和s1
func join(string s1:String,tostring s2:String,withjoiner joiner:String) ->String {
return s1 + joiner + s2
}
println(join(string : "hello",tostring : "hi",withjoiner : ","))
//简写
//#作为局部外部参数名
func containcharacter(#string : String,#charactertofind:Character) ->Bool {
for character in string {
if character == charactertofind {
return true
}
}
return false
}
let containasvee = containcharacter(string: "asdada",charactertofind:"a" )
println(containasvee)
func join(string s1:String,string s2:String,withjoiner joiner:String = ",")->String {
return s1 + joiner + s2
}
let str1 = join(string:"hefe",tostring: "dawwq",withjoiner: "-")
println(str1)
//joiner有默认值 所以下面要写上
func join(s1:String,s2:String,joiner: String = " ")->String {
return s1 + joiner + s2
}
let str3 = join("hello","world",joiner:"-")
println(str3)
//可变参数
func mean(numbers:Double...)->Double {
var total:Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
println(mean(1.1,2.2,3.1,4.5))
func mean2(lala: Double...) ->Double {
var total1: Double = 0
for lala2 in lala {
total1 += lala2
}
return total1 / Double(lala.count)
}
println(mean2(2.3,5.4))
/*
func alignright(var string:String,count: Int,pad:Character) ->String {
let amounttopad = count - count(string)
for
*/
let label = "The width is"
let width = 94
let widthLabel = label + String(width)
println(widthLabel)
func swaptwoints(inout a :Int,inout b:Int){
let temp = a
a = b
b = temp
}
var someint = 3
var anotherint = 7
//作为输入输出参数时
swaptwoints(&someint,&anotherint)
println("someint is now \(someint),and anotherint is now \(anotherint)")
/*
let interestingnumbers = [
"prime":[2,3,5,7,11,13],
"fibonacci":[1,1,2,8],
"square":[1,4,9,16,25],
]
var largest = 0
for (kind,numbers) in interestingnumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
println(largest)
var n = 2
while n < 100 {
n = n * 2
}
print(n)
var m = 2
do {
m = m * 2
}while m < 100
print(m)
var firstforloop = 0
for i in 0..<4 {
firstforloop += i
}
println(firstforloop)
//使用..<创建的范围不包含上界,如果想包含的话需要使用...。
func calculateStatistics(scores: [Int]) -> (min: Int,max: Int,sum: Int) {
var min = scores[0]
var max = scores[0]
var sum = 0
if score > max {
max = score
} else if score < min {
min = score
}
sum += score
}
return (min,max,sum)
}
let statistics = calculateStatistics([5,100,9])
print(statistics.sum)
print(statistics.2)
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y
}
returnFifteen()
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
*/
//函数类型:1,定义函数,2,定义函数类型变量或常量3,给函数类型赋值
func addtowints(a : Int,b: Int) -> Int {
return a + b
}
var mathfunction:(Int,Int) ->Int //函数类型
mathfunction = addtowints // 赋值
println("result:\(mathfunction(2,3))")
func sum(a: Int,b :Int ) -> Int {
return a - b
}
var mathfunction2:(Int,Int) ->Int = sum
println(mathfunction2(2,5))
func printmathresult(mathfun:(Int,Int)-> Int,a :Int,b : Int){
println("result:\(mathfun(a,b))")
}
printmathresult(addtowints,1,2)
func stepForward(input: Int) ->Int {
return input + 1
}
func stepbackward(input:Int) ->Int {
return input - 1
}
func choosestepfunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepbackward : stepForward
}
var currentvalue = 3
let movenearertozero = choosestepfunction(currentvalue > 0)
println("movenearertozero:\(movenearertozero)")
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
println(answer)
let names = ["1","2","3"]
for name in names {
println(name)
}
let numberoflegs = ["spider":8,"ant":10,"cat":4]
for (animalname,legs) in numberoflegs{
println("\(animalname) has \(legs)")
}
for character in "hello" {
println(character)
}
for var index=0;index<3 ;++index {
println(index)
}
var va = 1
while va < 5 {
println("while index is \(va)")
va++
}
var vb = 1
do {
println("is \(vb)")
vb++
}while vb < 5
原文链接:https://www.f2er.com/swift/325823.html