dhr*_*hrm 4 inheritance objective-c class-variables subclassing
我的iOS应用程序中有以下类(它类似于Java世界中的抽象类).
@implementation WSObject
static NSDictionary* _dictionary = nil;
+(NSDictionary*) dictionary {
if (_dictionary == nil) {
_dictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:[self localStorePath]];
}
return _dictionary;
}
...
@end
Run Code Online (Sandbox Code Playgroud)
然后,我有多个类,WSObject
使用类方法实现上述dictionary
.问题是,这些类中的每一个都应该有自己的类_dictionary
,但它们都是从超类共享同一个对象.当然,我可以复制到所有子类,但这会破坏可重用性.除了这个getter之外,还有其他类方法WSObject
可以改变字典.因此,每个子类中都应该有几个类方法.
我怎样才能以聪明的方式解决这个问题?如果我的描述不充分,请告诉我.
联想参考似乎他们会做的伎俩.您实际上可以在类对象本身上添加一些存储空间.(我在NSString
这里使用s代替你想要使用的词典,仅用于演示.)
超类:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Stuper : NSObject
// Accessor method for the "class variable"
+ (NSString *) str;
// Analog to your +localStorePath
+ (NSString *) quote;
@end
Run Code Online (Sandbox Code Playgroud)
#import "Stuper.h"
// The doc suggests simply using the address of a static variable as the key.
// This works fine, even though every class is (as in your problem) using
// the same key, because we are associating to a different class each time.
static char key;
@implementation Stuper
+ (NSString *) str {
NSString * s = objc_getAssociatedObject(self, &key);
if( !s ){
s = [self quote];
// You'll probably want to use OBJC_ASSOCIATION_RETAIN for your dictionary.
// self inside a class method is the class object; use that as
// the associator. The string is now tied to the associator, i.e.,
// has the same lifetime.
objc_setAssociatedObject(self, &key, s, OBJC_ASSOCIATION_COPY);
}
return s;
}
+ (NSString *) quote {
return @"It was the best of times, it was the worst of times.";
}
@end
Run Code Online (Sandbox Code Playgroud)
子类:
#import "Stuper.h"
@interface Stub : Stuper @end
Run Code Online (Sandbox Code Playgroud)
#import "Stub.h"
@implementation Stub
+ (NSString *) quote {
return @"Call me Ishmael.";
}
@end
Run Code Online (Sandbox Code Playgroud)
尝试这个:
#import <Foundation/Foundation.h>
#import "Stuper.h"
#import "Stub.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"%@", [Stuper str]);
NSLog(@"%@", [Stub str]);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每个类对象现在都有自己的字符串,与之关联.
2011-12-05 23:11:09.031 SubClassVariables [36254:903]这是最好的时期,这是最糟糕的时期.
2011-12-05 23:11:09.034 SubClassVariables [36254:903]叫我以实玛利.
这里唯一的缺点是你每次想要对象时都必须调用accessor方法; 你没有可以直接使用的指针.objc_getAssociatedObject
当然,您也可以将超类作为访问者调用,因为它可以访问key
.
为了给每个子类提供自己的字典,使用类名作为键在主字典中存储第二个字典对象.例如:
static NSMutableDictionary *_dictionary = nil;
+ (NSDictionary*)dictionary
{
if (_dictionary == nil)
_dictionary = [[NSKeyedUnarchiver unarchiveObjectWithFile:[self localStorePath]] mutableCopy];
NSString *key = NSStringFromClass( [self class] );
if ( [_dictionary objectForKey:key] == nil )
[_dictionary setObject:[NSMutableDictionary dictionary] forKey:key];
return [_dictionary objectForKey:key];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1078 次 |
最近记录: |