Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Created July 29, 2012 07:51
Show Gist options
  • Save tonyarnold/3196558 to your computer and use it in GitHub Desktop.
Save tonyarnold/3196558 to your computer and use it in GitHub Desktop.
Add simple object subscripting to NSUserDefaults
//
// NSUserDefaults+ObjectSubscripting.h
//
// Created by Tony Arnold on 29/07/12.
// Copyright (c) 2012 The CocoaBots. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSUserDefaults (ObjectSubscripting)
- (id)objectForKeyedSubscript:(NSString *)key;
- (void)setObject:(id)object forKeyedSubscript:(NSString *)key;
@end
//
// NSUserDefaults+ObjectSubscripting.m
//
// Created by Tony Arnold on 29/07/12.
// Copyright (c) 2012 The CocoaBots. All rights reserved.
//
#import "NSUserDefaults+ObjectSubscripting.h"
@implementation NSUserDefaults (ObjectSubscripting)
- (id)objectForKeyedSubscript:(NSString *)key
{
return [self objectForKey:key];
}
- (void)setObject:(id)object forKeyedSubscript:(NSString *)key
{
if (CBIsEmpty(object)) {
[self removeObjectForKey:key];
} else {
[self setObject:object forKey:key];
}
}
@end
@casademora
Copy link

CBIsEmpty is defined where? I'm sure it's simple, but still... :)

@tonyarnold
Copy link
Author

Ahh, sorry @casademora — there's a version of it here: https://gist.github.com/2010649

I'll post an updated version later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment