SwiftUI:Swift语法

前言

 

一.关键字

 


 

   用作声明的关键字: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 in a
{
  ...  
}

 

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可以用在循环多个视图上

struct sonView : View
{
  var body : some View
  {
      Text("a")
  }      
}
    

VStack
{
  ForEach(0 ..< 10){ i in
      sonView()  
  }
}

 

struct sonStruct : Identifiable
{
  var id = UUID()    
}

//必须是结构体数组才能在ForEach里面循环
struct sonView : View
{
    var c : [sonStruct] = [sonStruct]()
    var body : some View
    {
        ForEach(c){  item in
            Text(c.description)
        }
    }    
}


或者

struct sonStruct
{
  var id = UUID()    
}

//必须是结构体数组才能在ForEach里面循环
struct sonView : View
{
    var c : [sonStruct] = [sonStruct]()
    var body : some View
    {
        ForEach(c, id : \.id){  item in
            Text(c.description)
        }
    }    
}

 

六.函数

 


  

  Func  声明函数

 

//SwiftUI 视图写法
var
body: some View { self.k1() }
//函数写法 func makeRow()
-> some View{ Text("hello") }

 

七.结构体

 


 

 

  struct  当在循环结构体或者数组时,要求每个循环的元素都是唯一可标识的Identifiable,这样才能在视图中实时刷新

           

struct a : Identifiable
{
    var id = UUID()
    ...                
}

 

八.类

 


   

  class  用于声明类

       类成员必须初始化或者用init()初始化  

 

struct sonStruct
{
    var a: Bool = true
    var b : String = "1"
}

class fatherClass : ObservableObject
{
   @Published var c : [sonStruct] = [sonStruct]()
    var d : Bool = false
}

struct ContentView: View
{
   @ObservedObject var fatherclass : fatherClass = fatherClass()
    var body: some View {
        
        Button(action :
        {
            fatherclass.c.append(sonStruct(a:false, b : "2"))
        })
        {
            VStack{
                Text(fatherclass.c.count.description)
            }
        }
    }
}

 

posted @ 2021-01-29 00:17  言午丶  阅读(373)  评论(0)    收藏  举报