第1年6月30日 swiftui playgrounds
1.
import SwiftUI import PlaygroundSupport struct ContentView : View { @State var index = 0 var body: some View { VStack{ Text("hello world") .padding() Text("\(self.index)") .padding() Button("tap me"){ self.index = self.index+1 } } } } PlaygroundPage.current.setLiveView(ContentView())
https://www.jianshu.com/p/00eeafbfc163
import UIKit func sortArr(_ nums: [Int]) -> ([Int]) { var nums = nums for i in 0..<nums.count { for j in 0..<nums.count-i-1 { if(nums[j]>nums[j+1]) { let temp = nums[j] nums[j] = nums[j+1] nums[j+1] = temp } } } return nums } class NumWrap { var nums: [Int] = [] init(nums:[Int]) { self.nums = nums } func sort(){ self.nums = sortArr(self.nums) } func toString() -> (String) { var text = "" for i in 0..<nums.count{ text += "\(nums[i])," } return text } } var nums = [2,4,1,5,3] print("排序前:",nums) print("排序后:",sortArr(nums)) let num = NumWrap(nums: nums) print(num.toString()) import SwiftUI import PlaygroundSupport struct ContentView : View { @State var index = 0 @State var text = "" var num: NumWrap init(num:NumWrap) { self.num = num _text = State(initialValue: num.toString()) } var body: some View { VStack(spacing:10){ Text("\(self.text)") .padding() Text("\(self.index)") .padding() Button("sort") { self.num.sort() self.text = "排序后:" self.text += self.num.toString() }.padding() Button("tap me") { self.index = self.index+1 }.padding() } } } var view = ContentView(num:num ) PlaygroundPage.current.setLiveView(view)
初始化@state
https://zhuanlan.zhihu.com/p/266137663