zoukankan      html  css  js  c++  java
  • 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)
                }
            }
        }
    }
  • 相关阅读:
    Markdown基本语法
    面向对象
    LeetCode739 每日温度
    LeetCode155 最小栈
    LeetCode279 完全平方数
    LeetCode752 打开转盘锁
    LeetCode622 设计循环队列
    LeetCode200 岛屿的个数
    LeetCode61 旋转链表
    LeetCode138 复制带随机指针的链表
  • 原文地址:https://www.cnblogs.com/k5bg/p/14342690.html
Copyright © 2011-2022 走看看