Featured image of post Swift Notes

Swift Notes

Some notes about Swift & SwiftUI

Some learning notes about UIKit, Swift, SwiftUI and Objective-C.

Basics

  • OOP

Project Structure

1
View -> Viewmodifiers -> Viewmodifiers -> …

@State & @Binding

$ symbol

  • Binding
  • e.g. in actionSheet, using $variable allows actionSheet update its value instead of solely reading its value

@State

  • Creates pointer in memory, not necessarily making variable mutable
  • Views that listen to the @State variable will auto change their presentations according to the variable
1
@State private var restaurantIsFavorites: Bool

@Binding

  • Require caller to pass in a $Variable Normal usage
1
@Binding private var isFavorite: Bool

With Object

1
2
3
4
5
6
7
Struct Restaurant{
    ...
}
.
.
.
@Binding var restaurant: Restaurant

ForEach

The target should contain identifiable “unique” items (String normally considered as not identifiable, since two string might look the same but are used to identify different stuff)

To force foreach to think each the element are identifiable

1
id: \.self

Different ForEach list method

Assume there is a list:

1
RestaurantList = ["","",""]

From 0 to end of list

1
2
3
ForEach(0...RestaurantList.count-1, id:\.self){ index in 

}

For indices

1
2
3
ForEach(RestaurantList.indices, id:\.self){ index in 

}

For all items in the list

1
2
3
ForEach(RestaurantList, id:\.self){ Restaurants in 

}

Normal number for each

1
2
3
ForEach(1...4, id:\.self){ Restaurants in 

}

Remark:

  • Swift uses three dots “…” instead of “..”

Symbols & Icons

Download SF symbols Sample usage:

1
image(systemName: "ICON_NAME")

Factoring out bricks

a. New VIEW struct b. Var some view

Grid

LazyVGrid (Makes as small as possible to fit more) Other stacks (Use up all available spaces)

Other

To take all open space available that no one want

1
spacer()

Aspect ratio

1
aspectRatio(1/3, contentMode:.fit)

Classes and Structures

Class

1
2
3
4
5
6
7
class Plane {
    var brand = "Airbus"
}
var plane1 = Plane()
var plane2 = plane 1
plane1.brand = "Boeing"
print (plan2.brand)

Output -> Boeing

Structure

1
2
3
4
5
6
7
struct Plane {
    var brand = "Airbus"
}
var plane1 = Plane()
var plane2 = plane 1
plane1.brand = "Boeing"
print (plan2.brand)

Output -> Airbus

New struct

Similar to what Java does

1. Create new struct (better in new Swift file)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
struct STRUCT_NAME{
    var name: String
    var type: String
    
    // For passing value to initialize
    init(name: String,type: String){
        self.name = name
        self.type = type
    }
    
    // For initialize with default values
    init(){
        self.init(name:"",type:"")
    }
    
}

2. Initialize

In normal case

1
2
Let STRUE_VARIABLE_DEFAULT = STRUCT_NAME()
Let STRUE_VARIABLE_A = STRUCT_NAME(name:"A", type:"1")

If the struct needs to have $Variable, a list of it will be:

1
2
3
4
@State var LIST_OF_STRUCT = [
    STRUCT_NAME(name:"A", type:"1"),
    STRUCT_NAME(name:"B", type:"2"),
]

Comment

Standard comment

1
//

To create sections in code

1
// Mark: - SECTION_NAME
Licensed under CC BY-NC-SA 4.0
Last updated on Saturday, Jul 2, 2022