Swift 语言基础(1)-The basics

前端之家收集整理的这篇文章主要介绍了Swift 语言基础(1)-The basics前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

常量与变量

@H_403_4@let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt 0

x 0.0,y z 0.0

welcomeMessage: String
welcomeMessage = "Hello"

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

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

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

println(friendlyWelcome)
// prints "Bonjour!"

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

"The current value of friendlyWelcome is \()"// 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,128)"> Int is the same size as Int64.
On a 32-bit platform,128)"> UInt UInt32 UInt64.

类型安全与类型推理

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

pi // pi is inferred to be of type Double

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

数字字面常量

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

1.25e2 means 1.25 × 102,or 125.0.
1.25e-2 means 1.25 × 10-2,128)"> 0.0125.

0xFp2 means 15 × 22,128)">60.00xFp-2 means 15 × 2-2,128)"> 3.75.

decimalDouble 12.1875
exponentDouble 1.21875e1
hexadecimalDouble 0xC.3p0

paddedDouble 000123.456
oneMillion 1_000_000
justOverOneMillion 1_000_000.000_000_1

数字类型转换

cannotBeNegativeUInt8 = -1
// UInt8 cannot store negative numbers,and so this will report an error
tooBigInt8 = Int8.max + 1
// Int8 cannot store a number larger than its maximum value,18)">// and so this will also report an error


twoThousandUInt16 2_000
onetwoThousandAndOne twoThousand + UInt16)

three 3
pointOneFourOneFiveNine Doublethree) + pointOneFourOneFiveNine
// pi equals 3.14159,and is inferred to be of type Double


integerPi Intpi// integerPi equals 3,and is inferred to be of type Int

类型别名

typealias AudioSample = UInt16

maxAmplitudeFound AudioSamplemin
// maxAmplitudeFound is now 0

布尔型

orangesAreOrange true
turnipsAreDelicIoUs false

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


i == 1 // this example will compile successfully
}


元组

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


let statusCodestatusMessage) = http404Error
"The status code is \(// prints "The status code is 404"
"The status message is // prints "The status message is Not Found"


justTheStatusCode_// prints "The status code is 404"

http404Error.01// prints "The status message is Not Found"

http200Status = (: 200description: "OK")
http200Status// prints "The status code is 200"
// prints "The status message is OK"


可选类型

possibleNumber "123"
convertedNumber possibleNumbertoInt()
// convertedNumber is inferred to be of type "Int?",or "optional Int"


) has an integer value of convertedNumber!)could not be converted to an integer"// prints "123 has an integer value of 123"

if let actualNumber () {
actualNumber// prints "123 has an integer value of 123"


空值(nil)

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

surveyAnswerString?
// surveyAnswer is automatically set to nil

隐式可选类型

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

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


assumedString // prints "An implicitly unwrapped optional string."

断言

age assert>= 0 "A person's age cannot be less than zero"// this causes the assertion to trigger,because age is not >= 0
原文链接:https://www.f2er.com/swift/327473.html

猜你在找的Swift相关文章