diff --git a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Fuse.xcscheme b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Fuse.xcscheme index f924cfd..af9f7a1 100644 --- a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Fuse.xcscheme +++ b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/Fuse.xcscheme @@ -29,8 +29,6 @@ shouldUseLaunchSchemeArgsEnv = "YES"> - - - - ])]() item.properties.forEach { property in - - let value = property.name + + let value = FuseUtilities.propertyStringValueUsingKey(property.name, instance: item) if let result = self.search(pattern, in: value) { let weight = property.weight == 1 ? 1 : 1 - property.weight @@ -505,7 +505,7 @@ extension Fuse { item.properties.forEach { property in - let value = property.name + let value = FuseUtilities.propertyStringValueUsingKey(property.name, instance: item) if let result = self.search(pattern, in: value) { let weight = property.weight == 1 ? 1 : 1 - property.weight diff --git a/Fuse/Classes/FuseUtilities.swift b/Fuse/Classes/FuseUtilities.swift index b33d6d9..919e597 100644 --- a/Fuse/Classes/FuseUtilities.swift +++ b/Fuse/Classes/FuseUtilities.swift @@ -83,4 +83,36 @@ class FuseUtilities { } return ranges } + + static func propertyStringValueUsingKey(_ key: String, instance: Any) -> String { + + // values containing periods Also have spaces. Here to support using value rather than key in FuseProperty + if(key.contains(" ")){ return key } + + var mirror = Mirror(reflecting: instance) + var propertyValue: Any = mirror.descendant(key) ?? key + // walk key path if dot notation is present + let keyFragments = key.components(separatedBy: ".") + // only do the work if there were key path fragments + if(keyFragments.count > 1){ + // iterate fragments + keyFragments.forEach{ keyFragment in + // retrieve property value + propertyValue = mirror.descendant(keyFragment) ?? "" + // reflect on property value + mirror = Mirror(reflecting: propertyValue) + // if optional, descendents aren't there ;-\ + if(mirror.displayStyle == .optional) { + // unwrap optional + if let some = mirror.children.first?.value { + // use the wrapped value + propertyValue = some + // reflect on the unwrapped value + mirror = Mirror(reflecting: propertyValue) + } + } + } + } + return propertyValue as? String ?? key + } } diff --git a/README.md b/README.md index b608a61..ae7b5b1 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,8 @@ struct Book: Fuseable { var properties: [FuseProperty] { return [ - FuseProperty(name: title, weight: 0.3), - FuseProperty(name: author, weight: 0.7), + FuseProperty(name: "title", weight: 0.3), + FuseProperty(name: "author", weight: 0.7), ] } }