aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Framework/MASDictionaryTransformer.m15
-rw-r--r--Framework/MASDictionaryTransformerTests.m9
2 files changed, 20 insertions, 4 deletions
diff --git a/Framework/MASDictionaryTransformer.m b/Framework/MASDictionaryTransformer.m
index f0d9b2e..54a7d7a 100644
--- a/Framework/MASDictionaryTransformer.m
+++ b/Framework/MASDictionaryTransformer.m
@@ -11,12 +11,19 @@ static NSString *const MASModifierFlagsKey = @"modifierFlags";
return YES;
}
+// Storing nil values as an empty dictionary lets us differ between
+// “not available, use default value” and “explicitly set to none”.
+// See http://stackoverflow.com/questions/5540760 for details.
- (NSDictionary*) reverseTransformedValue: (MASShortcut*) shortcut
{
- return @{
- MASKeyCodeKey: @([shortcut keyCode]),
- MASModifierFlagsKey: @([shortcut modifierFlags])
- };
+ if (shortcut == nil) {
+ return [NSDictionary dictionary];
+ } else {
+ return @{
+ MASKeyCodeKey: @([shortcut keyCode]),
+ MASModifierFlagsKey: @([shortcut modifierFlags])
+ };
+ }
}
- (MASShortcut*) transformedValue: (NSDictionary*) dictionary
diff --git a/Framework/MASDictionaryTransformerTests.m b/Framework/MASDictionaryTransformerTests.m
index bd5c1db..48e11f3 100644
--- a/Framework/MASDictionaryTransformerTests.m
+++ b/Framework/MASDictionaryTransformerTests.m
@@ -20,4 +20,13 @@
@"Decoding a shortcut from an incomplete dictionary returns nil.");
}
+- (void) testNilRepresentation
+{
+ MASDictionaryTransformer *transformer = [MASDictionaryTransformer new];
+ XCTAssertEqualObjects([transformer reverseTransformedValue:nil], [NSDictionary dictionary],
+ @"Store nil values as an empty dictionary.");
+ XCTAssertNil([transformer transformedValue:[NSDictionary dictionary]],
+ @"Load empty dictionary as nil.");
+}
+
@end