diff --git a/EVReflection.podspec b/EVReflection.podspec index 3388377a..c3673741 100644 --- a/EVReflection.podspec +++ b/EVReflection.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "EVReflection" - s.version = "4.5.0" + s.version = "4.5.1" s.summary = "Reflection based (dictionary, JSON or XML) object mapping (including extensions for Alamofire and Moya with RxSwift or ReactiveSwift)" s.description = <<-EOS diff --git a/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate b/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate index b0dcd0b8..41ce23d3 100644 Binary files a/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate and b/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Source/EVReflectable.swift b/Source/EVReflectable.swift index 285d2e8c..baa96043 100644 --- a/Source/EVReflectable.swift +++ b/Source/EVReflectable.swift @@ -304,6 +304,15 @@ extension EVReflectable { return false } + /** + Return a custom object for the object + + - returns: The custom object that will be parsed (single value, dictionary or array) + */ + public func customConverter() -> AnyObject? { + return nil + } + /** Get the type of this object diff --git a/UnitTests/EVReflectionTests/EVReflectionExtendingNSObjects.swif.swift b/UnitTests/EVReflectionTests/EVReflectionExtendingNSObjects.swif.swift new file mode 100644 index 00000000..2ee9dab1 --- /dev/null +++ b/UnitTests/EVReflectionTests/EVReflectionExtendingNSObjects.swif.swift @@ -0,0 +1,82 @@ +// +// EVReflectionExtendingNSObjects.swif.swift +// UnitTests +// +// Created by Edwin Vermeer on 18/03/2017. +// Copyright © 2017 evict. All rights reserved. +// + +import Foundation +import EVReflection + + +// You can have any object that has NSObject as its base class +public class ExtendingNSObjects: NSObject { +} + +// And then extend it with the EVReflectable protocol using only: +extension ExtendingNSObjects: EVReflectable { } + +// You could for instance do this for a Realm Object or a Core data NSMAnagedObject + + + +// It is adviced to add an implementation for the 'setValue forUndefinedKey' to prevent crashes. +// You cod do that like this: + +public class ExtendingNSObjectsMore: NSObject { +} + +extension ExtendingNSObjectsMore: EVReflectable { + open override func setValue(_ value: Any!, forUndefinedKey key: String) { + if let kvc = self as? EVGenericsKVC { + kvc.setGenericValue(value as AnyObject!, forUndefinedKey: key) + } else { + self.addStatusMessage(.IncorrectKey, message: "The class '\(EVReflection.swiftStringFromClass(self))' is not key value coding-compliant for the key '\(key)'") + print("\nWARNING: The class '\(EVReflection.swiftStringFromClass(self))' is not key value coding-compliant for the key '\(key)'\n There is no support for optional type, array of optionals or enum properties.\nAs a workaround you can implement the function 'setValue forUndefinedKey' for this. See the unit tests for more information\n") + + } + } +} + + +// And you could extend it with a property level iEqual or advanced description and debugDescription functions + +extension ExtendingNSObjectsMore { + + open override func isEqual(_ object: Any?) -> Bool { // for isEqual: + if let obj = object as? EVObject { + return EVReflection.areEqual(self, rhs: obj) + } + return false + } + + open override var description: String { + get { + return EVReflection.description(self, prettyPrinted: true) + } + } + + open override var debugDescription: String { + get { + return EVReflection.description(self, prettyPrinted: true) + } + } +} + + +// If you do want NSCoding support, then you need to create it in your own class. You could make this generic for all your objects by putting it in a base class. + +public class ExtendingNSObjectsCoding: ExtendingNSObjectsMore, NSCoding { + public convenience required init?(coder: NSCoder) { + self.init() + EVReflection.decodeObjectWithCoder(self, aDecoder: coder, conversionOptions: .DefaultNSCoding) + } + + open func encode(with aCoder: NSCoder) { + EVReflection.encodeWithCoder(self , aCoder: aCoder, conversionOptions: .DefaultNSCoding) + } +} + + + diff --git a/UnitTests/EVReflectionTests/EVReflectionTests.swift b/UnitTests/EVReflectionTests/EVReflectionTests.swift index 41d64dca..7fa6c6eb 100644 --- a/UnitTests/EVReflectionTests/EVReflectionTests.swift +++ b/UnitTests/EVReflectionTests/EVReflectionTests.swift @@ -479,6 +479,7 @@ class EVReflectionTests: XCTestCase { } + class A81: EVObject { var openId: String = "" } diff --git a/UnitTests/EVReflectionTests/EVReflectionWorkaroundsTests.swift b/UnitTests/EVReflectionTests/EVReflectionWorkaroundsTests.swift index b01f109e..d399faa0 100644 --- a/UnitTests/EVReflectionTests/EVReflectionWorkaroundsTests.swift +++ b/UnitTests/EVReflectionTests/EVReflectionWorkaroundsTests.swift @@ -121,7 +121,7 @@ class EVReflectionWorkaroundsTests: XCTestCase { } -@objc enum StatusType: Int, EVRaw { +enum StatusType: Int, EVRaw { case notOK = 0 case ok = 1 } diff --git a/UnitTests/EVReflectionTests/WorkaroundEnumTest.swift b/UnitTests/EVReflectionTests/WorkaroundEnumTest.swift index 64f679d8..0e8ea214 100644 --- a/UnitTests/EVReflectionTests/WorkaroundEnumTest.swift +++ b/UnitTests/EVReflectionTests/WorkaroundEnumTest.swift @@ -51,7 +51,7 @@ class EnumWorkaroundsTests: XCTestCase { XCTAssertTrue(varTest5.associated.label == "ok", "Could nog get the rawvalue using a generic function") XCTAssertTrue(test5 == 3, "Could nog get the associated value using a generic function") let test6 = getRawValue(MyEnumFive.ok) - XCTAssertTrue(test6 as? String == "ok", "So we could get the raw value? Otherwise this would succeed") + XCTAssertTrue(test6 as? String == "ok", "So we could get the raw value? Otherwise this would fail") } func testArrayNullable() { diff --git a/UnitTests/UnitTests.xcodeproj/project.pbxproj b/UnitTests/UnitTests.xcodeproj/project.pbxproj index a579f8ac..2e7d6d26 100644 --- a/UnitTests/UnitTests.xcodeproj/project.pbxproj +++ b/UnitTests/UnitTests.xcodeproj/project.pbxproj @@ -96,6 +96,7 @@ 7F7663391DA922FB00740ACA /* EVReflectionIssue114b.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */; }; 7F76633A1DA922FB00740ACA /* EVReflectionIssue114b.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */; }; 7F76633B1DA922FC00740ACA /* EVReflectionIssue114b.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */; }; + 7F7903CA1E7D4F760086D111 /* EVReflectionExtendingNSObjects.swif.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7903C91E7D4F750086D111 /* EVReflectionExtendingNSObjects.swif.swift */; }; 7F7BBC661C009EE90046F05E /* EVReflectionMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */; }; 7F7BBC671C00A22B0046F05E /* EVReflectionMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */; }; 7F83A6711D132F6100A4C508 /* EVReflectionIssue96.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F83A66F1D132F5400A4C508 /* EVReflectionIssue96.swift */; }; @@ -236,6 +237,7 @@ 7F734AB61E53AE5E007A5688 /* EVReflectionIssue159.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue159.swift; sourceTree = ""; }; 7F734AB81E53AF03007A5688 /* EVReflectionIssue159.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = EVReflectionIssue159.json; sourceTree = ""; }; 7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue114b.swift; sourceTree = ""; }; + 7F7903C91E7D4F750086D111 /* EVReflectionExtendingNSObjects.swif.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionExtendingNSObjects.swif.swift; sourceTree = ""; }; 7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = EVReflectionMappingTest.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 7F83A66F1D132F5400A4C508 /* EVReflectionIssue96.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue96.swift; sourceTree = ""; }; 7F8818F81C04447100E17072 /* EVReflectionPropertyMappingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionPropertyMappingTest.swift; sourceTree = ""; }; @@ -330,6 +332,7 @@ 7FDC390D1DC3582F0091D27E /* sample.json */, 7F41ECA41B91F2E0003A2236 /* TestObjects.swift */, 7F41ECA81B91F306003A2236 /* EVReflectionTests.swift */, + 7F7903C91E7D4F750086D111 /* EVReflectionExtendingNSObjects.swif.swift */, 7FA506E31E68C953003D83E7 /* EVReflectionCustomConverterTests.swift */, 7F59F9DA1C07999300C14CF9 /* EVReflectionEVObjectTests.swift */, 7F8818F81C04447100E17072 /* EVReflectionPropertyMappingTest.swift */, @@ -566,7 +569,7 @@ }; 7F3E31841BB9095C001E6788 = { CreatedOnToolsVersion = 7.0; - DevelopmentTeam = 8829GZ84Q6; + DevelopmentTeam = 443FV7F3ZK; ProvisioningStyle = Automatic; }; 7F9336B61C28227400AF7076 = { @@ -820,6 +823,7 @@ 7F663C321D014D7B00642499 /* EVReflectionIssue91.swift in Sources */, 7F1E77911CD3440000F88A9A /* EVReflectionConversionOptionsTest.swift in Sources */, 7F44151C1E2A84E00077F033 /* CKAsset+UIImage.swift in Sources */, + 7F7903CA1E7D4F760086D111 /* EVReflectionExtendingNSObjects.swif.swift in Sources */, 7F9930201E2426B500792CF1 /* AlamofireXml3Tests.swift in Sources */, 7F9801971E260A4D00D6C5C0 /* Owner.swift in Sources */, 7F41ECB01B91F306003A2236 /* ProfilePhotoUrlListTest.swift in Sources */, @@ -1106,7 +1110,7 @@ buildSettings = { CODE_SIGN_IDENTITY = "Mac Developer"; COMBINE_HIDPI_IMAGES = YES; - DEVELOPMENT_TEAM = 8829GZ84Q6; + DEVELOPMENT_TEAM = 443FV7F3ZK; INFOPLIST_FILE = "EVReflectionTests/osx-Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; @@ -1123,7 +1127,7 @@ buildSettings = { CODE_SIGN_IDENTITY = "Mac Developer"; COMBINE_HIDPI_IMAGES = YES; - DEVELOPMENT_TEAM = 8829GZ84Q6; + DEVELOPMENT_TEAM = 443FV7F3ZK; INFOPLIST_FILE = "EVReflectionTests/osx-Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10;