Skip to content

Instantly share code, notes, and snippets.

@Canis-UK
Created July 10, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Canis-UK/2409f367bbef978b43c3 to your computer and use it in GitHub Desktop.
Save Canis-UK/2409f367bbef978b43c3 to your computer and use it in GitHub Desktop.
Reproducing an issue with FastCoding and Swift 2.0
import UIKit
class TestObject: NSObject, NSCoding
{
var myData1: Int
var myData2: Int
init(a: Int, b: Int)
{
myData1 = a
myData2 = b
}
required init?(coder: NSCoder)
{
let pair = coder.decodeObjectForKey("myData") as! [Int]
myData1 = pair[0]
myData2 = pair[1]
}
func encodeWithCoder(coder: NSCoder)
{
coder.encodeObject([myData1, myData2], forKey: "myData")
}
}
func ==(lhs: TestObject, rhs: TestObject) -> Bool
{
return (lhs.myData1 == rhs.myData1) && (lhs.myData2 == rhs.myData2)
}
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let testData =
[
"test1": TestObject(a: 1, b: 2),
"test2": TestObject(a: 3, b: 4),
]
#if true
let encoded = FastCoder.dataWithRootObject(testData)
let decoded = FastCoder.objectWithData(encoded) as! [String: TestObject]
#else
let encoded = NSKeyedArchiver.archivedDataWithRootObject(testData)
let decoded = NSKeyedUnarchiver.unarchiveObjectWithData(encoded) as! [String: TestObject]
#endif
let a = decoded["test1"]!
let b = decoded["test2"]!
assert(a.myData1 == 1)
assert(a.myData2 == 2)
assert(b.myData1 == 3)
assert(b.myData2 == 4)
assert(Array(testData.keys) == Array(decoded.keys))
for (k1, k2) in zip(testData.keys, decoded.keys)
{
assert(testData[k1]! == decoded[k2]!)
}
print("OK")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment