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 use KVO in Swift

Issue #554

A class must inherit from NSObject, and we have 3 ways to trigger property change

Use setValue(value: AnyObject?, forKey key: String) from NSKeyValueCoding

class MyObjectToObserve: NSObject {
    var myDate = NSDate()
    func …

How to use precondition and assert in Swift

Issue #553

Read Swift asserts - the missing manual

                            debug	release	  release
    function	            -Onone	-O	     -Ounchecked
    assert()	            YES	    NO	      NO
    assertionFailure()	    YES	    NO	      NO** …

How to get ISO string from date in Javascript

Issue #552

type Components = {
  day: number,
  month: number,
  year: number
}

export default class DateFormatter {
  // 2018-11-11T00:00:00
  static ISOStringWithoutTimeZone = (date: Date): string => {
    const components = DateFormatter. …

How to use regular expression in Swift

Issue #551

Find matches

import Foundation

public extension String {
    func matches(pattern: String) throws -> [String] {
        let regex = try NSRegularExpression(pattern: pattern)
        let results = regex.matches(in: self, options: [], …

Why is didSelectItem not called in UICollectionView

Issue #550

Check

  • shouldHighlightItem -> shouldSelectItem -> didSelectItem
  • Gesture recognizer
  • isUserInteractionEnabled

How to keep command line tool running with async in Swift

Issue #549

Use Semaphore

public class Sequence: Task {
    public func run(workflow: Workflow, completion: @escaping TaskCompletion) {
        let semaphore = DispatchSemaphore(value: 0)
        runFirst(tasks: tasks, workflow: workflow, completion: …

How to use deep link and universal link in iOS

Issue #548

How to sync an async function in Swift

Issue #547

func sync<T>(_ work: (@escaping ([T]) -> Void) -> Void) -> [T] {
    let semaphore = DispatchSemaphore(value: 1)
    var results = [T]()
    work { values in
        results = values
        semaphore.signal()
    } …

How to use UICollectionViewLayout

Issue #546

How to do localization in Xcode

Issue #545

Xcode 10 Localization catalog and XclocReader

Xcode 11 localized screenshots

How to use xcodebuild

Issue #544

man xcodebuild

man xcodebuild

XCODEBUILD(1)             BSD General Commands Manual            XCODEBUILD(1)

NAME
     xcodebuild -- build Xcode projects and workspaces

SYNOPSIS
     xcodebuild [-project name.xcodeproj] [[-target …

How to use Derived data in Xcode

Issue #543

Workspace

Workspace has its own DerivedData folder

DerivedData
  ModuleCache.noindex
  workspace_name
    Build
      Products
        Debug-iphonesimulator
          Cat
          Dog
          Dog2
    Index
    Info.plist
    Logs …

How to not use protocol extension in Swift

Issue #542

With protocol extension

See code Puma

Build is UsesXcodeBuild is UsesCommandLine
/// Any task that uses command line
public protocol UsesCommandLine: AnyObject {}

public extension UsesCommandLine {
    func runBash(
        workflow: …

How to use test scheme in Xcode

Issue #541

Scheme action

A scheme, either for app or test, consists of actions

Run

Used when Cmd+R. The executable specifies which app target to run

Screenshot 2019-12-14 at 23 39 48

Test

Used when Cmd+U. The tests specifies which test target to run

Screenshot 2019-12-14 at 23 40 09

Test target recognises app …

How to set language and locale with xcodebuild

Issue #540

testLanguage and testRegion

 -testLanguage language
       Specifies ISO 639-1 language during testing. This overrides the setting for the test action of a
       scheme in a workspace.

 -testRegion region
       Specifies ISO 3166-1 …

How to take screenshots for UITest in Xcodee

Issue #539

XCUIScreenshot

extension XCTestCase {
    func takeScreenshot(name: String) {
        let screenshot = XCUIScreen.main.screenshot()
        let attach = XCTAttachment(screenshot: screenshot)
        attach.lifetime = .keepAlways …

How to fix UIToolbar Auto Layout issues

Issue #538

Hierarchy

UIToolbar -> _UIToolbarContentView -> _UIButtonBarStackVie

UIToolbarContentView

_UIToolbarContentView's width should equal 0
_UIToolbarContentView's height should equal 0

Workaround that fixes 1 warning

toolbar. …

How to use passed launch arguments in UITests

Issue #537

Specify launch arguments

In xcodebuild, specify launch arguments.

You can specify this under Launch Arguments in Run action of the app scheme or UITest scheme

Screenshot 2019-12-10 at 23 27 02
-AppleLanguages (jp) -AppleLocale (jp_JP)
(lldb) po ProcessInfo().arguments
▿ …

How to add padding to left right view in UITextField

Issue #536

extension UITextField {
    func setLeftView(_ view: UIView, padding: CGFloat) {
        view.translatesAutoresizingMaskIntoConstraints = true

        let outerView = UIView()
        outerView.translatesAutoresizingMaskIntoConstraints = …

How to set date color in UIDatePicker in iOS 13

Issue #535

datePicker.setValue(UIColor.green, forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")

In iOS 14

if #available(iOS 14.0, *) {
    datePicker.preferredDatePickerStyle = .wheels
    datePicker.tintColor …

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