Last active
December 23, 2015 12:59
-
-
Save sora0077/6638739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (NSUserDefaults) | |
@property BOOL defaultsBool; | |
@property NSInteger defaultsInteger; | |
@property float defaultsFloat; | |
@property double defaultsDouble; | |
@property id defaultsObject; | |
@property NSData *defaultsData; | |
@property NSString *defaultsString; | |
@property NSURL *defaultsURL; | |
@property NSArray *defaultsArray; | |
@property NSDictionary *defaultsDictionary; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSString+NSUserDefaults.h" | |
#import "user_defaults.h" | |
@implementation NSString (NSUserDefaults) | |
- (BOOL)defaultsBool | |
{ | |
return NSUserDefaultsReadBool(self); | |
} | |
- (void)setDefaultsBool:(BOOL)defaultsBool | |
{ | |
NSUserDefaultsWriteBool(self, defaultsBool); | |
} | |
- (NSInteger)defaultsInteger | |
{ | |
return NSUserDefaultsReadInteger(self); | |
} | |
- (void)setDefaultsInteger:(NSInteger)defaultsInteger | |
{ | |
NSUserDefaultsWriteInteger(self, defaultsInteger); | |
} | |
- (float)defaultsFloat | |
{ | |
return NSUserDefaultsReadFloat(self); | |
} | |
- (void)setDefaultsFloat:(float)defaultsFloat | |
{ | |
NSUserDefaultsWriteFloat(self, defaultsFloat); | |
} | |
- (double)defaultsDouble | |
{ | |
return NSUserDefaultsReadDouble(self); | |
} | |
- (void)setDefaultsDouble:(double)defaultsDouble | |
{ | |
NSUserDefaultsWriteDouble(self, defaultsDouble); | |
} | |
- (id)defaultsObject | |
{ | |
return NSUserDefaultsReadObject(self); | |
} | |
- (void)setDefaultsObject:(id)defaultsObject | |
{ | |
NSUserDefaultsWriteObject(self, defaultsObject); | |
} | |
- (NSData *)defaultsData | |
{ | |
id obj = self.defaultsObject; | |
return [obj isKindOfClass:[NSData class]] ? obj : nil; | |
} | |
- (void)setDefaultsData:(NSData *)defaultsData | |
{ | |
self.defaultsObject = defaultsData; | |
} | |
- (NSString *)defaultsString | |
{ | |
id obj = self.defaultsObject; | |
return [obj isKindOfClass:[NSString class]] ? obj : nil; | |
} | |
- (void)setDefaultsString:(NSString *)defaultsString | |
{ | |
self.defaultsObject = defaultsString; | |
} | |
- (NSURL *)defaultsURL | |
{ | |
return NSUserDefaultsReadURL(self); | |
} | |
- (void)setDefaultsURL:(NSURL *)defaultsURL | |
{ | |
NSUserDefaultsWriteURL(self, defaultsURL); | |
} | |
- (NSArray *)defaultsArray | |
{ | |
id obj = self.defaultsObject; | |
return [obj isKindOfClass:[NSArray class]] ? obj : nil; | |
} | |
- (void)setDefaultsArray:(NSArray *)defaultsArray | |
{ | |
self.defaultsObject = defaultsArray; | |
} | |
- (NSDictionary *)defaultsDictionary | |
{ | |
id obj = self.defaultsObject; | |
return [obj isKindOfClass:[NSDictionary class]] ? obj : nil; | |
} | |
- (void)setDefaultsDictionary:(NSDictionary *)defaultsDictionary | |
{ | |
self.defaultsObject = defaultsDictionary; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef user_defaults_h | |
#define user_defaults_h | |
#import <Foundation/Foundation.h> | |
#define NSUserDefaults_Write(aKey, anObject, method) \ | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\ | |
if (anObject) {\ | |
[defaults method:anObject forKey:aKey];\ | |
} else {\ | |
[defaults removeObjectForKey:aKey];\ | |
}\ | |
[defaults synchronize] | |
#define NSUserDefaults_Read(aKey, method) \ | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\ | |
return [defaults method:aKey] | |
static inline void NSUserDefaultsWriteObject(id aKey, id anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setObject); | |
} | |
static inline void NSUserDefaultsWriteURL(id aKey, NSURL *anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setURL); | |
} | |
static inline void NSUserDefaultsWriteBool(id aKey, BOOL anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setBool); | |
} | |
static inline void NSUserDefaultsWriteInteger(id aKey, NSInteger anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setInteger); | |
} | |
static inline void NSUserDefaultsWriteFloat(id aKey, float anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setFloat); | |
} | |
static inline void NSUserDefaultsWriteDouble(id aKey, double anObject) | |
{ | |
NSUserDefaults_Write(aKey, anObject, setDouble); | |
} | |
static inline id NSUserDefaultsReadObject(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, objectForKey); | |
} | |
static inline id NSUserDefaultsReadURL(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, URLForKey); | |
} | |
static inline BOOL NSUserDefaultsReadBool(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, boolForKey); | |
} | |
static inline NSInteger NSUserDefaultsReadInteger(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, integerForKey); | |
} | |
static inline float NSUserDefaultsReadFloat(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, floatForKey); | |
} | |
static inline double NSUserDefaultsReadDouble(id aKey) | |
{ | |
NSUserDefaults_Read(aKey, doubleForKey); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment