Skip to content

Instantly share code, notes, and snippets.

@regnerjr
Created March 15, 2015 14:41
Show Gist options
  • Save regnerjr/a7603c49ac79ca14aaa2 to your computer and use it in GitHub Desktop.
Save regnerjr/a7603c49ac79ca14aaa2 to your computer and use it in GitHub Desktop.
A set of functions for Archiving Swift Collections.
import Swift
import Foundation
// convert input Collection<T> into an NSMutableArray<NSData>
// works with Array<T>, Set<T>, Dictionary<T,U>
func convertCollectionToArrayOfData<T: CollectionType>(collection: T) -> NSMutableArray {
return NSMutableArray(array: map(collection){
var mutableItem = $0
return NSData(bytes: &mutableItem, length: sizeof(T.Generator.Element.self))
})
}
/// Helper function don't call this.
func memoryOfType<T>(fromAnyObject obj: AnyObject) -> T {
let itemData = UnsafeMutablePointer<T>.alloc(sizeof(T))
(obj as! NSData).getBytes(itemData, length: sizeof(T))
return itemData.memory
}
func restoreArrayFromArchiveArray<T>( array: NSMutableArray) -> Array<T>{
return Array<T>( map( array ) { memoryOfType(fromAnyObject: $0) } )
}
func restoreSetFromArchiveArray<T>(array: NSMutableArray) -> Set<T> {
return Set<T>( map( array ) { memoryOfType(fromAnyObject: $0) } as [T] )
}
func restoreDictFromArchiveArray<T,U>(array: NSMutableArray) -> Dictionary<T,U>{
var results = Dictionary<T,U>()
map(array){ item -> Void in
let (k,v): (T,U) = memoryOfType(fromAnyObject: item)
results[k] = v
}
return results
}
// Our model object is an immutable struct, like all the magic immutable
// data structures we cool kids are supposed to have in 2015
struct Person {
let name: String
let age: Int
}
// Person needs to be equatable in order to be used in a Set
extension Person: Equatable {}
func ==(rhs: Person, lhs: Person) -> Bool{
return (rhs.name == lhs.name) && (rhs.age == lhs.age)
}
// Person Must be hashable to be used in a set
extension Person : Hashable {
var hashValue: Int {
return self.name.hashValue
}
}
//as our program runs we add some structs to a store
var people = [Person]()
let me = Person(name: "John", age: 30)
let shelby = Person(name: "Shelby", age: 31)
people.append(me)
let uum = NSUUID()
people.append(shelby)
let uus = NSUUID()
// Dictionary Example
var someDict = [uum: me, uus: shelby]
let archive = NSKeyedArchiver.archivedDataWithRootObject(convertCollectionToArrayOfData(someDict))
let unarchive = NSKeyedUnarchiver.unarchiveObjectWithData(archive) as? NSMutableArray
let restored = restoreDictFromArchiveArray(unarchive!) as Dictionary<NSUUID,Person>
restored == someDict
// Array Example
let arrayArchive = NSKeyedArchiver.archivedDataWithRootObject(convertCollectionToArrayOfData(people))
let arayUnarchive = NSKeyedUnarchiver.unarchiveObjectWithData(arrayArchive) as? NSMutableArray
let restoredArray = restoreArrayFromArchiveArray(arayUnarchive!) as Array<Person>
restoredArray == people
// Set Example
let peopleSet = Set<Person>([me, shelby])
let setArchive = NSKeyedArchiver.archivedDataWithRootObject(convertCollectionToArrayOfData(peopleSet))
let setUnarchive = NSKeyedUnarchiver.unarchiveObjectWithData(setArchive) as? NSMutableArray
let restoredSet = restoreSetFromArchiveArray(setUnarchive!) as Set<Person>
restoredSet == peopleSet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment