我的Swift开发

Swift5.x-基础语法笔记

2021-12-16  本文已影响0人  冰棍儿好烫嘴

Swift是Apple在2014年6月WWDC发布的全新编程语言,中文名和LOGO是“雨燕”
Swift从1.x发展到5.x版本,经历多次重大改变,ABI终于稳定。
API(Application Programming Interface):应用程序编程接口即源代码和库之间的接口
ABI(Application Binary Interface):应用程序二进制接口即应用程序与操作系统之间的底层接口,涉及的内容有:目标文件格式、数据类型的大小/布局/对齐、函数调用约定等等。

随着ABI的稳定,Swift语法基本不会再有太大的变动

//:  开始markup
/*:
    开始markup
*/
开启markup渲染效果:Editor -> Show Rendered Markup,注意:Markup只在Playground中有效

Markup语法:

//: [上一页](@previous)
//: [下一页](@next)
/*:
  #一级标题

  ##无序列表
  - First Item
  - Second Item

  ##有序列表
  1. First Item
  2. Second Item

  ##笔记
  > This is a note
  --- 
  
  ##图片
  ![Logo](logo.png "Local image")
  
  ##链接
  *[文字描述](链接地址)

  ##粗体/斜体
  这是**Bold**,这是*Italic*

注释

//单行注释
/*
  多行注释
*/
/*
  1、
  /*多行注释的嵌套*/
*/

常量

let num = 10 //编译时确定常量值
var a = 10
a += 10
a+= 20
let num1 = a
print(num1)//运行时确定常量值

声明时如果不赋值,必须指定类型,不指定类型会报错,如下

let name:String;
name = "ceshi"

或者

let name = "ceshi"
let name:String = "ceshi"

常量、变量在初始化之前,都不能使用

常量变量未初始化

标识符

常见数据类型

常见数据类型

字面量

//布尔
let bool = true//取反是false
//字符串
let string = "测试数据"
//字符(可存储ASCII字符、Unicode字符)
let character: Character =  "a"//必须写Character才会被认为是字符,否则被认为是字符串,因为用的都是双引号
//整数
let intDecimal = 17  //十进制
let intBinary = 0b10001  //二进制
let intOctal = 0o21  //八进制
let intHexadecimal = 0x11  //十六进制
//浮点数
let doubleDecimal = 125.0 //十进制,125.0等价于1.25e2(代表1.25*10的2次方);0.0125等价于1.25e-2(代表1.25*10的-2次方)
let doubleHexadecimal1 = 0xFp2  //十六进制,意味着15*2^2(15乘以2的平方),相当于十进制的60.0
let doubleHexadecimal2 = 0xFp2-2  //十六进制,意味着15*2^-2(15乘以2的-2次方),相当于十进制的3.75
//以下都是表示12.1875
//十进制:12.1875、1.21875e1
//十六进制:0xC.3p0

//字典
let dictionary = ["age" : 18,"height" : 168]

类型转换

//整数转换
let int1:UInt16 = 2_000;
let int:UInt8 = 1
let int3 = int1 + UInt16(int2)
//整数、浮点数转换
let int = 3
let double = 0.14159
let pi = Double(int) + double
let intPi = Int(pi)

//字面量可以直接相加,因为数字字面量本身没有明确的类型
let result = 3 + 0.14159

元组

let http404Error = (404,"Not Found")
http404Error.0//下标取值
http404Error.1

let (statusCode, statusMessage) = http404Error

let (justTheStatusCode,_) = http404Error //不想取值用_忽略

let http200Status = (statusCode: 200, description:"ok")
http200Status. statusCode//访问

if-else语法

while语法

var num = 5
while num>0{
    print("num is \(num)")
    num -= 1 
}//打印了5次
var num = -1
repeat {
  print("num is \(num)")
}while num>0//打印了一次

for语法

let strings = ["ab","cd","ef","gh"]
for i in 0...3{
  print(names[I])
} 

i默认是let,有需要时可以声明为var,如下
for var i in 0...3{
  i += 5
  print(i)//6 7 8
} 
 如果花括号里边用不到i,则用_代替即可
