美文网首页
1、Swift 基础

1、Swift 基础

作者: Vergil_wj | 来源:发表于2017-08-15 10:32 被阅读7次
声明常量与变量,分号可加可不加
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

var x = 0.0, y = 0.0, z = 0.0
类型注释
var welcomeMessage: String
welcomeMessage = "Hello"

var red, green, blue: Double
打印
可以直接打印当前常量或变量:
print(friendlyWelcome) 
//"Bonjour"

拼接打印:
print("The current value of friendlyWelcome is \(friendlyWelcome)")
// "The current value of friendlyWelcome is Bonjour!"
注解
单行:
// This is a comment.
多行:
/* This is also a comment
 but is 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. */
整数
swift提供提供的整数有8、16、32、64位,例如Int8、Int32...
整数的范围:
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
Int 与 UInt
在32位平台上,Int就等于Int32;
在64位平台上,Int就等于Int64;
UInt同理;
小数
Double 代表64位浮点数;
Float 代表32位浮点数;
类型推断
整数默认推断为Int:
let meaningOfLife = 42
小数默认推断为Double:
let pi = 3.14159
let anotherPi = 3 + 0.14159
字面量
let decimalInteger = 17
let binaryInteger = 0b10001       // 17 二进制
let octalInteger = 0o21           // 17 八进制
let hexadecimalInteger = 0x11     // 17 16进制

字面量可以加额外的零和下划线,便于读取:
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1
指数
十进制:
1.25e2 = 1.25 x 10^2= 125.0.
1.25e-2 means 1.25 x 10^-2, or 0.0125.
十六进制:
0xFp2 =15 x 2^2= 60.0.
0xFp-2 = 15 x 2^-2= 3.75.
类型转换
小范围往大范围转:
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

小数转Int回直接舍去小数点后表面数值,4.75->4,-3.9->-3:
let integerPi = Int(pi)
// integerPi=3
类型别名
关键字:typealias

相当于给人起了个外号,用法没变:
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min
布尔值
只有true和false两个关键字

声明:
let orangesAreOrange = true
let turnipsAreDelicious = false
使用:
if turnipsAreDelicious {
    print("Mmm, tasty turnips!")
} else {
    print("Eww, turnips are horrible.")
}
// 打印 "Eww, turnips are horrible."

1和0不能用作布尔值判断:
let i = 1
if i {
    // 编译不通过,报错
}

let i = 1
if i == 1 {
    // 正确
}
元组tuples
元祖可以存放多种类型的值:
let http404Error = (404, "Not Found")
取值:
1、
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// "The status code is 404"
print("The status message is \(statusMessage)")
// "The status message is Not Found"

2、如果只想取部分值,其他部分可用下划线忽略掉:
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// "The status code is 404"

3、也可以直接通过索引号来取值:
print("The status code is \(http404Error.0)")
//"The status code is 404"
print("The status message is \(http404Error.1)")
//"The status message is Not Found"
Options
?:代表有值和没有值两种情况
!:有值
异常处理
使用throws抛出异常,放在方法名后面
func canThrowAnError() throws {
}

使用do-catch捕获异常
do {
    try canThrowAnError()
    //没有异常
} catch {
    // 异常处理
}

断言与预处理
断言:assert,只有在debug模式下起作用
预处理:precondition,debug和release都起作用

程序运行到这里会崩溃,并进行提示
let age = -3
assert(age>0, "年龄不能小于0")
或者precondition(age>0, "年龄不能小于0")

相关文章

  • 从 简单汇编基础 到 Swift 不简单的 a + 1

    从 简单汇编基础 到 Swift 不简单的 a + 1从 简单汇编基础 到 Swift 不简单的 a + 1

  • swift之对话框-简单封装

    备注:基础使用请参考swift之对话框-基础使用 1. 新建AlertHelper.swift 2. 调用 (1)...

  • Swift系统视频学习

    1、swift5.1基础语法:https://docs.swift.org/swift-book/Language...

  • Swift基础语法(二)

    swift基础语法(一)swift基础语法二主要讲数据类型 1.字符串 1.在swift中String是一个结构体...

  • Swift教程目录

    1.Swift基础知识

  • Swift-Core Data(1) 基础存储

    Swift-Core Data(1) 基础存储Swift-Core Data(2) 基础存储 最近有做个小 dem...

  • Swift 语言基础

    Swift 语言基础 Swift语言介绍 Swift基础部分 第一个Swift程序 Swift没有main函数,第...

  • Swift基础1

    1.var申明变量,let申明常量,一般不用指明常量是什么类型,系统会自动判定,如果系统不能根据你申明的值做出判断...

  • 1、Swift 基础

    声明常量与变量,分号可加可不加 类型注释 打印 注解 整数 Int 与 UInt 小数 类型推断 字面量 指数 类...

  • Swift基础_1

    Swift 是一门开发 iOS, macOS, watchOS 和 tvOS 应用的新语言。然而,如果你有 C 或...

网友评论

      本文标题:1、Swift 基础

      本文链接:https://www.haomeiwen.com/subject/mfdmrxtx.html