lib/sugarcube/nsuserdefaults.rb in sugarcube-0.11.3 vs lib/sugarcube/nsuserdefaults.rb in sugarcube-0.12
- old
+ new
@@ -6,11 +6,44 @@
self.standardUserDefaults.objectForKey(key.to_s)
end
# Sets the value for a given key and save it right away.
def []=(key, val)
- self.standardUserDefaults.setObject(val, forKey: key.to_s)
+ self.standardUserDefaults.setObject(val.to_nsuserdefaults, forKey: key.to_s)
self.standardUserDefaults.synchronize
end
+
+ def remove(key)
+ self.standardUserDefaults.removeObjectForKey(key)
+ self.standardUserDefaults.synchronize
+ end
end
+end
+
+
+class Object
+ def to_nsuserdefaults
+ self
+ end
+end
+
+
+class NilClass
+ def to_nsuserdefaults
+ false
+ end
+end
+
+
+class NSArray
+ def to_nsuserdefaults
+ self.map { |val| val.to_nsuserdefaults }
+ end
+end
+
+
+class NSDictionary
+ def to_nsuserdefaults
+ self.each_with_object({}) { |(key, val), h| h[key] = val.to_nsuserdefaults }
+ end
end