Nate Stedman

NSParagraphStyle+With

January 28th, 2016

I wrote Attributed to make attributed strings in Foundation more expressive. It works well with many attributes, including fonts and colors, because those values are represented by a single simple object. However, the paragraph style attribute contains a number of different properties. By default, the best way to create a paragraph style is to create a mutable version and set its properties. As this requires multiple expressions, it can’t be used inline with other attribute functions, without embedding it within a closure, which is also unnecessarily verbose.

To solve this, I’ve written a simple extension function for NSParagraphStyle, called with. It takes all of the properties that can be set on a mutable paragraph style as parameters, all of which have default values, so only the necessary properties need to be specified, like so:

let style = NSParagraphStyle.with(alignment: .Center, lineSpacing: 12)

Available on Github.

Nested closures & weak capture

January 27th, 2016

In Swift code, there’s a potential strong reference leak hiding in nested closures:

foo({
    bar({ [weak self] in
        self?.baz()
    })
})

This code seems okay, since only bar refers to self, which is captured weakly. However, that’s not actually the case! foo captures self strongly. Instead:

foo({ [weak self] in
    bar({ [weak self] in
        self?.baz()
    })
})

Will work correctly. There is a proposal to change this.

Joining Attributes

January 5th, 2016

Since the first release, Attributed has had a join() extension function for sequences of attributed-string-like values:

let attributedString = [
    "Unformatted ",
    UIColor.redColor().foregroundAttribute(["Red"]),
].join().attributedString

This allows for attribute-less portions in the final attributed string. In Attributed 0.3.0, the join() extension is added to sequences of attribute functions as well.

Read More

Shirley

December 30th, 2015

Shirley is a minimal request framework for Swift programs. It uses ReactiveCocoa signal producers as a unified method for asynchronous data delivery, instead of callback functions. This allows disparate responses to be unified, with a single stream-based system.

Sessions

The central type of the framework is a session, represented by the SessionType protocol. Session is a closure-based implementation, which can also be used for type erasure.

Read More