开始学习swift和swiftUI,记录一下自己的学习。
由于本人性格不太耐烦做详细的笔记, 基本上只记录自己认为自己需要提炼的重点, 所以观感会比较差, 不太适合0基础没有学过其他语言的同学
第一篇主要是基础的变量,基础数据类型,函数写法,自定义错误处理,闭包写法的记录
闭包第一次接触,是真的觉得难呀,主要是各种变体太多了
变量:var和let,就是动态变量和静态变量,静态变量只能赋值一次
let定义后可以分开赋值
Let a:String
….
A = [,,,,,,]
数据类型
字符串 String 方法:.count .uppercased .hasPrefix("x") .hasSuffix("x") .isEmpty
数字 Int Double 方法 .isMultiple(of:2) 不同型数字不能直接对比 比如 Double(0.1)<Int(1)是无法比较的
布尔型的方法 .toggle()
强制基本型的初始化 let a:String let b:Int let c:[String]
数组 Array
初始化 a= Array<Int>() B=[String]()
方法 .count . Append("s") .removeAll() remove(at:1) .contains("b") .sorted() .reversed()
cahracter.removeAll(keepingCapacity: true)//保留空间只删除数据
let stringLucknum = luckNumber.map{"luck number - ($0)"} //映射,相当于对每一个值做操作后返回
let evenLuckynum = luckNumber.filter{$0%2==0}//过滤掉条件为 false 的后返回
Set
Let a = Set(["a","b","c","d"]) lookup很快
方法.sorted() .insert("e")
Enum
初始化 和switch里的使用
Enum Week{
case a,b,c,d,e}
Let a = Week.a
Switch a
{
case: .a:
{
Print(…)
}
case: .b
{
Print(…)
}
}
?符封包
var h:String? = "a" //封包,以免没有赋值就使用的情况下error
print(h) // 自动解包,会有警告
print(h!)//手动解包,没有警告
在 if 的条件里直接赋值,可以同时判断是否成功来处理 error
if let firstNumber = Int("4"), let secondNumber = Int("sdf"), firstNumber < secondNumber {
print("(firstNumber) < (secondNumber)")
}
else
{
\do error process
}
range 的用法
for i in 1...12{
print("i = (i)\0")
}
for i in 1..<12{
print("i = (i)\0")
}
let id = Int.random(in: 1...100)
函数的写法
let str11 = "sdfsdf2"
let str22 = "sd2fsdf"
func the_same (str1:String,str2:String)-> Bool{
if str1.sorted() == str2.sorted(){
return true
}
else{
return false
}
}
the_same(str1: str11, str2: str22)
func isUppper(_ string: String,_ num: Int) -> Bool{ // _表示调用时无需显式声明
string == string.uppercased()
}
func printTable(for number: Int){//参数可以设置两个名字,第一个给外面调用用,第二个给内部用,以做到自然语言般的逻辑
for i in 1...number{
print(i)
}
}
printTable(for: 5)
自定义错误
enum PasswordError: Error{
case short, obvious
}
func checkPassword(_ password: String) throws -> String //throws 必须有
{
if password.count < 5 { throw PasswordError.short} // 定义具体每一个 throw
if password == "12345"{ throw PasswordError.obvious}
if password.count < 8 {
return "OK"
}
else if password.count < 10{
return "Good"
}
else
{
return "Excellent"
}
}
try checkPassword("12345") //直接 try,但出错会直接终止程序
do{ //do try catch 结构来具体定义处理错误发生时的逻辑,可以不终止程序
let result = try checkPassword("12345")
print("passwd is (result)")
}catch PasswordError.short{
print("too short")
}
catch PasswordError.obvious{
print("too easy")
}
catch{ //else
print(error.localizedDescription)
}
闭包赋值为函数
func getUser() -> (first:String,last:String){
(first:"Tay",last:"swfit")
}
let copyUser() -> Void = getUser
闭包定义
let sayHello = { (name:String)-> String in //in 用来分割参数和主体
return "Hello (name)"
}
sayHello("Jay")
let copyhello: (String) -> String = sayHello //闭包赋值闭包
copyhello("Kate")
let team = ["Go","Su","Pp","Tifa"]
func firstSorted(name1:String, name2:String) -> Bool{
if name1 == "Su"{
return true
}
else if name2 == "Su"{
return false
}
else
{
return name1 < name2
}
}
print(team.sorted(by: firstSorted)) //闭包传入作为参数
闭包定义参数的省略
//trailing closure
//remove by and parenthess, and the data type
let sortedTeam = team.sorted{ name1, name2 in //swift 可以猜测的类型定义,包括小括号,不需要显式写出来
if name1 == "Su"{
return true
}
else if name2 == "Su"{
return false
}
else
{
return name1 < name2
}
}
//more
let sortedTeam2 = team.sorted{ //name1, name2 in //甚至不需要参数定义部分,使用$0,$1 来代替各个参数
if $0 == "Su"{
return true
}
else if $1 == "Su"{
return false
}
else
{
return $0 < $1
}
}
//more common used in smaller code
let reverseTeam = team.sorted{
$0 > $1
}
let tOnly = team.filter{$0.hasPrefix("T")} //$0在调用时的用法
print(tOnly)
尾随闭包
func doimport(first:()->Void,second:()->Void,third:()->Void){
print("to First")
first()
print("to second")
second()
print("to third")
third()
}
doimport(first:{print("do first")},
second:{print("do second")})
{print("do third")} //last closuere //最后一个闭包可以直接省略
doimport{print("do first")} //last closure //虽然叫做尾随闭包,并不一定要在书写顺序上是末尾的才能用?
second:{print("do second")}
third:{print("do third")}