-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathContents.swift
42 lines (26 loc) · 1.17 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//: Playground - noun: a place where people can play
import UIKit
// 有一个默认参数
//func sayHelloTo( name: String , withGreetingWord greeting: String = "Hello" ) -> String{
//
// return "\(greeting), \(name)!"
//}
//
//sayHelloTo("Playground", withGreetingWord: "Hi")
//sayHelloTo("Playground")
// 有多个默认参数
func sayHelloTo( name: String , withGreetingWord greeting: String = "Hello" , punctuation: String = "!") -> String{
return "\(greeting), \(name)\(punctuation)"
}
sayHelloTo("Playground", withGreetingWord: "Hi", punctuation: "!!!")
// 默认参数顺序变更是可以的,但最好和声明序一样
sayHelloTo("Playground", punctuation: "!!!", withGreetingWord: "Hi")
// 最好将默认参数放在最后, 但不是必须
// 第一个参数含有默认参数值
func sayHello( to name: String = "Playground" , withGreetingWord greeting: String = "Hello" , punctuation: String = "!") -> String{
return "\(greeting), \(name)\(punctuation)"
}
sayHello()
sayHello(punctuation: "!!!", withGreetingWord: "Bye", to: "OC")
// print是一个非常好的有两个默认参数的函数
print("Hello", 1, 2, 3, separator:",", terminator:".")