[iOS] Swift introduction

Limited resources:

  • Power limitation
  • CPU limitation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ContentView.swift
import SwfitUI

struct ContentView: View {
var body: some View {
// return can be omitted
return HStack(content: { // return a view with contents horizontally on one layer
ForEach(0..<4, content: { index in // return an array of View, from 0 to 4(not including 4)
ZStack(content: { // return a view with contents on top of each other
RoundedRectangle(cornerRadius: 10.0).fill(Color.white)
RoundedRectangle(corderRadius: 10.0).stroke(lineWidth: 3)
Text("test")
})
})
})
.padding() // will add padding to everything in ZStack
.foregroundColor(Color.orange) // Will change color of everything in ZStack
.font(Font.largeTitle)
}
}
  • return can be omitted
  • If a function only has one argument, it can be omitted, and also the parenthesis can be omitted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct ContentView: View {
var body: some View {
// return can be omitted
return HStack { // return a view with contents horizontally on one layer
ForEach(0..<4) { index in // return an array of View, from 0 to 4(not including 4)
CardView(isFaceUp: false)
}
}
.padding() // will add padding to everything in ZStack
.foregroundColor(Color.orange) // Will change color of everything in ZStack
.font(Font.largeTitle)
}
}

struct CardView: View {
var isFaceUp: Bool;

var body: some View {
ZStack { // return a view with contents on top of each other
if isFaceUp {
RoundedRectangle(cornerRadius: 10.0).fill(Color.white)
RoundedRectangle(corderRadius: 10.0).stroke(lineWidth: 3)
Text("test")
} else {
RoundedRectangle(cornerRadius: 10.0).fill()
}

}
}
}