-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.swift
62 lines (47 loc) · 1.85 KB
/
example.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// this is a single line comment using two slashes.
// Swift variables are declared with "var"
// this is followed by a name, a type, and a value
var explicitDouble: Double = 70
// If the type is omitted, Swift will infer it from
// the variable's initial value
var implicitInteger = 70
var implicitDouble = 70.0
var 國 = "美國"
// Swift constants are declared with "let"
// followed by a name, a type, and a value
let numberOfBananas: Int = 10
// Like variables, if the type of a constant is omitted,
// Swift will infer it from the constant's value
let numberOfApples = 3
let numberOfOranges = 5
// Values of variables and constants can both be
// interpolated in strings as follows
let appleSummary = "I have \(numberOfApples) apples."
let fruitSummary = "I have \(numberOfApples + numberOfOranges) pieces of fruit."
// In "playgrounds", code can be placed in the global scope
println("Hello, world")
// This is an array variable
var fruits = ["mango", "kiwi", "avocado"]
// Define a dictionary with four items:
// Each item has a person's name and age
let people = ["Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25]
// Functions and methods are both declared with the
// "func" syntax, and the return type is specified with ->
func sayHello(personName: String) -> String {
let greeting = "Hello, " + personName + "!"
return greeting
}
// prints "Hello, Dilan!"
print(sayHello("Dilan"))
// Parameter names can be made external and required
// for calling.
// The external name can be the same as the parameter
// name by prefixing with an octothorpe (#)
// - or it can be defined separately.
func sayAge(#personName: String, personAge age: Int) -> String {
let result = "\(personName) is \(age) years old."
return result
}
sayAge(personName:"Jaden", personAge:19)
// We can also specify the name of the parameter
print(sayAge(personName: "Dilan", personAge: 42))