Thi Notes
AboutNotesBlogTopicsToolsReading
About|Sketches |Cooking |Cafe icon Support Thi

Swift / SwiftUI discrete notes

Swift / SwiftUI discrete notes

Anh-Thi Dinh
draft
Swift
MacOS
⚠️
This is a quick & dirty draft, for me only!

References

  • vscode-apple-swift-format
Develop in Swift Tutorials | Apple Developer Documentation
  • Swift Package Index
  • Swift Style Guide | Google
SwiftUI Tutorials | Apple Developer Documentation
Exploring SwiftUI Sample Apps | Apple

XCode notes

  • Trong XCode, khi muốn xem Canvas của một preview trong 1 file nhưng lại mở 1 file khác → pin preview đó lại trước khi mở.
  • Nếu làm việc cùng lúc với VSCode/Cursor thì cần save 2 lần để trong XCode detects thay đổi vcà update Canvas.
  • Go to definition: cmd + click
  • Quickly open a file (like cmd+p in vscode): cmd+shift+o
  • Open a new playground script: XCode → File → New → Playground… → Blank
  • Để có thể xem kiểu của 1 variable: nhấn phím Option + click vào biến đó.
  • Mỗi khi có thay đổi gì thì nhấn build lại: cmd+r. Nếu muốn clean và build lại thì clean với cmd+shift+k. Còn nếu muốn build mà ko mở app → cmd+b.
  • Thay đổi version + build cho 1 app: trong XCode → tên project bên trai → tab general
    • Version là nhãn cho người dùng, Build là số đếm nội bộ để phân biệt các build.
    • 1Version 1.0.0 - Build 1    → Release đầu tiên
      2Version 1.0.0 - Build 2    → Fix bug nhỏ, không đổi version
      3Version 1.0.0 - Build 3    → Fix thêm bug
      4Version 1.1.0 - Build 4    → Thêm tính năng mới, tăng version
      5Version 1.1.0 - Build 5    → Fix bug trong 1.1.0
      6Version 2.0.0 - Build 6    → Major update

Notes

  • Naming convention: MyPlayground.playground
  • Swift là 1 compiled language.
    • 1Code Swift → LLVM compiler → Machine code (binary native)
  • Memory Management - ARC (Automatic Reference Counting) → memory được free ngay khi reference count = 0
  • Use let to make a constant, var to make a variable
Develop in Swift Tutorials | Apple Developer Documentation
  • Different types of scenes: WindowGroup (nhiều cửa sổ của cùng 1 app, mỗi cửa sổ của state và storage riêng), Window (cửa sổ chính của app), DocumentGroup (làm việc với document-based data → ), Settings (trang settings)
  • Hot Reload không hoạt động với Preview nặng (ví dụ trong trường hợp của mình thì result query nặng quá) → phải tạo static mock data nhẹ.
    • → Mở Xcode Canvas (Cmd + Option + Enter hoặc Editor → Canvas)
  • SourceKit error: Type resolution, compilation errors (như "Cannot find type
    'QueryResult'") ← Lỗi trong IDE ví dụ như ko thể go to definition.
    • Tạo buildServer.json → Giúp VSCode's sourcekit-lsp hiểu Xcode project structure
  • SwiftLint: Coding style và conventions (như naming, indentation, code smells)
  • Thay size của canvas cho each preview
    • 1.frame(width: 820, height: 600)
  • Deployment target: version tối thiểu của macos để app có thể hoạt động!