Swift 语言基础(1)-The basics
来源:互联网 发布:studio美图软件 编辑:程序博客网 时间:2024/06/11 19:36
常量与变量
letmaximumNumberOfLoginAttempts = 10
varcurrentLoginAttempt = 0
varx = 0.0,y = 0.0,z = 0.0
varwelcomeMessage:String
welcomeMessage= "Hello"
letπ = 3.14159
let你好 = "你好世界"
let □□="dogcow"
varwelcomeMessage:String
welcomeMessage= "Hello"
letπ = 3.14159
let你好 = "你好世界"
let □□="dogcow"
varfriendlyWelcome = "Hello!"
friendlyWelcome= "Bonjour!"
// friendlyWelcome is now "Bonjour!"
friendlyWelcome= "Bonjour!"
// friendlyWelcome is now "Bonjour!"
letlanguageName = "Swift"
languageName= "Swift++"
// this is a compile-time error - languageName cannot be changed
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"
// 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!"
// 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 */
/* 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 */
分号
letcat= "□";println(cat)
// prints "□"
// prints "□"
整型数
letminValue= 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.
// meaningOfLife is inferred to be of type Int
letpi = 3.14159
// pi is inferred to be of type Double
letanotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double
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.
类型安全与类型推理
letmeaningOfLife = 42// meaningOfLife is inferred to be of type Int
letpi = 3.14159
// pi is inferred to be of type Double
letanotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double
数字字面常量
letdecimalInteger = 17
letbinaryInteger = 0b10001 // 17 in binary notation
letoctalInteger = 0o21 // 17 in octal notation
lethexadecimalInteger = 0x11 // 17 in hexadecimal notation
1.25e2means 1.25 × 102, or125.0.
1.25e-2 means 1.25 × 10-2, or 0.0125.
0xFp2means 15 × 22, or60.0.
0xFp-2 means 15 × 2-2, or 3.75.
letdecimalDouble = 12.1875
letexponentDouble = 1.21875e1
lethexadecimalDouble = 0xC.3p0
letpaddedDouble = 000123.456
letoneMillion = 1_000_000
letjustOverOneMillion = 1_000_000.000_000_1
letbinaryInteger = 0b10001 // 17 in binary notation
letoctalInteger = 0o21 // 17 in octal notation
lethexadecimalInteger = 0x11 // 17 in hexadecimal notation
1.25e2means 1.25 × 102, or125.0.
1.25e-2 means 1.25 × 10-2, or 0.0125.
0xFp2means 15 × 22, or60.0.
0xFp-2 means 15 × 2-2, or 3.75.
letdecimalDouble = 12.1875
letexponentDouble = 1.21875e1
lethexadecimalDouble = 0xC.3p0
letpaddedDouble = 000123.456
letoneMillion = 1_000_000
letjustOverOneMillion = 1_000_000.000_000_1
数字类型转换
letcannotBeNegative:UInt8 = -1
// UInt8 cannot store negative numbers, and so this will report an error
lettooBig:Int8 = Int8.max+ 1
// Int8 cannot store a number larger than its maximum value,
// and so this will also report an error
lettwoThousand:UInt16 = 2_000
letone:UInt8 = 1
lettwoThousandAndOne = twoThousand + UInt16(one)
letthree = 3
letpointOneFourOneFiveNine = 0.14159
letpi = Double(three) +pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
letintegerPi = Int(pi)
// integerPi equals 3, and is inferred to be of type Int
varmaxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0
// UInt8 cannot store negative numbers, and so this will report an error
lettooBig:Int8 = Int8.max+ 1
// Int8 cannot store a number larger than its maximum value,
// and so this will also report an error
lettwoThousand:UInt16 = 2_000
letone:UInt8 = 1
lettwoThousandAndOne = twoThousand + UInt16(one)
letthree = 3
letpointOneFourOneFiveNine = 0.14159
letpi = Double(three) +pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
letintegerPi = Int(pi)
// integerPi equals 3, and is inferred to be of type Int
类型别名
typealiasAudioSample = UInt16varmaxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0
布尔型
letorangesAreOrange = true
letturnipsAreDelicious = false
ifturnipsAreDelicious {
println("Mmm, tasty turnips!")
}else {
println("Eww, turnips are horrible.")
}
// prints "Eww, turnips are horrible."
ifturnipsAreDelicious {
println("Mmm, tasty turnips!")
}else {
println("Eww, turnips are horrible.")
}
// prints "Eww, turnips are horrible."
leti = 1
ifi == 1 {
// this example will compile successfully
}
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"
lethttp200Status = (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"
letturnipsAreDelicious = false
ifturnipsAreDelicious {
println("Mmm, tasty turnips!")
}else {
println("Eww, turnips are horrible.")
}
// prints "Eww, turnips are horrible."
ifturnipsAreDelicious {
println("Mmm, tasty turnips!")
}else {
println("Eww, turnips are horrible.")
}
// prints "Eww, turnips are horrible."
leti = 1
ifi == 1 {
// this example will compile successfully
}
元组
lethttp404Error = (404,"Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")
let(statusCode,statusMessage) =http404Error// http404Error is of type (Int, String), and equals (404, "Not Found")
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"
lethttp200Status = (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"
可选类型
letpossibleNumber = "123"
letconvertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"
ifconvertedNumber {
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"
letconvertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"
ifconvertedNumber {
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)
varserverResponseCode:Int? =404
// serverResponseCode contains an actual Int value of 404
serverResponseCode= nil
// serverResponseCode now contains no value
varsurveyAnswer:String?
// surveyAnswer is automatically set to nil
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."
ifassumedString {
println(assumedString)
}
// prints "An implicitly unwrapped optional string."
// serverResponseCode contains an actual Int value of 404
serverResponseCode= nil
// serverResponseCode now contains no value
varsurveyAnswer:String?
// surveyAnswer is automatically set to nil
隐式可选类型
letpossibleString: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."
ifassumedString {
println(assumedString)
}
// prints "An implicitly unwrapped optional string."
断言
letage = -3
assert(age>= 0, "A person's age cannot be less than zero")
// this causes the assertion to trigger, because age is not >= 0
assert(age>= 0, "A person's age cannot be less than zero")
// this causes the assertion to trigger, because age is not >= 0
0 0
- Swift 语言基础(1)-The basics
- swift 1: the basics
- Swift文档 ---The Basics基础部分
- Swift基础部分(The Basics)
- The basics of swift
- Swift语言官方文档翻译(3)-The Basics
- Language Basics:语言基础
- 基础部分(The Basics)
- Swift -1 语言基础
- 1.1 NumPy The Basics(NumPy基础)
- swift3.1(1)The Basics
- The Swift Programming Language--语言指南--基础部分
- The Swift Programming Language--语言指南--基础部分
- The Swift Programming Language--语言指南--基础部分
- Lesson 1: The basics of C(国外网站上的一个c语言教程)
- Apple Swift语言基础入门 —— Swift概览1
- Swift语言 快速基础入门 (1)
- Swift 语言基础算法
- HDU 3308 LCIS(线段树区间合并)
- 杭电1016 同时也是 UVA524
- GitHub上让人吓尿的效果Android
- MarkDown 学习笔记(二)
- Longest Consecutive Sequence
- Swift 语言基础(1)-The basics
- mac上用homebrew安装并启动postgresql数据库
- 全排列和汉诺塔
- C++继承类的类型
- Service之startservice与bindservice
- 用C/C++实现代码的动态修改(SMC)
- 单例设计模式
- CVTE宣讲会之后
- 薁螄羆罿蒄螀蚂芁蒆蒃袁膁