Khoa Pham
Khoa Pham

Ohayo

Swift Discovery

Discover all the tech

How to test DispatchQueue in Swift

Issue #646

Sync the DispatchQueue

Pass DispatchQueue and call queue.sync to sync all async works before asserting

Use mock

Use DispatchQueueType and in mock, call the work immediately

swift …

How to use webpack to bundle html css js

Issue #645

Install webpack

js
npm init
npm install webpack webpack-cli --save-dev
vim webpack.config.js
text …

How to assert asynchronously in XCTest

Issue #644

swift
import XCTest

extension XCTestCase {
    /// Asynchronously assertion
    func XCTAssertWait(
        timeout: TimeInterval = 1,
        _ …

How to make simple filter menu in css

Issue #643

Use material icons

html
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
css …

How to make simple grid gallery in css

Issue #642

Specify container with flex-wrap and justify-content, and item with float: left

css
div.cards {
  display: flex;
  justify-content: center; …

How to add independent page in hexo

Issue #641

Create a new page

js
hexo new page mydemo

Remove index.md and create index.html, you can reference external css and js in this index.html. Hexo has …

How to use async function as parameter in TypeScript

Issue #640

js
async function useCache(
    collectionName: string, 
    key: string, 
    response: functions.Response<any>, 
    fetch: () => Promise …

How to format percent in Swift

Issue #639

Never use String(format: "%.2f %%", 1.2 because each region can have different separator and placement of percent sign.

Use NumberFormatter instead

swift …

How to declare commands in Xcode extensions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

swift
import Foundation
import XcodeKit

class SourceEditorExtension: NSObject, …

How to declare commands in Xcode extenstions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

swift
import Foundation
import XcodeKit

class SourceEditorExtension: NSObject, …

How to disable ring type in TextField in SwiftUI

Issue #636

Normally we can just wrap NSTextField

swift
struct SearchTextField: NSViewRepresentable {
    @Binding var text: String
    var hint: String
    var …

How to handle enter key in NSTextField

Issue #635

swift
textField.delegate = self
swift
NSTextFieldDelegate …

How to decode with default case for enum in Swift

Issue #634

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

How to conditionally apply modifier in SwiftUI

Issue #633

Use autoclosure and AnyView

swift
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension View {
    func applyIf<T: View>(_ …

How to toggle with animation in SwiftUI

Issue #632

Use Group

swift
private func makeHeader() -> some View {
    Group {
        if showsSearch {
            SearchView(
                onSearch: …

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

text …

How to make segmented control in SwiftUI for macOS

Issue #629

Use Picker with SegmentedPickerStyle.

swift
Picker(selection: $preferenceManager.preference.display, label: EmptyView()) {
    Image("grid") …

How to iterate over XCUIElementQuery in UITests

Issue #628

swift
extension XCUIElementQuery: Sequence {
    public typealias Iterator = AnyIterator<XCUIElement>
    public func makeIterator() -> …

How to check if NSColor is light

Issue #627

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

swift
extension NSColor {
    var isLight: Bool {
        guard
            let …

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