Skip to content

Commit

Permalink
Realm fixes for #252
Browse files Browse the repository at this point in the history
  • Loading branch information
Vermeer, Edwin authored and Vermeer, Edwin committed Dec 14, 2017
1 parent d00c72f commit d0a6687
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 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 = "5.3.1"
s.version = "5.4.0"
s.summary = "Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift"

s.description = <<-EOS
Expand Down
Binary file not shown.
29 changes: 15 additions & 14 deletions Source/Realm/RealmObjectExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ extension Object {
open override func setValue(_ value: Any!, forUndefinedKey key: String) {
if let kvc = self as? EVGenericsKVC {
kvc.setGenericValue(value as AnyObject!, forUndefinedKey: key)
} else {
if let current = super.value(forKey: key) {
if current is NSNumber, value is NSString {
//If we do have key and the types are different, then this is called from EVReflection
let num = NSNumber(value: Int("\(value as! NSString)") ?? 0)
super.setValue(num, forUndefinedKey: key)
} else if type(of: current) == type(of: value!)
|| (String(describing: type(of: current)).hasPrefix("List<") && value! is NSArray) {
super.setValue(value, forUndefinedKey: key)
} else {
(self as? EVReflectable)?.addStatusMessage(.InvalidType, message: "Invalid type for the key '\(key)' in the class '\(EVReflection.swiftStringFromClass(self))'")
evPrint(.IncorrectKey, "WARNING: the type (\(type(of: current)) of the The key '\(key)' in class '\(EVReflection.swiftStringFromClass(self))' does not corresponds to the type (\(type(of: value!)) in the json or Realm database.\n")
}
} else {
} else if let current = super.value(forKey: key) {
if current is NSNumber, value is NSString {
//If we do have key and the types are different, then this is called from EVReflection
let num = NSNumber(value: Int("\(value as! NSString)") ?? 0)
super.setValue(num, forUndefinedKey: key)
} else if type(of: current) == type(of: value)
|| (String(describing: type(of: current)).hasPrefix("List<") && value! is NSArray) {
super.setValue(value, forUndefinedKey: key)
} else {
(self as? EVReflectable)?.addStatusMessage(.InvalidType, message: "Invalid type for the key '\(key)' in the class '\(EVReflection.swiftStringFromClass(self))'")
evPrint(.IncorrectKey, "WARNING: the type (\(type(of: current)) of the The key '\(key)' in class '\(EVReflection.swiftStringFromClass(self))' does not corresponds to the type (\(type(of: value!)) in the json or Realm database.\n")
}
} else if objectSchema.properties.first(where: { $0.name == key }) != nil {
super.setValue(value, forUndefinedKey: key)
} else {
(self as? EVReflectable)?.addStatusMessage(.IncorrectKey, message: "The class '\(EVReflection.swiftStringFromClass(self))' is not key value coding-compliant for the key '\(key)'")
evPrint(.IncorrectKey, "WARNING: The class '\(EVReflection.swiftStringFromClass(self))' is not key value coding-compliant for the key '\(key)'\n❓ This could be a strange Realm List issue where the key is reported undefined but it's still set.\n")
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Source/Realm/RealmOptionalEVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ extension RealmOptional : EVCustomReflectable {
- parameter value: The dictionary that will be converted to an object
*/
public func constructWith(value: Any?) {
self.value = value.map(dynamicBridgeCast)
if value is NSNull {
self.value = nil
} else {
self.value = value.map(dynamicBridgeCast)
}
}

/**
Expand Down

0 comments on commit d0a6687

Please sign in to comment.