for _  in 1...3{
  print("打印")
} 
for i in 1..<5{
  print(i)//打印结果1,2,3,4
}
let names = ["anda","bob","Tina"]
for name in names[0...2]{
  print(name)
}
let names = ["anda","bob","Tina"]
for name in names[1...]{
  print(name)//bob Tina(数组下标无穷大到数组最后一个下标结束)
}
for name in names[...1]{
  print(name)//anda bob (数组下标无穷小到数组第一个下标开始)
}
for name in names[..<1]{
  print(name)//anda  
}
let range = ...5
range.contains(7)//false
range.contains(4)//true
range.contains(-3)//true
let range1:ClosedRange = 1...3
let range2:Range<Int> = 1..<3
let range3:PartialRangeThrough<Int> = ...5
let stringRange1 = "cc"..."ff"
//"cc"..."ff",包含cc,cd,ce,cf,cg.....cz;da,db....ff
stringRange1.contains("cb")//false
stringRange1.contains("dz")//true
stringRange1.contains("fg")//false
let stringRange2 = "a"..."f"
stringRange2.contains("d")//true
stringRange2.contains("h")//false
//  \0到~囊括了所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G")//true
let hours = 11
let hoursInterval = 2
//tickMark的取值,从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: hours, by: hoursInterval) {
    print(tickMark)//4,6,8,10
}

switch

var num = 1
switch num {
case 1:
    print("num = 1")
    break
case 2:
    print("num = 2")
    break
default:
    print("default")
    break
}//num = 1
var num = 1
switch num {
case 1:
    print("num = 1")
case 2:
    print("num = 2")
default:
    print("default")
}//num = 1

fallthrough

var num = 1
switch num {
case 1:
    print("num = 1")
    fallthrough
case 2:
    print("num = 2")
default:
    print("default")
}//num = 1 num = 2,如果还想继续贯穿到default,在case 2后边继续加上fallthrough
var num = 1
switch num {
case 1:
    print("num = 1")
case 2:
    print("num = 2")
default:
   break
}
enum Answer {case right,wrong}
let answer = Answer.right
switch answer {
case Answer.right:
    print("对")
case Answer.wrong:
    print("错")
}
**由于已确定answer是Answer类型,因此可省略Answer**
switch answer {
case .right:
    print("对")
case .wrong:
    print("错")
}
let string = "jack"
复合条件第一种写法
switch string {
case "jack":
    fallthrough
case "rose":
    print("rose")
default:
    break
}
复合条件第二种写法
switch string {
case "jack","rose":
    print("rose")
default:
    break
}

字符类型也支持
let character:Character = "a"
switch character {
case "a","A":
    print("字符a")
default:
    print("不是字符a")
}

区间匹配、元组匹配

区间匹配
let count = 62
switch count {
case 0:
    print("none")
case 1..<5:
    print("1到5之间")
case 5..<12:
    print("5到12之间")
default:
    break
}
元组匹配
let point = (1,1)
switch point {
case (0,0):
    print("0,0")
case (_,0):
    print("_,0")
case (0,_):
    print("0,_")
case (-2...2,-2...2):
    print("-2...2,-2...2")
default:
    print("default")
}//打印-2...2,-2...2

值绑定

let point = (2,0)
switch point {
case (let x,0):
    print("this is x =  \(x)")
case (0,let y):
    print("this is y =  \(y)")
case let(x,y):
    print("this is x--y =  \(x)--\(y)")
}//this is x  = 2

where

let point = (1,-1)
switch point {
case let(x,y) where x == y:
    print("x==y")
case let(x,y) where x == -y:
    print("x==-y")
case let(x,y):
    print("\(x)-\(y)x!=y && x!=-y")
}
var numbers = [10,20,-10,-20,30,-30]
var sum = 0
for num in numbers where num>0 {//使用where来过滤num
    sum += num
}
print(sum)//60

标签语句

outer: for i in 1...4{
    for k in 1...4 {
        if k==3 {
            continue outer
        }
        if i==3 {
            break outer
        }
        print("i == \(i),k == \(k)")
    }
} //i == 1,k == 1;i == 1,k == 2;i == 2,k == 1;i == 2,k == 2
上一篇 下一篇

猜你喜欢

热点阅读