Skip to content

Commit

Permalink
default implementation for customConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 18, 2017
1 parent 8ebf053 commit 29dc452
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file not shown.
9 changes: 9 additions & 0 deletions Source/EVReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}



1 change: 1 addition & 0 deletions UnitTests/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ class EVReflectionTests: XCTestCase {
}



class A81: EVObject {
var openId: String = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class EVReflectionWorkaroundsTests: XCTestCase {
}


@objc enum StatusType: Int, EVRaw {
enum StatusType: Int, EVRaw {
case notOK = 0
case ok = 1
}
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/EVReflectionTests/WorkaroundEnumTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 7 additions & 3 deletions UnitTests/UnitTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -236,6 +237,7 @@
7F734AB61E53AE5E007A5688 /* EVReflectionIssue159.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue159.swift; sourceTree = "<group>"; };
7F734AB81E53AF03007A5688 /* EVReflectionIssue159.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = EVReflectionIssue159.json; sourceTree = "<group>"; };
7F7663371DA922A300740ACA /* EVReflectionIssue114b.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue114b.swift; sourceTree = "<group>"; };
7F7903C91E7D4F750086D111 /* EVReflectionExtendingNSObjects.swif.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionExtendingNSObjects.swif.swift; sourceTree = "<group>"; };
7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = EVReflectionMappingTest.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
7F83A66F1D132F5400A4C508 /* EVReflectionIssue96.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue96.swift; sourceTree = "<group>"; };
7F8818F81C04447100E17072 /* EVReflectionPropertyMappingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionPropertyMappingTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -566,7 +569,7 @@
};
7F3E31841BB9095C001E6788 = {
CreatedOnToolsVersion = 7.0;
DevelopmentTeam = 8829GZ84Q6;
DevelopmentTeam = 443FV7F3ZK;
ProvisioningStyle = Automatic;
};
7F9336B61C28227400AF7076 = {
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 29dc452

Please sign in to comment.