Swift SwiftUI 学习笔记 2024

Swift SwiftUI 学习笔记 2024

一、资源

视频资源 StanfordUnivercity 公开课 2023: https://cs193p.sites.stanford.edu/2023
教程 Swift 初识:基础语法:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/

二、Playground

如何打开 playground?

找到 File -> New -> PlayGround

三、class 与 struct 的区别

  • struct 是值类型的变量,传递的时候传递的是值的复制值
  • class 是引用型变量,传递的时候传递的是它的引用
  • class 需要 init 方法进行初始化, struct 则不需要,默认就对内部的变量进行 init 操作,可以说是免 init
  • struct 内的所有变量都必须有默认值

struct 写法,简写

swift 复制代码
RoundedRectangle(cornerRadius: 20)
RoundedRectangle(cornerRadius: 20).fill()
swift 复制代码
VStack {
    CardView(isFaceUp: true)
    CardView()
    CardView()
    CardView(isFaceUp: true)
}
VStack() {
    CardView(isFaceUp: true)
    CardView()
    CardView()
    CardView(isFaceUp: true)
}
swift 复制代码
VStack{}
.onTapGesture(perform: {
    print("tapped")
})
// ===
VStack{}
.onTapGesture {
    print("tapped")
}

struct 特性

如果想改变 struct 内的变量,如果方法要改变值,需要添加 matable

struct 只有一条指令时,不需要使用 return

四、 一些知识

Bool 有 .toggle 方法

swift 复制代码
func toString(by offset: Int, label: String) -> some View {
	// by 是外部变量名  offset 是内部使用的变量名
}
相关推荐
永日4567030 分钟前
学习日记-day24-6.8
开发语言·学习·php
安和昂33 分钟前
【iOS】 Block再学习
学习·ios·cocoa
pop_xiaoli34 分钟前
OC学习—命名规范
学习·ios
jackson凌35 分钟前
【Java学习笔记】String类(重点)
java·笔记·学习
行云流水剑44 分钟前
【学习记录】在 Ubuntu 中将新硬盘挂载到 /home 目录的完整指南
服务器·学习·ubuntu
vijaycc1 小时前
python学习打卡day47
学习
Camellia03111 小时前
嵌入式学习--江协stm32day5
stm32·嵌入式硬件·学习
a_157153249862 小时前
SpringCloud学习笔记-4
笔记·学习·spring cloud
FserSuN2 小时前
Prompt工程学习之思维树(TOT)
人工智能·学习·prompt
哆啦A梦的口袋呀2 小时前
基于Python学习《Head First设计模式》第九章 迭代器和组合模式
python·学习·设计模式