Platformsios 10.0


Use Sticky to quickly persist common Swift objects using the Swift 4 Codable type and local file storage.

How it works


Simply define an object in Swift (use your value types!) and conform it to the Stickable protocol. In order to take full advantage of Sticky, make sure to add Equatable conformance to your object as well.

Note: In Swift 4.1, conformance can be synthesized for you.

import Sticky

struct Candy: Stickable {
    var productId: Int
    var name: String
    var rating: Rating

// Needs to conform to Equatable
extension Candy: Equatable {
    static func == (lhs: Candy, rhs: Candy) -> Bool {
            lhs.productId == rhs.productId &&
   == &&
            lhs.rating == rhs.rating


Once you conform your object to Stickable, all you need to do is instantiate it and call the stick() method to persist it.

var candyBar = Candy(productId: 1, name: "Snickers", rating: .four)


Want to get your data back out?
//  [{productId: 1, name: "Snickers", rating: 4}]


If you’re following along at home, you also need to define the Rating type used above and make sure it’s also Codable.

enum Rating: Int {
    case one = 1
    case two
    case three
    case four

extension Rating: Codable {}

So what if you want to add a new candy bar? = "Milky Way"
// [
//  {productId: 1, name: "Snickers", rating: 4},
//  {productId: 1, name: "Milky Way", rating: 4}
// ]


Wait, I didn’t want to create a new candy bar, just wanted to update the name…

No problem, just create a StickyKey:

extension Candy: StickyKey {
    struct Key: Equatable {
        var productId: Int

        static func ==(lhs: Key, rhs: Key) -> Bool {
            return lhs.productId == rhs.productId

    var key: Candy.Key {
        return Candy.Key(productId: self.productId)
} = "Almond Joy"

// [
//  {productId: 1, name: "Almond Joy", rating: 4},
//  {productId: 1, name: "Milky Way", rating: 4}
// ]


Probably want to get rid of that duplicate productId now…

A couple ways to do that. First, you can simply create or use the Milky Way object to unstick.

let milkyWay = Candy(productId: 1, name: "Milky Way", rating: .four)
// [{productId: 1, name: "Almond Joy", rating: 4}]


Also, when you first initialize Sticky (for instance, from AppDelegate), you can configure it to clear the directory on startup which gives you a clean slate.

let stickyConfig = StickyConfiguration(
    preloadCache: false, 
    clearDirectory: true, 
    async: false, 
    logStyle: .verbose)

Sticky.configure(with: .custom(stickyConfig))

Of course, you can grab the directory and remove the .json files yourself.

// /var/folders/63/hmdwgb3148v4_xzv_jff_ztr0000gn/T/



  • iOS 10.0+ | macOS 10.12+ | tvOS 11.0+ | watchOS 4.0+
  • Xcode 9.0+


    CocoaPods (iOS 10+, OS X 10.12+)

You can use CocoaPods to install Sticky by adding it to your Podfile:

platform :ios, '10.0'

target 'MyApp' do
    pod 'Sticky'

Latest podspec

    "name": "Sticky",
    "version": "1.0.0",
    "summary": "Use Sticky to quickly persist common Swift objects using the Swift 4 Codable type and local file storage.",
    "description": "Sticky allows developers to quickly and easily persist common Swift objectsnwithout the overhead of a full persistence layer. Sticky leverages JSONnand the "Document" directory, along with the Codable protocol, for easy nstorage and access. Sticky is not intended for extremely large or highly ntransactional data sets, however, it does leverage caching to help with performance.nSticky includes powerful features like built in notification center integrationnfor transaction events. It also includes a simple schema change management solutionnas app data structure changes over the life of the app.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "James Langdon": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.0.0"
    "platforms": {
        "ios": "10.0"
    "swift_version": "4.0",
    "source_files": "sticky/**/*"

Pin It on Pinterest

Share This