用作声明的关键字:class deinit enum extension
func import init internal
let operator private protocol
public static struct subscript
typealias var
用作语句的关键字: break case continue default
do else fallthrough for
if in return switch
where while
用作表达和类型的关键字: as dynamicType false is
nil self Self super
true _COLUMN_ _FILE_ _FUNCTION_
_LINE_
在特定上下文中被保留的关键字:
associativity convenience dynamic didSet
final get infix inout
lazy left mutating none
nonmutating optinal override postfix
precedence prefix Protocol required
right set Type unowned
weak willSet
二.基本类型1.布尔值
Bool 布尔值
var a : Bool = true a.toggle()
2.字符串
String 字符串
3.日期
Date 日期
三.复合类型1.数组
四.判断1.是否
var a : Bool = true Image(systemName : self.a ? "a" : "b")
1.For
For 用于函数方法当中的循环
for index in 0...5{ ... //index表示遍历其中的所有数字 } var a : [Int] = [1, 2, 3] for index ina { ... }
2.ForEach
ForEach 用于视图中的循环
//通过下标访问
List
{
ForEach(0 ..< 10)
{
number in
Text("(number)")
}
}
let agents = ["1", "2", "3"] //数组
List
{
ForEach(0 ..< agents.count)
{
Text(self.agents[$0])
}
}
//通过id Identifiable协议
let agents = ["1", "2", "3"]
List
{
ForEach(agents, id : .self)
{
Text($0)
}
}
List
{
ForEach(agents, id : .self)
{
k in
Text(k)
}
}
//数组的id标识符可以是自己,循环不重复
@State var texts:[String] = ["ab","cd","ef"]
ForEach(0..<texts.count,id:.self){ i in
TextField("",text:$texts[i])
}
//ForEach可以用在循环多个视图上 structsonView : View { varbody : some View { Text("a") } } VStack { ForEach(0 ..< 10){ i insonView() } }
structsonStruct : Identifiable { var id =UUID() } //必须是结构体数组才能在ForEach里面循环 structsonView : View { var c : [sonStruct] =[sonStruct]() varbody : some View { ForEach(c){ item inText(c.description) } } } 或者 structsonStruct { var id =UUID() } //必须是结构体数组才能在ForEach里面循环 structsonView : View { var c : [sonStruct] =[sonStruct]() varbody : some View { ForEach(c, id : .id){ item inText(c.description) } } }
Func 声明函数
//SwiftUI 视图写法
varbody: some View { self.k1() }
//函数写法 func makeRow() ->some View{ Text("hello") }
struct 当在循环结构体或者数组时,要求每个循环的元素都是唯一可标识的Identifiable,这样才能在视图中实时刷新
structa : Identifiable { var id =UUID() ... }
class 用于声明类
类成员必须初始化或者用init()初始化
structsonStruct { var a: Bool = true var b : String = "1"} classfatherClass : ObservableObject { @Published var c : [sonStruct] =[sonStruct]() var d : Bool = false} structContentView: View { @ObservedObject var fatherclass : fatherClass =fatherClass() varbody: some View { Button(action : { fatherclass.c.append(sonStruct(a:false, b : "2")) }) { VStack{ Text(fatherclass.c.count.description) } } } }