Khoa Pham
Khoa Pham

Ohayo

Swift Discovery

Discover all the tech

Featured

My year in review 2020

Issue #715

I remember this time last year in December 2019, I spent almost every single bit of my free time on Puma because I want a Swift friendly version of fastlane that suits my need and leverages Swift 5 features.

Here’s my review of my …

How to press and hold button in SwiftUI

Issue #929

We can use ButtonStyleConfiguration to detect isPressed state

struct RecordButton: View {
    var body: some View {
        Button {
        
        } label: {
            Image(systemSymbol: .micFill)
        }
        .buttonStyle( …

How to copy text to the clipboard in Swift

Issue #928

For iOS, use string

Setting this property replaces all current items in the pasteboard with the new item. If the first item has no value of the indicated type, nil is returned.

let pasteboard = UIPasteboard.general
pasteboard.string = …

How to add launch screen in SwiftUI project

Issue #927

For a plain SwiftUI project, Xcode will generate a Info.plist file once you start to edit the Info tab in target settings.

There is a Launch screen (UILaunchScreen) key by default. It has a UILaunchScreen which we don’t need to so we …

How to create UserDefaults property wrapper in Swift

Issue #926

Like AppStorage, we can make a custom UserDefaults property wrapper that conforms to DynamicProperty

@propertyWrapper
public struct UserDefault<Value>: DynamicProperty {
    @State private var value: Value

    let key: String …

How to encrypt using CryptoKit in Swift

Issue #925

Use AES.GCM method with 128 bits key

import CryptoKit

public extension Optional {
    func tryUnwrap() throws -> Wrapped {
        if let value = self {
            return value
        } else {
            throw NSError(domain: …

How to use NavigationSplitView and NavigationStack in SwiftUI

Issue #924

Note

  • Navigation state needs to be in the container of NavigationSplitView for changes to propagate
  • Need to use WindowGroup for navigation bar to work

NavigationSplitView

the navigation split view coordinates with the List in its first …

How to style NavigationLink in macOS

Issue #923

NavigationLink on Mac applies the default button style. We can style it using ButtonStyle, here to use plain style we can just

NavigationLink(value: DetailRoute.books) {
    BooksView()
}
.buttonStyle(.plain)

SwiftUI EnvironmentValues

Issue #922

EnvironmentValues Views in SwiftUI can react to configuration information that they read from the environment using an Environment property wrapper

Updated for iOS 17

SwiftUI Environment values

How to make TextField Stepper in SwiftUI

Issue #921

Use HStack with TextField and a little extension

extension Binding where Value == Int {
    var toString: Binding<String> {
        Binding<String>(
            get: {
                "\(wrappedValue)"
            }, …

How to clear TextEditor background in SwiftUI

Issue #920

For iOS 16 and macOS 13.0

TextEditor(text: $text)
    .scrollContentBackground(.hidden)

For below, use [SwiftUI-Introspect](https://github.com/siteline/SwiftUI-Introspect)

TextEditor(text: $text)
    .instrospectTextView {
        $0. …

Learning Metal for SwiftUI

Issue #919

WWDC23 introduces lots of new additions to SwiftUI, notably Metal shader support with these modifiers

  • colorEffect: Returns a new view that applies shader to self as a filter effect on the color of each pixel.
  • layerEffect: Returns a new …

WWDC23 SwiftUI Q&A

Issue #918

Interesting SwiftUI Q&A during WWDC23

Observable vs ObservableObject

Q: With the new SwiftUI @Observable macro, are there any cases where ObservableObject would still be a better alternative?

A: Use ObservableObject when you need to …

WWDC21 SwiftUI Q&A

Issue #917

Interesting SwiftUI Q&A during WWDC21

@StateObject or ObservedObject?

Q: Let’s say I have a purely SwiftUI flow. I have a ListView with a @StateObject var listFetcher, that makes requests for a list of items. Tapping on an item in the …

What's new in SwiftUI iOS 17 at WWDC23

Issue #916

WWDC23 brings new additions to SwiftUI

Screenshot 2023-06-07 at 22 02 36

Scrolling

The scroll transition modifier is very similar to the visual effect modifier Curt used earlier for the welcome screen. It lets you apply effects to items in your scroll view.

I’m …

Recommended iOS articles to read

Issue #915

Here are my favorite iOS articles

How to render markdown view with React

Issue #914

Use react-markdown to parse markdown content, react-syntax-highlighter to highlight code, and rehype-raw to parse raw html

import ReactMarkdown from "react-markdown"
import { Prism as SyntaxHighlighter } from …

How to create Quick look thumbnail for files

Issue #913

Use QuickLookThumbnailing framework

import AppKit
import QuickLookThumbnailing

actor QuicklookService {
    static let shared = QuicklookService()
    
    private let generator = QLThumbnailGenerator.shared
    
    func image( …

How to deal with actor reentrancy in Swift

Issue #912

Perform check before and after suspension point

actor Worker {
    var isDoing = false
    var toBeDone = Set<String>()
    
    func work(string: String) async {
        if isDoing {
            toBeDone.insert(string) …

How to run parallel Task with Swift concurrency

Issue #911

Make an parallelTask function that wraps TaskGroup

public func parallelTask(@ParallelTaskBuilder builder: () -> [ParallelTaskBuilder.Work]) async {
    await withTaskGroup(of: Void.self) { group in
        for work in builder() { …

How to use Range and NSRange in Swift

Issue #910

Use one-sided range operator

let string = "Hello world"
string[string.startIndex...] // Hello world
string[..<string.endIndex] // Hello world

Substring

let string = "Hello world"
let range = string.startIndex ..< …

Khoa Pham

Hello, I’m Khoa

I’m a thinker and storyteller with a passion for exploring the intersection of creativity and technology

🧑‍💻 I love crafting high quality and useful apps
🔥 I love open source. My GitHub open source has 2.3k followers with packages that are integrated by 45k+ apps and over 3.4m+ downloads on CocoaPods.
✍️ I write here on my blog and on Medium, which has over 2.7k+ followers with tons of articles and 90k+ monthly views.
🖥 Follow me for sharings about Swift, SwiftUI, iOS and macOS development.
Hei