-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSObject+YMOptionsAndDefaults.m
30 lines (23 loc) · 1.16 KB
/
NSObject+YMOptionsAndDefaults.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Created by YangMeyer on 08.10.12.
// Copyright (c) 2012 Yang Meyer. All rights reserved.
#import "NSObject+YMOptionsAndDefaults.h"
#import <objc/runtime.h>
@implementation NSObject (YMOptionsAndDefaults)
#define kYMStandardOptionsTableName @"YMStandardOptionsTableName"
#define kYMStandardDefaultsTableName @"YMStandardDefaultsTableName"
- (void)ym_registerOptions:(NSDictionary *)options
defaults:(NSDictionary *)defaults
{
NSParameterAssert(defaults);
objc_setAssociatedObject(self, (__bridge const void *)(kYMStandardOptionsTableName), options, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, (__bridge const void *)(kYMStandardDefaultsTableName), defaults, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)ym_optionOrDefaultForKey:(NSString*)optionKey
{
NSParameterAssert(optionKey);
NSDictionary *options = objc_getAssociatedObject(self, (__bridge const void *)(kYMStandardOptionsTableName));
NSDictionary *defaults = objc_getAssociatedObject(self, (__bridge const void *)(kYMStandardDefaultsTableName));
NSAssert(defaults, @"Defaults must have been set when accessing options.");
return options[optionKey] ?: defaults[optionKey];
}
@end