[英]How to convert this C++ code to Objective C
What would be equivalent Objective C for this:为此,什么是等效的目标 C:
template<class T>
class Singleton
{
public:
static T * instance()
{
static T t;
return &t;
}
private:
Singleton();
~Singleton();
};
While calling:调用时:
friend class Singletone<MyManagerClass>;
Objective-C does not provide templates and templates do not work with Objective-C classes when compiled as Objective-C++. Objective-C 不提供模板,并且模板在编译为 Objective-C++ 时不适用于 Objective-C 类。 It is not always possible to have a 1-to-1 conversion between two programming languages.
两种编程语言之间并不总是可以进行一对一的转换。 For example, Objective-C does not provide compile-time enforced private methods at all, so there is no way to exactly implement what you want.
例如,Objective-C 根本不提供编译时强制私有方法,因此无法准确实现您想要的。
The same goes the other way, Objective-C has features that are not available in C++, such as the @encode()
directive.反之亦然,Objective-C 具有 C++ 中没有的特性,例如
@encode()
指令。
Perhaps related to this topic: it is important to understand that design patterns that work well with C++ do not necessarily work well—or at all—with Objective-C.可能与这个主题有关:重要的是要理解适用于 C++ 的设计模式不一定适用于 - 或根本不适用于 Objective-C。
What is the goal of your design of being able to singletonise any class?您设计的能够单例化任何类的目标是什么? Perhaps there is a more established Objective-C way to acheive what you want.
也许有一种更成熟的 Objective-C 方式来实现你想要的。 There may also be a way to implement something similar using preprocessor macros, but you don't want to go down that path.
也可能有一种方法可以使用预处理器宏来实现类似的东西,但您不想走这条路。
Oh thats Pretty easy!哦,那很容易!
@interface YourClass : NSObject {}
+ (YourClass *)singleton;
@end
static YourClass *singletonVar = nil;
@implementation YourClass
+ (YourClass *)singleton
{
if( ! singletonVar ) {
singletonVar = [[self alloc] init];
}
return singletonVar;
}
@end
Now you can call [[YourClass singleton] doSomething] anywhere!现在你可以在任何地方调用 [[YourClass singleton] doSomething] 了! :D Just like you do [[NSUserDefaults standUserDefaults] valueForKey:@"Burp"] and such, do note that this ads overhead to your app, so when you use the singleton more than once in a scope consider storing it in a pointer for performance.
:D 就像您所做的 [[NSUserDefaults standUserDefaults] valueForKey:@"Burp"] 等,请注意此广告对您的应用程序的开销,因此当您在范围内多次使用单例时,请考虑将其存储在指针中表现。
#define SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)instance { \
@synchronized(self) { \
if (shared##classname == nil) { \
[[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone { \
\
@synchronized(self) { \
if (shared##classname == nil) { \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone { \
\
return self; \
} \
\
- (id)retain { \
\
return self; \
} \
\
- (NSUInteger)retainCount { \
\
return UINT_MAX; \
} \
\
- (void)release { \
\
} \
\
- (id)autorelease { \
\
return self; \
} \
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.