Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding shortcut methods for NSCoding compliant object #9

Merged
merged 2 commits into from

2 participants

@jfradj

I'm just (for now ;-) an iPhone coder, but with a little search it seems that NSArchiver is not used anymore since 10.2, so I think my code is right for MacOS too.

jfradj added some commits
@jfradj jfradj Adding shortcut methods for NSCoding compliant object 4580b2d
@jfradj jfradj unarchiveObjectWithData raise exception for null data
Must change a little - (id<NSCoding>)objectForKey:(NSString*)key method because it raises an exception if the cache is empty for a key
b88e557
@shnhrrsn shnhrrsn merged commit 018a217 into enormego:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2011
  1. @jfradj
  2. @jfradj

    unarchiveObjectWithData raise exception for null data

    jfradj authored
    Must change a little - (id<NSCoding>)objectForKey:(NSString*)key method because it raises an exception if the cache is empty for a key
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 0 deletions.
  1. +4 −0 EGOCache.h
  2. +19 −0 EGOCache.m
View
4 EGOCache.h 100644 → 100755
@@ -66,5 +66,9 @@
- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key;
- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
+- (id<NSCoding>)objectForKey:(NSString*)key;
+- (void)setObject:(id<NSCoding>)anObject forKey:(NSString*)key;
+- (void)setObject:(id<NSCoding>)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
+
@property(nonatomic,assign) NSTimeInterval defaultTimeoutInterval; // Default is 1 day
@end
View
19 EGOCache.m
@@ -276,6 +276,25 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi
}
#pragma mark -
+#pragma mark Object methods
+
+- (id<NSCoding>)objectForKey:(NSString*)key {
+ if([self hasCacheForKey:key]) {
+ return [NSKeyedUnarchiver unarchiveObjectWithData:[self dataForKey:key]];
+ } else {
+ return nil;
+ }
+}
+
+- (void)setObject:(id<NSCoding>)anObject forKey:(NSString*)key {
+ [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
+}
+
+- (void)setObject:(id<NSCoding>)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
+ [self setData:[NSKeyedArchiver archivedDataWithRootObject:anObject] forKey:key withTimeoutInterval:timeoutInterval];
+}
+
+#pragma mark -
#pragma mark Disk writing operations
- (void)performDiskWriteOperation:(NSInvocation *)invoction {
Something went wrong with that request. Please try again.