aboutsummaryrefslogtreecommitdiffstats
path: root/Framework
diff options
context:
space:
mode:
authorTomáš Znamenáček2014-08-07 11:35:45 +0200
committerTomáš Znamenáček2015-01-07 15:42:22 +0100
commitbbf2f69da4253b9fc28bd622a3041622f064d22c (patch)
treee73b5c0c1a6bfbe481ff904829a45a49375d608d /Framework
parentd687545083839db1218ab874f59a7290d516b46b (diff)
downloadMASShortcut-bbf2f69da4253b9fc28bd622a3041622f064d22c.tar.bz2
Better treatment of nil values when storing shortcuts as dictionaries.
This makes it possible to make a difference between “shortcut not set, use default” and “shortcut set to none”.
Diffstat (limited to 'Framework')
-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