Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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