Latest 0.0.7
License MIT
Platforms ios 8.0

Struct Archiver

CocoaPods Compatible
Carthage Compatible

Archive struct and fundamental values into NSData and unarchive.


  • [x] Archive/Unarchive Fundamental Values
  • [x] Archive/Unarchive Custom Struct Values
  • [ ] Linux Build (for Swift3)


  • iOS 8.0+
  • Xcode 7.3+


  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.



pod 'StructArchiver'


github "naru-jpn/struct-archiver"

Swift Packege Manager


You need to activate Archiver before archiving/unarchiving as below.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Activate StructArchiver
    StructArchiver.activateStandardArchivables(withCustomStructActivations: {
        // Add activation if you create customArchivable struct 

    // ...

    return true


Archive/Unarchive Fundamental Values

You can archive value of type conforming Archivable protocol and you can unarchive data by passing archived data to archiver.

    // Int
    let archivedInt: Int = -2016
    let archivedIntData: NSData = archivedInt.archivedData
    let unarchiveInt = Archiver.unarchive(data: archivedIntData) // -2016

    // Double
    let archivedDouble: Double = 2.016
    let archivedDoubleData: NSData = archivedDouble.archivedData
    let unarchivedDouble = Archiver.unarchive(data: archivedDoubleData) // 2016

    // String
    let archivedString: String = "archiving..."
    let archivedStringData: NSData = archivedString.archivedData
    let unarchiveString = Archiver.unarchive(data: archivedStringData) // "archiving..."

Value types you can archive

Int, UInt, Float, Double, String, [Archibavle], [String: Archibavle]

Archive/Unarchive Custom Struct Values

1. Define CustomArchivable Struct

Computed property restoreProcedure returns closure to convert dictionary into struct. Dictionary contains values of property having struct.

struct SampleStruct: CustomArchivable {

    // Archived values need to conform Archivable protocol.
    let title: String
    let timestamp: Double

    // Return closure to convert dictionary into struct
    public static var restoreProcedure: ArchiveRestoreProcedure {
        return { (dictionary: ArchivableDictionary) in
            if let title = dictionary["title"] as? String, let timestamp = dictionary["timestamp"] as? Double {
                return SampleStruct(title: title, timestamp: timestamp)
            return SampleStruct(title: "", timestamp: 0.0)

2. Add Activation

StructArchiver.activateStandardArchivables(withCustomStructActivations: {

3. Archive/Unarchive

You can archive/unarchive by the same way for fundamental values.

let archivedStruct: SampleStruct = SampleStruct(title: title, timestamp: timestamp)
// Archive
let archivedStructData: NSData = archivedStruct.archivedData
// Unarchive
let unarchivedStruct = Archiver.unarchive(data: archivedStructData)

Archive/Unarchive Complex Values

You can archive array or dictionary containing archibavle values.

let archivedStruct1: SampleStruct = SampleStruct(title: title1, timestamp: timestamp1)
let archivedStruct2: SampleStruct = SampleStruct(title: title2, timestamp: timestamp2)
let archivedStruct3: SampleStruct = SampleStruct(title: title3, timestamp: timestamp3)

let archivedStructs: Archivables = [archivedStruct1, archivedStruct2, archivedStruct3]

// Archive
let archivedStructsData: NSData = archivedStructs.archivedData
// Unarchive
let unarchivedStructs = Archiver.unarchive(data: archivedStructsData)

See example project too.



Latest podspec

    "name": "StructArchiver",
    "version": "0.0.7",
    "summary": "Archive struct into NSData and unarchive in Swift.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    "authors": {
        "naru-jpn": "[email protected]"
    "platforms": {
        "ios": "8.0"
    "source": {
        "git": "",
        "tag": "0.0.3"
    "source_files": "Sources/*.swift"

Pin It on Pinterest

Share This