标签:
创建一个playGround,然后看一下代码,跟着打,别复制粘贴。
1 // Playground - noun: a place where people can play
2
3 import UIKit
4
5 // HelloWorld
6 var str = "Hello, playground"
7
8 // 常量可以改变值
9 var myVariable = 42
10 myVariable = 50
11 let myConstant = 42
12
13 // 指定数据类型
14 let floatNumber:Float = 4
15
16 // 使用强制转换
17 let lable = "the lable is "
18 let width = 94
19 let labelwidth = lable + String(width)
20
21 // 使用双引号中的\来转换
22 let apples = 3
23 let oranges = 5
24 let appleSummary = "i have \(apples) apples"
25 let orangeSummary = "i have \(oranges) oranges"
26
27 // 自动识别数据类型加斜杠转换
28 let somebody = "Alice"
29 let age = 23.5
30 let hellosomebody = "\(somebody) is \(age)"
31
32 // 数组的初始化以及赋值
33 var shoppingList = ["Alice", "Dylan"]
34 shoppingList[1] = "Petit Alice"
35
36 // 字典的初始化以及赋值
37 var occupations = [
38
39 "male" : "Alice",
40 "female" : "Dylan"
41 ]
42 occupations["male"] = "Alice.Petit"
43
44 // 初始化一个空的数组或者字典
45 let emptyArray = [String]()
46 let emptyDictionary = [String:Float]()
47
48 // 初始化一个自动识别类型的字典或者数组
49 let emptyArray_auto = []
50 let emptyDictionary_auto = [:]
51
52 // 控制流 不能省略大括号
53 let individualScores = [75, 42, 103, 87, 12]
54 var teamScore = 0
55 for score in individualScores {
56 if score > 50 {
57 teamScore += 3
58 } else {
59 teamScore += 1
60 }
61 }
62 teamScore
63
64 // 写到这里 突然发现 变量名称直接打出去 就能打印了
65 let emptyA = ["key": "value"]
66 emptyA
67
68 // 在if条件中 条件必须是bool表达式 输如一个直接score会出错的 看好了 是必须是表达式
69
70 // 使用let来标记
71 var optionalString: String? = "Hello"
72 optionalString == nil
73
74 var optionalName: String? = "John Appleseed"
75 var getting = "Hello"
76 optionalName = nil
77
78 if let name = optionalName {
79 getting = "hello, \(name)"
80 } else {
81 getting = "hello, Alice"
82 }
83
84 // switch
85 let vegetable = "red pepper"
86 switch vegetable {
87 case "celery":
88 let vegetableComment = "add some"
89