Home objective-c troubleshooting
Post
Cancel

objective-c troubleshooting

Singleton pattern in objc, how to keep init private?

+ (instancetype)shareInstance;

- (instancetype)init UNAVAILABLE_ATTRIBUTE;
+ (instancetype)new UNAVAILABLE_ATTRIBUTE;

+ (instancetype)shareInstance {
    static MyClass *shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[super allocWithZone:NULL] initPrivate];
    });
    return shareInstance;
}

- (instancetype)initPrivate {
    self = [super init];
    if (self) {

    }
    return self;
}

//  MARK: Rewrite
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [MyClass shareInstance];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

Singleton pattern in objc, how to keep init private?

#define SINGLETON_CLS_IMPL_BEGIN(ClsName, FactoryMethodName) \
@implementation ClsName \
+ (instancetype)FactoryMethodName { \
    static ClsName* _instance = nil; \
    static dispatch_once_t token; \
    dispatch_once(&token, ^{ \
    _instance = [[super allocWithZone:NULL] initPrivate]; \
    }); \
    return _instance; \
} \
\
-(instancetype)initPrivate {\
    self =  [super init]; \
    return self; \
} \
\
+ (id)allocWithZone:(struct _NSZone *)zone { \
    return [ClsName FactoryMethodName]; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
    return self; \
} \

#define SINGLETON_CLS_INTERFACE_BEGIN(ClsName, FactoryMethodName) \
@interface ClsName : NSObject \
- (instancetype)init UNAVAILABLE_ATTRIBUTE; \
+ (instancetype)new UNAVAILABLE_ATTRIBUTE; \
+ (instancetype)FactoryMethodName; \

Linker error using extern “C” in Objective-C code

Extern C functions in Objective-c

Ask Question

1
2
3
4
5
6
7
#if !defined(__cplusplus)
#define MONExternC extern
#else
#define MONExternC extern "C"
#endif
// declare loadMeshFromFile
MONExternC void loadMeshFromFile(char const*, void*);

[What is Objective C++? closed]

Compile with: g++ -x objective-c++ -framework Foundation Hello.mm -o hello

References

https://web.archive.org/web/20101203170217/http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html

This post is licensed under CC BY 4.0 by the author.