Skip to content

Instantly share code, notes, and snippets.

@joelparsons
Created July 29, 2012 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelparsons/3195445 to your computer and use it in GitHub Desktop.
Save joelparsons/3195445 to your computer and use it in GitHub Desktop.
Category on NSCoder to support the objective-c subscript syntax
//
// NSCoder+subscript.h
//
// Created by Joel Parsons on 29/07/2012.
// Copyright (c) 2012 All rights reserved.
//
//This category adds objective-c subscript support to NSCoder for basic object types
//handy for implementing NSCoding.
//without this category:
//[anEncoder encodeObject:object forKey:key];
//object = [aDecoder objectForKey:key];
//with this category:
//anEncoder[key] = object;
//object = aDecoder[key];
#import <Foundation/Foundation.h>
@interface NSCoder (subscript)
- (id)objectForKeyedSubscript:(NSString *)key;
- (void)setObject:(id)object forKeyedSubscript:(NSString *)aKey;
@end
//
// NSCoder+subscript.m
//
// Created by Joel Parsons on 29/07/2012.
// Copyright (c) 2012 All rights reserved.
//
#import "NSCoder+subscript.h"
@implementation NSCoder (subscript)
-(id)objectForKeyedSubscript:(NSString *)key{
return [self decodeObjectForKey:key];
}
-(void)setObject:(id)object forKeyedSubscript:(NSString *)aKey{
[self encodeObject:object forKey:aKey];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment