let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt 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 总结
以上是内存溢出为你收集整理的Swift 语言基础(1)-The basics全部内容,希望文章能够帮你解决Swift 语言基础(1)-The basics所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)