swift学习笔记-基本内容2

129 阅读5分钟
  • 常量和变量 把名字和特定的类型的值 关联起来。
  • 常量和变量 必须在使用前被声明
//20201024 程序员节日
// 基本类型 - 常量和变量

let s = "S" 
//常量,常量的值一旦设置好便不能再被更改 
//常量的声明:使用关键词 let(来声明常量)
var q =  7  
//变量 , 变量的值可以在将来被设置为不同的值 
//变量的声明:使用关键词 var)


注释

当 Swift 编译器在编译代码的时候会忽略掉你的注释。

// 单行注释

 /* 多行 
    注释        */

整数范围

你可以通过 min 和 max 属性来访问每个整数类型的最小值和最大值:

let minValue = UInt8.min // 最小值是 0, 值的类型是 UInt8
let maxValue = UInt8.max // 最大值是 255, 值得类型是 UInt8
let maxInt = Int.max  //9223372036854775807
let minInt = Int.min  //-9223372036854775807
let maxInt32 = Int32.max //2147483647
let minInt32 = Int32.min  //-2147483647
let maxInt16 = Int16.max  //32767
let minInt16 = Int16.min  //-32767
let maxInt8 = Int8.max    //127
let minInt8 = Int8.min    //-128 

/*
Int8 类型的常量或变量可以存储的数字范围是 -128~127,
而 UInt8 类型的常量或者变量能存储的数字范围是 0~255 。
如果数字超出了常量或者变量可存储的范围,编译的时候就会报错 */
  • Int

在32位平台上, Int 的长度和 Int32 相同。

在64位平台上, Int 的长度和 Int64 相同。

  • UInt

在32位平台上, UInt 长度和 UInt32 长度相同。

在64位平台上, UInt 长度和 UInt64 长度相同。

浮点数

  • Double代表 64 位的浮点数。
  • Float 代表 32 位的浮点数。
  • Double 有至少 15 位数字的精度
  • Float 的精度只有 6 位
  • 具体使用哪种浮点类型取决于你代码需要处理的值范围。在两种类型都可以的情况下,推荐使用 Double 类型。

数值型字面量

整数型字面量可以写作:

  • 一个十进制数,没有前缀
  • 一个二进制数,前缀是 0b
  • 一个八进制数,前缀是 0o
  • 一个十六进制数,前缀是 0x
//下面的这些所有整数字面量的十进制值都是 17  :

let decimalInteger = 17   // 十进制没有前缀
let binaryInteger = 0b10001 // 17 in binary notation 二进制里的17
let octalInteger = 0o21 // 17 in octal notation 八进制中的17
let hexadecimalInteger = 0x11 // 17 in hexadecimal notation 十六进制中的17

十进制数与 exp 的指数,结果就等于基数乘以 10exp:

  • 1.25e2 意味着 1.25 x 102, 或者 125.0 .
  • 1.25e-2 意味着 1.25 x 10-2, 或者 0.0125 .

十六进制数与 exp 指数,结果就等于基数乘以2exp:

  • 0xFp2 意味着 15 x 2 2, 或者 60.0 . 2的2次方
  • 0xFp-2 意味着 15 x 2-2, 或者 3.75 . 2的负2次方

下面的这些浮点字面量的值都是十进制的 12.1875 :

//下面的这些浮点字面量的值都是十进制的 12.1875 :

let decimalDouble = 12.1875  //十进制
let exponentDouble = 1.21875e1   //十进制exp指数,1.21875 x 10
let hexadecimalDouble = 0xC.3p0  //
  • 数值型字面量也可以增加额外的格式使代码更加易读。整数和浮点数都可以添加额外的零或者添加下划线来增加代码的可读性。
//下面的这些格式都不会影响字面量的值

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

布尔值

  • 布尔量被作为逻辑值来引用
  • 布尔量提供了两个常量值, true 和 false 。
let ras = true
let yalgan = false

//bool值, if语句 自动判断为 真假 ,也就是bool值truefalse

let orangesAreOrange = true
let turnipsAreDelicious = false

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

  • Swift 的类型安全机制会阻止你用一个非布尔量的值替换掉 Bool 。下面的栗子中报告了一个发生在编译时的错误:
let i = 1
if i {
    // this example will not compile, and will report an error 此例子不运行并将报错
    //let i 是非布尔量
}

然而,下边的这个例子就是可行的:

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

元组

元组把多个值合并成单一的复合型的值。元组内的值可以是任何类型,而且可以不必是同一类型。

  • 在下面的示例中, (404, "Not Found") 是一个描述了 HTTP 状态代码 的元组。
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")

//(404, "Not Found") 元组把一个 Int  和一个 String  组合起来表示 HTTP 状态代码的两种不同的值:数字和人类可读的描述。他可以被描述为“一个类型为 (Int, String)  的元组”
let http404Error = (404, "Not Found")
let (messege, code) = http404Error
print(messege)  // messege 和 code可以调换位置,code可以取值为 string类型的字符
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
//prints "The status code is 404"
print("The status message is \(statusMessage)")
// prints "The status message is Not Found"
当你分解元组的时候,如果只需要使用其中的一部分数据,不需要的数据可以用下滑线( _ )代替:

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

let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// prints "The status code is 404"
另外一种方法就是利用从零开始的索引数字访问元组中的单独元素:

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

print("The status code is \(http404Error.0)")
// prints "The status code is 404"
print("The status message is \(http404Error.1)")
// prints "The status message is Not Found"
你可以在定义元组的时候给其中的单个元素命名:

let http200Status = (statusCode: 200, description: "OK")
1
let http200Status = (statusCode: 200, description: "OK")
在命名之后,你就可以通过访问名字来获取元素的值了:

print("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
print("The status message is \(http200Status.description)")
// prints "The status message is OK"

print("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
print("The status message is \(http200Status.description)")
// prints "The status message is OK"