Swift 语言基础(1)-The basics

常量与变量

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

var x = 0.0, y = 0.0, z = 0.0

var welcomeMessage: String
welcomeMessage = "Hello"

 let π = 3.14159
 let 你好 = "你好世界"
 let □□= "dogcow"

 var friendlyWelcome = "Hello!"
 friendlyWelcome = "Bonjour!"
 // friendlyWelcome is now "Bonjour!"

 let languageName = "Swift"
 languageName = "Swift++"
 // this is a compile-time error - languageName cannot be changed

 println(friendlyWelcome)
 // prints "Bonjour!"

 println("This is a string")
 // prints "This is a string"

println("The current value of friendlyWelcome is \(friendlyWelcome)")
 // prints "The current value of friendlyWelcome is Bonjour!"

注释

// this is a comment

/* this is also a comment,
 but written over multiple lines */

/* this is the start of the first multiline comment
/* this is the second, nested multiline comment */
this is the end of the first multiline comment */

分号

  let cat = "□" ; println ( cat )
  // prints "□"

整型数

let minValue = UInt8 . min // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8 . max // maxValue is equal to 255, and is of type UInt8

On a 32-bit platform, Int is the same size as Int32.
On a 64-bit platform, Int is the same size as Int64.
On a 32-bit platform, UInt is the same size as UInt32.
On a 64-bit platform, UInt is the same size as UInt64.

类型安全与类型推理

 let meaningOfLife = 42
 // meaningOfLife is inferred to be of type Int

let pi = 3.14159
 // pi is inferred to be of type Double

 let anotherPi = 3 + 0.14159
 // anotherPi is also inferred to be of type Double

数字字面常量

let decimalInteger = 17
 let binaryInteger = 0b10001 // 17 in binary notation
 let octalInteger = 0o21 // 17 in octal notation
 let hexadecimalInteger = 0x11 // 17 in hexadecimal notation

1.25e2 means 1.25 × 102, or 125.0.
1.25e-2 means 1.25 × 10-2, or 0.0125.

0xFp2 means 15 × 22, or 60.0.
0xFp-2 means 15 × 2-2, or 3.75.

 let decimalDouble = 12.1875
 let exponentDouble = 1.21875e1
 let hexadecimalDouble = 0xC.3p0

 let paddedDouble = 000123.456
 let oneMillion = 1_000_000
 let justOverOneMillion = 1_000_000.000_000_1

数字类型转换

 let cannotBeNegative: UInt8 = -1
 // UInt8 cannot store negative numbers, and so this will report an error
 let tooBig: Int8 = Int8.max + 1
 // Int8 cannot store a number larger than its maximum value,
 // and so this will also report an error


let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double


 let integerPi = Int(pi)
 // integerPi equals 3, and is inferred to be of type Int

类型别名

typealias AudioSample = UInt16

var maxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0

布尔型

let orangesAreOrange = true
 let turnipsAreDelicious = false

 if turnipsAreDelicious {
println("Mmm, tasty turnips!")
 } else {
println("Eww, turnips are horrible.")
 }
// prints "Eww, turnips are horrible."


 if turnipsAreDelicious {
println("Mmm, tasty turnips!")
 } else {
println("Eww, turnips are horrible.")
 }
// prints "Eww, turnips are horrible."


 let i = 1
 if i == 1 {
 // this example will compile successfully
}


元组

 let http404Error = (404, "Not Found")
 // http404Error is of type (Int, String), and equals (404, "Not Found")


let (statusCode, statusMessage) = http404Error
println("The status code is \(statusCode)")
// prints "The status code is 404"
println("The status message is \(statusMessage)")
// prints "The status message is Not Found"


 let (justTheStatusCode, _) = http404Error
 println("The status code is \(justTheStatusCode)")
 // prints "The status code is 404"

 println("The status code is \(http404Error.0)")
 // prints "The status code is 404"
 println("The status message is \(http404Error.1)")
 // prints "The status message is Not Found"


let http200Status = (statusCode: 200, description: "OK")
 println("The status code is \(http200Status.statusCode)")
 // prints "The status code is 200"
 println("The status message is \(http200Status.description)")
 // prints "The status message is OK"


可选类型

 let possibleNumber = "123"
 let convertedNumber = possibleNumber.toInt()
 // convertedNumber is inferred to be of type "Int?", or "optional Int"

 if convertedNumber {
println("\(possibleNumber) has an integer value of \(convertedNumber!)")
 } else {
println("\(possibleNumber) could not be converted to an integer")
 }
 // prints "123 has an integer value of 123"


 if let actualNumber = possibleNumber.toInt() {
println("\(possibleNumber) has an integer value of \(actualNumber)")
 } else {
println("\(possibleNumber) could not be converted to an integer")
 }
// prints "123 has an integer value of 123"


空值(nil)

 var serverResponseCode: Int? = 404
 // serverResponseCode contains an actual Int value of 404
 serverResponseCode = nil
// serverResponseCode now contains no value

 var surveyAnswer: String?
 // surveyAnswer is automatically set to nil

隐式可选类型

 let possibleString: String? = "An optional string."
 println(possibleString!) // requires an exclamation mark to access its value
 // prints "An optional string."

let assumedString: String! = "An implicitly unwrapped optional string."
 println(assumedString) // no exclamation mark is needed to access its value
 // prints "An implicitly unwrapped optional string."


if assumedString {
println(assumedString)
 }
 // prints "An implicitly unwrapped optional string."


断言

 let age = -3
 assert(age >= 0, "A person's age cannot be less than zero")
 // this causes the assertion to trigger, because age is not >= 0





















你可能感兴趣的:(Swift 语言基础(1)-The basics)