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 decode with default case for enum in Swift

Issue #634

public enum Weapon: String, Decodable {
    case sword = "SWORD"
    case gun = "GUN"
    case unknown = "UNKNOWN"

    public init(from decoder: Decoder) throws {
        let rawValue = try decoder. …

How to conditionally apply modifier in SwiftUI

Issue #633

Use autoclosure and AnyView

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension View {
    func applyIf<T: View>(_ condition: @autoclosure () -> Bool, apply: (Self) -> T) -> AnyView {
        if …

How to toggle with animation in SwiftUI

Issue #632

Use Group

private func makeHeader() -> some View {
    Group {
        if showsSearch {
            SearchView(
                onSearch: onSearch
            )
            .transition(.move(edge: .leading))
        } else { …

How to use background in iOS

Issue #631

beginBackgroundTask

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time

When your app moves to the background, the system …

How to show context popover from SwiftUI for macOS

Issue #630

For SwiftUI app using NSPopover, to show context popover menu, we can ask for windows array, get the _NSPopoverWindow and calculate the position. Note that origin of macOS screen is bottom left

(lldb) po NSApp.windows
▿ 2 elements
  - 0 : …

How to make segmented control in SwiftUI for macOS

Issue #629

Use Picker with SegmentedPickerStyle.

Picker(selection: $preferenceManager.preference.display, label: EmptyView()) {
    Image("grid")
        .resizable()
    .padding()
        .tag(0)
    Image("list")
        .resizable …

How to iterate over XCUIElementQuery in UITests

Issue #628

extension XCUIElementQuery: Sequence {
    public typealias Iterator = AnyIterator<XCUIElement>
    public func makeIterator() -> Iterator {
        var index = UInt(0)
        return AnyIterator {
            guard index < …

How to check if NSColor is light

Issue #627

Algorithm from https://www.w3.org/WAI/ER/WD-AERT/#color-contrast

extension NSColor {
    var isLight: Bool {
        guard
            let components = cgColor.components,
            components.count >= 3
        else { return false } …

How to trigger onAppear in SwiftUI for macOS

Issue #626

SwiftUI does not trigger onAppear and onDisappear like we expect. We can use NSView to trigger

import SwiftUI

struct AppearAware: NSViewRepresentable {
    var onAppear: () -> Void

    func makeNSView(context: …

How to force refresh in ForEach in SwiftUI for macOS

Issue #625

For some strange reasons, content inside ForEach does not update with changes in Core Data NSManagedObject. The workaround is to introduce salt, like UUID just to make state change

struct NoteRow: View {
    let note: Note
    let id: UUID …

How to access bookmark url in macOS

Issue #624

By default the approaches above grant you access while the app remains open. When you quit the app, any folder access you had is lost.

To gain persistent access to a folder even on subsequent launches, we’ll have to take advantage of a …

How to force FetchRequest update in SwiftUI

Issue #623

Listen to context changes notification and change SwiftUI View state

let changes = [NSDeletedObjectsKey: ids]
NSManagedObjectContext.mergeChanges(
    fromRemoteContextSave: changes,
    into: [context]
)
try context.save()

struct …

How to batch delete in Core Data

Issue #622

Read Implementing Batch Deletes

If the entities that are being deleted are not loaded into memory, there is no need to update your application after the NSBatchDeleteRequest has been executed. However, if you are deleting objects in the …

How to update FetchRequest with predicate in SwiftUI

Issue #621

Make subview that accepts FetchRequest. Trigger search by setting property

struct SideView: View {
    @Environment(\.managedObjectContext)
    var context

    @State var search: Search?

    var body: some View {
        VStack(alignment …

How to make TextField focus in SwiftUI for macOS

Issue #620

For NSWindow having levelother than .normal, need to override key and main property to allow TextField to be focusable

class FocusWindow: NSWindow {
    override var canBecomeKey: Bool  { true }
    override var canBecomeMain: Bool { …

How to manage work

Issue #619

Remote

Communication

Plan

How to show popover for item in ForEach in SwiftUI

Issue #618

Create custom Binding

List {
    ForEach(self.items) { (item: item) in
        ItemRowView(item: item)
            .popover(isPresented: self.makeIsPresented(item: item)) {
                ItemDetailView(item: item)
            }
    }
} …

How to make tooltip in SwiftUI for macOS

Issue #617

On macOS 11, we can use .help modifier to add tooltip

Button()
    .help("Click here to open settings")

If you support macOS 10.15, then create empty NSView and use as overlay. Need to updateNSView in case we toggle the state of …

How to make translucent SwiftUI List in macOS

Issue #615

List {
    ForEach(books) { (book: Book) in
        BookRow(book: book)
    }
}
.listStyle(SidebarListStyle())  

How to make tab view in SwiftUI

Issue #614

struct MyTabView: View {
    @EnvironmentObject
    var preferenceManager: PreferenceManager

    var body: some View {
        VOrH(isVertical: preferenceManager.preference.position.isVertical) {
            OneTabView(image: …

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