1 // Playground-noun: a place where people can play
2
3 import UIKit
4 // ----------------------------------------------- ------------------------
5 // 1. String output
6 var str = "world"
7 println ("hello, \ (str)")
8
9 //----------------------------------------------- ------------------------
10 // 2. Type alias
11 typealias audioSample = UInt16
12
13 // ----------------------------------------------- ------------------------
14 // 3. Yuanzu tuples
15 // Definition 1
16 let http404Error = (404, "Not Found")
17 let (statusCode, statusMessage) = http404Error
18 let (justTheStatusCode, _) = http404Error // Only one value
19
20 // Definition 2
21 let http200Status = (statusCode: 200, descirption: "OK")
twenty two
23 // Access mode 1
24 println ("\ (statusCode), \ (statusMessage)")
25
26 // Access mode 2
27 println ("\ (http404Error.0), \ (http404Error.1)")
28
29 // Access mode 3
30 println ("\ (http200Status.statusCode), \ (http200Status.descirption)")
31
32 // ----------------------------------------------- ------------------------
33 // 4 Optionals
34 // Optional value means either a certain value or nothing
35 let possibleNumber = "123"
36 var surveyAnswer: String? // The default is nil
37 let converedNumber = possibleNumber.toInt () // converedNumber is optional
38
39 // Optional value binding
40 if let actualNumber = converedNumber {
41 println ("\ (possibleNumber) has numbers \ (actualNumber)")
42} else {
43 println ("\ (possibleNumber) cannot be converted to integer")
44}
45
46 //4.1 nil
47 // Object-C: nil represents a pointer to a non-existent object
48 // swift: nil means a certain type of value, any optional value type and object type can be set to nil
49
50 //4.2 Implicity Unwrapped Optionals
51 // Unpack each use
52 let possibleString: String? = "An optional string."
53 println (possibleString!)
54
55 // Whether it is used or not, it is automatically unpacked
56 let assumeString: String! = "An implicitly unwrapped optional string."
57 println (assumeString)
58
59 // ------------------------------------------------ ------------------------
60 // 5 Assertions
61 // The assertion checks whether a certain situation is true at run time. If it is true, the code continues to execute, and if it is false, the code is no longer executed
62 let age = 3
63 assert (age> = 0, "age cannot be less than 0")
64
65 // ------------------------------------------------ ------------------------
66 // 6 basic operators
67 //6.1 Range Operators
68 //6.1.1 Closed Range Operator (a ... b), which is mathematically [a, b]
69 for index in 1 ... 5 {
70 println ("\ (index) times is \ (5 * index)")
71}
72 //6.1.2 Half-Closed Range Operator (a..b), namely (a, b)
73 // for index in 1..5 {
74 // println ("\ (index) times is \ (5 * index)")
75 //}
76
77 // ----------------------------------------------- ------------------------
78 // 7. String Literals
79 var emptyString = String ()
80 if emptyString.isEmpty {
81}
82
83 //7.1 escape character
84 / **
85 * \ 0 null character
86 * \\ backslash
87 * \ t horizontal tab
88 * \ n newline
89 * \ r return
90 * \ "double quotes
91 * \ ‘single quote
92 * /
93
94 // 7.2 Strings in swift are value types
95 // When passing a string to a function, or assigning a string to another string, a copy of the string value is passed, not the address or reference of the string. A new copy of the string is created. The newly created copy is passed to the function or other string instead of the original string. This copy is created lazily and will only be created when necessary, so it is very efficient.
96
97 // Note that this is different from NSString in Cocoa, NSString passes reference.
98
99 // 7.3 Traverse Character in String
100 // Each character in the String is a single Unicode character
101 for character in "abcdef" {
102 println (character)
103}
104
105 // 7.4 Count the number of characters in the string countElements
106 let string = "abcdef"
107 println (countElements (string))
108
109 // 7.5 string concatenation
110 let string1 = "hello"
111 let string2 = "world"
112 let character1: Character = "!"
113 let multipier = 3
114
115 let stringPlusString = string1 + "" + string2 + String (character1)
116 let message = "\ (multipier * 2) times \ (stringPlusString)"
117
118 // 7.6 String comparison
119 if string1 == string2 {
120 println ("\ (string1) and \ (string2) contain the same character in the same position")
121}
122
123 // 7.7 hasPrefix, hasSuffix
124 var hasPrefix: Bool = stringPlusString.hasPrefix (string1)
125
126 // 7.8 case conversion
127 let upper = string1.uppercaseString
128 let lower = upper.lowercaseString
129
130 // 7.9 Unicode encoding
131
132 // Unicode is an international encoding, it can represent any character in any language through a standard format. In Unicode, any character can be represented by one or more unicode scalars (scalars: scalar, that is, non-vector, only numeric size, no direction) A unicode scalar is a unique 21-bit number (or name), for example U + 0061 means LOWERCASE LATIN LETTER A ("a")
133
134 // Swift's String and Character are fully compatible with Unicode encoding, it supports a series of different Unicode encoding
135
136 // UTF-8
137 // Get the utf8 representation of the character through the utf8 attribute of the character UTF8View
138 let dog = "Dog!"
139 for codeUnit in dog.utf8 {
140 print (codeUnit)
141}
142 // UTF-16
143 for codeUnit in dog.utf16 {
144 print (codeUnit)
145}
146 // Unicode Scalars
147 // Get the UnicodeScalar representation of the character through the unicodeScalars attribute of the type UnicodeScalarView
148 for scalar in dog.unicodeScalars {
149 print (scalar)
150}
Swift Basic data types