Archive struct and fundamental values into NSData and unarchive.
- Archive/Unarchive Fundamental Values
- 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"
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
}
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..."
Int, UInt, Float, Double, String, [Archibavle], [String: Archibavle]
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)
}
}
}
StructArchiver.activateStandardArchivables(withCustomStructActivations: {
SampleStruct.activateArchive()
})
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)
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.
MIT