Skip to content

Commit

Permalink
NSCoding array fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 12, 2016
1 parent 9394ad3 commit 20db5d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVReflection"
s.version = "2.20.1"
s.version = "2.20.2"
s.summary = "iOS: Swift helper library with reflection functions"
s.description = "Swift helper library with reflection functions including support for NSCoding, Printable, Hashable, Equatable and JSON"
s.homepage = "https://github.com/evermeer/EVReflection"
Expand Down
35 changes: 35 additions & 0 deletions EVReflection/EVReflectionTests/EVReflectionEVObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,39 @@ class EVReflectionEVObjectTests: XCTestCase {
XCTAssert(theObject == result, "Pass")
}

func testNSCodingSubObjectArray() {
let rootObject = TestRootObject()
rootObject.id = "root"
let inner1 = TestInnerObject()
inner1.id = "inner1"
let inner2 = TestInnerObject()
inner2.id = "inner2"
rootObject.array = [inner1, inner2]

rootObject.saveToTemp("rootObject.dat")
let result = TestRootObject(fileNameInTemp: "rootObject.dat")

XCTAssert(result.array?.count == 2, "Array size should be 2")

//Crash here
let out1 = result.array?.first

XCTAssert(out1?.id == "inner1")
}

}


public class TestBaseObject: EVObject {
var id: String?
}

public class TestRootObject: TestBaseObject {
var array: [TestInnerObject]?
}

public class TestInnerObject: TestBaseObject {
}



8 changes: 5 additions & 3 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ final public class EVReflection {

:returns: Nothing
*/
public class func encodeWithCoder(theObject: NSObject, aCoder: NSCoder) {
public class func encodeWithCoder(theObject: EVObject, aCoder: NSCoder) {
let (hasKeys, _) = toDictionary(theObject, performKeyCleanup:false)
for (key, value) in hasKeys {
aCoder.encodeObject(value, forKey: key as! String)
Expand All @@ -261,16 +261,18 @@ final public class EVReflection {
:parameter: theObject The object that we want to decode.
:parameter: aDecoder The NSCoder that will be used for decoding the object.
*/
public class func decodeObjectWithCoder(theObject: NSObject, aDecoder: NSCoder) {
public class func decodeObjectWithCoder(theObject: EVObject, aDecoder: NSCoder) {
let (hasKeys, _) = toDictionary(theObject, performKeyCleanup:false)
let dict = NSMutableDictionary()
for (key, _) in hasKeys {
if aDecoder.containsValueForKey(key as! String) {
let newValue: AnyObject? = aDecoder.decodeObjectForKey(key as! String)
if !(newValue is NSNull) {
theObject.setValue(newValue, forKey: key as! String)
dict[key as! String] = newValue
}
}
}
EVReflection.setPropertiesfromDictionary(dict, anyObject: theObject)
}

/**
Expand Down

0 comments on commit 20db5d7

Please sign in to comment.