在Objective-C中使用Singleton类方法

在Objective-C中使用Singleton类方法,objective-c,singleton,Objective C,Singleton,当使用singleton时,我反复遇到相同的代码,比如: [[SomeManager sharedInstance]stringForKey:@“someKey”] 在我看来,只编写[SomeManager stringForKey:@“someKey”] 我当前使用的stringForKey方法是: - (NSString *)stringForKey:(NSString *)key { if (self.localeDictionary) { return [self

当使用singleton时,我反复遇到相同的代码,比如:

[[SomeManager sharedInstance]stringForKey:@“someKey”]

在我看来,只编写
[SomeManager stringForKey:@“someKey”]

我当前使用的
stringForKey
方法是:

- (NSString *)stringForKey:(NSString *)key {
    if (self.localeDictionary) {
        return [self stringByStrippingHTML:[self safeStringForKey:key]];
    } else return nil;
}
如果我只是将其更改为类方法,则会出现几个错误。我可以通过以下方式减轻所有错误:

+ (NSString *)stringForKey:(NSString *)key {
    SomeManager *theManager = [SomeManager sharedManager];
    if (theManager.localeDictionary) {
        return [theManager.localeDictionary stringByStrippingHTML:[theManager.localeDictionary safeStringForKey:key]];
    } else return nil;
}
我的问题是我是否做错了


编辑:使用局部变量更新类方法。

当然会出现错误,因为在类方法中,“self”不是实例,但是类MyClass的类方法中的类-[self which]与类方法[MyClass which]相同


我个人不喜欢用三行代码给[SomeManager sharedManager]打三个电话。不仅仅是因为它使调试成为一场噩梦。只需将sharedManager分配给一个局部变量

这没什么错,只要它不在代码的性能关键部分。使用局部变量更新了问题。这是一种比做
[[SomeManager sharedManager]stringForKey]更好还是更差的新方法