Swift学习备忘

  • SwiftUI 变量触发界面刷新

1.关于ObservableObject,Published和EnvironmentObject三个关键字的含义理解

2.两种方法触发界面刷新,方法一,如下

class UserOnboard: ObservableObject {
    @Published var onboardComplete : Bool = false
}

方法二:不带关键字 Published,在属性发生变化是,该属性的类调用objectWillChange.send(),通知界面刷新

 

触发刷新的代码:

struct StartOnboardView: View {
    
    @EnvironmentObject var userOnboard: UserOnboard
    
    var body: some View {
        
        let contentView = StartView()
        let settings = UserSettings()
        
        if userOnboard.onboardComplete {
            return AnyView(contentView.environmentObject(settings))
        } else {
            if UserDefaults.standard.bool(forKey: "Loggedin") {
                settings.loggedIn = true
                return AnyView(TabbarVC())
            } else {
                settings.loggedIn = false
                return AnyView(OnboardingView())
            }
        }
    }
}

相关问题可以参考Stack Overflow:NSManagedObject changes do not trigger objectWillChange,I'm trying to use “objectWillChange.send()” in an protocol extension but it's not working, any idea why?,SwiftUI ObservableObject used as EnvironmentObject odd behaviour

仅以此做个记录,备忘

PS,像这种刚刚(也不叫刚刚)出来的技术和属性,国内很少有说明和解释的博客,估计过几年各种博客搬运,都要泛滥了

 

  • List 在 UIKit 中对应的是 UITableView

  • 闭包相关 

List {
    Text("Static row 1")
    Text("Static row 2")

    ForEach(0..<5) {
        Text("Dynamic row \($0)")
    }

    Text("Static row 3")
    Text("Static row 4")
}

举例说明list的简单用法,顺便引出闭包的相关问题

上面的ForEach 可以理解为简单的闭包

List {
    Text("Static row 1")
    Text("Static row 2")

    ForEach(0..<5) {(index:int)->void in
        Text("Dynamic row \(index)")
    }

    Text("Static row 3")
    Text("Static row 4")
}

Another example:

let array = ["1", "2", "3", "4", "5"]
array.forEach { (element) in
    print(element)
}

闭包表达式的一般格式

{ (parameters) -> (return type) in
    statements
  }

几种简写

/*    原始闭包写法    */

//从数组中筛选指出合适的数据组成新的数组
func getList(score:[Int], con:(Int)->Bool) -> [Int]{
    
    var newScore:[Int] = [Int]()
    for item in score {
        if con(item) {
            newScore.append(item)
        }
    }
    return newScore
}

let newAarray = getList(score: [75,60,95,45,85],  con:{(s:Int)->Bool in return s>80})
print(newAarray)


/*    简写一    */
let newAarray = getList(score: [75,60,95,45,85],  con:{(s:Int) in return s>80})

/*    简写二    */
let newAarray = getList(score: [75,60,95,45,85],  con:{s in return s>80})

/*    简写三    */
let newAarray = getList(score: [75,60,95,45,85],  con:{s in s>80})

/*    简写四    */
let newAarray = getList(score: [75,60,95,45,85],  con:{$0>80})
  • Swift解包

var num: Int?

这是Optional的声明,意思不是"我声明了一个Optional的Int值",而是”我声明了一个Optional类型值,它可能包含一个Int值,也可能什么都不包含”,也就是说实际上我们声明的是Optional类型,而不是声明了一个Int类型!

//if let 解包
func getHeight(_ height: Float?) -> Float? {

 if let unwrapedHeight = height {

 return unwrapedHeight / 100

 }

 return nil

}


//guard let解包
func getHeight(_ height: Float?) -> Float? {

 guard let unwrapedHeight = height else {

 return nil

 }

 return unwrapedHeight / 100

}

 

你可能感兴趣的:(iOS,Swift,SwiftUI)