Latest weblog entries

2017-10-09 using reduce in Swift

Here's a little playground that shows how to use reduce in Swift, specifically use it to report on a bunch of booleans. In the following code, we use reduce to determine whether all objects are enabled, or whether at least one is enabled.

    struct MyStruct {
        var enabled: Bool
        var text: String
    let collection = [
        MyStruct(enabled: true, text: "one"),
        MyStruct(enabled: false, text: "two"),
        MyStruct(enabled: true, text: "three"),
    let enabledArray = { $0.enabled }
    let allEnabled = enabledArray.reduce(true) { $0 && $1 }
    let oneEnabled = enabledArray.reduce(false) { $0 || $1 }

I've used this when I had a bunch of UITextField instances. Some of them were enabled, some not (i.e. property isEnabled set to true or false). Or for example when you have a bunch of UIView instances; are none of them hidden? That sort of stuff.

I could've posted this example with just an array of booleans as input, but I wanted to demonstrate the map as well. Often you don't just have a bare array of booleans.

One minor thing with the allEnabled variable in the example is that its result is meaningless when applied to an empty array. It'll return true. But what does that mean, right? You'll have to decide for yourself.

2017-10-06 reset display under macOS

Sometimes, you need to reset the display under macOS, without it being visible. Under Linux, this is incredibly easy; CTRL-ALT-F1 usually gives you a text console. You can then jump back to the GUI with alt-Left. Under macOS, this is slightly harder.

This does require preparation. First, you have to make a shortcut key to System Preferences:

  • Open System Preferences
  • Open the Keyboard applet
  • Open the Shortcuts tab
  • On the left, select App Shortcuts
  • On the right, below All Applications, add a shortcut key. The text should be exactly "System Preferences..." The shortcut key could be something like Ctrl-Cmd-,

In the future, it'll help you as follows: assuming you're logged in and for some reason don't have a display, do the following procedure:

  • Type Ctrl-Cmd-,
  • Hit Cmd-L to assure System Preferences is at the Show All view
  • Type "Displays" and hit space. This opens the Display Preferences.
  • Assuming the resolution is set to "Default for display", hit the Tab key twice and press Down arrow. This selects "scaled".
  • Hit tab three times. This selects the medium resolution. Hit space. This should reset the resolution and bring back your display.

2017-09-25 Swift example of a factory and a closure typealias

I couldn't find a nice, compact example of a closure typealias in Swift, so here is one you can paste straight into a Playground:

    import Foundation
    typealias MakeClosure = (_ a: Int, _ b: Int) -> Int
    class IntFactory {
        static let instance = IntFactory()
        var makeClosure: MakeClosure?
        func makeInt(a: Int, b: Int) -> Int {
            guard let closure = self.makeClosure else {
            let result = closure(a, b)
            return result
        private init() {
    func sum(a: Int, b: Int) -> Int {
        let result = a + b
        return result
    IntFactory.instance.makeClosure = sum
    IntFactory.instance.makeInt(a: 22, b: 20)
    // This also works
    let sum2: MakeClosure = { (a: Int, b: Int) in
        a + b
    IntFactory.instance.makeClosure = sum2
    IntFactory.instance.makeInt(a: 22, b: 20)

2017-09-17 Replacing NSTimer in Swift 4

For repeated calling of a function, the Timer class is available (used to be called NSTimer). So perhaps you had the following code in your Swift 3 project:

    private var timer: Timer?
    override func viewDidAppear(_ animated: Bool) {
        self.timer = Timer.scheduledTimer(
            timeInterval: 1.0, target: self,
            selector: #selector(self.timerAction),
            userInfo: nil,
            repeats: true)
    func timerAction() {
        // Do something

When you move your project to Swift 4 (via Xcode 9), then first you get the following warning:

    The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc inference” logging enabled, and then disable inference by changing the "Swift 3 @objc Inference" build setting to "Default" for the "blah" target.

You can fix this warning by updating the project settings and update the code as follows:

    @objc func timerAction() {
        // Do something

For some reason, I don't like seeing that @objc in my projects. There's an alternative, and that's not to use Timer at all, but instead move to GCD:

    private let timer = DispatchSource.makeTimerSource()
    override func viewDidAppear(_ animated: Bool) {
        timer.schedule(deadline: .now(), repeating: 1.0)
        timer.setEventHandler {
            DispatchQueue.main.sync {

Have fun with Swift 4 :)


Weblog Archive

Weblog entries 2017

Weblog entries 2016

Weblog entries 2015

Weblog entries 2014

Weblog entries 2013

Weblog entries 2012

Weblog entries 2011

Weblog entries 2010

Weblog entries 2009

Weblog entries 2008

Weblog entries 2007

Weblog entries 2006

Weblog entries 2005

Weblog entries 2004

All weblog entries


Articles, chronologically (latest first). This is pretty old stuff.


Not yet finished. Maybe will never be finished. Maybe they'll get deleted. Who knows?


System administration: