请看以下类别...据我所知,不能将实例变量添加到类别中。
。H
//
// Created by macbook on 17.01.13.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import <Foundation/Foundation.h>
@interface NSMutableArray (Cache)
@property (nonatomic, strong) NSArray *backup;
- (void)restoreBackup;
@end
.m
//
// Created by macbook on 17.01.13.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "NSMutableArray+Cache.h"
#import <objc/runtime.h>
#define CEBackupKey @"Backup"
@implementation NSMutableArray (Cache)
#pragma mark - Properties
- (NSArray *)backup {
return objc_getAssociatedObject(self, CEBackupKey);
}
- (void)setBackup:(NSArray *)backup {
objc_setAssociatedObject(self, CEBackupKey, backup, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - Methods
- (void)restoreBackup {
[self removeAllObjects];
[self addObjectsFromArray:self.backup];
}
@end
任何人都可以详细解释这里发生了什么..
并且这种存储备份的方式会占用更少的内存,而不是拥有该数组的单独深层副本......?