TahDoodle TaskStore extension

Hello,

The sample code for TahDoodle introduces an extension to TaskStore to add a stored static property ‘sample’ (p. 1081). I thought it was not possible to add stored properties using an extension, but apparently it is. Could someone please provide some more insight?

extension TaskStore {
static var sample: TaskStore = {

let store = TaskStore()
return store
}()
}

Thank you,
Luc

You can add a static property with an extension because it is a single property shared across all objects of that type. It doesn’t change the memory layout of the objects so the code remains compatible with other code that was compiled not knowing about the extension.

There are limits on this, however. You can read a more detailed explanation here.