Installing OS X El Capitan on Parallels VM

Want to install El Capitan, but not on your main machine?  This was really helpful for me in setting it up.

A cautionary tale about singletons and frameworks.

There is a pattern for creating singletons in Objective C.  It is extremely quick, thread-safe and allows only one instance of an object to be created.
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static NSObject *_sharedInstance;
    dispatch_once(&onceToken, ^{
        _sharedInstance =
            [[NSObject alloc] init];
    return _sharedInstance;
dispatch_once_t is an 64 bit integer and  dispatch_once appears to be an OSAtomicCompareAndSwap64Barrier call to guard the execution of the block.


So, if init is made to always return nil and another private initializer is used, then only this class function will be used and there will only ever be one instance of _sharedInstance.


Turns out, there is a way to thwart this though.  onceToken will be added to each compilation unit that includes it.  Normally though if you have the same class linked into a program twice you will get a linker error.  There is one exception to this and that is XCTest.  When you build a unit test target for iOS, it is injected into the application that you are testing.  So if the same source code file is in your application target and your unit test target, there will be two onceToken variables.


At runtime, it is nondeterministic as to which is used, so you can end up creating multiple _sharedInstance objects.  So, don’t include files in your application AND your test targets.


Well, there is one more way to do this.  I’ve been building a static lib to support iOS7.  Now that the requirement to support it has been dropped and I only need to support iOS8 or later, I can use frameworks.  My project currently uses both.  I haven’t completed the transition yet.  It isn’t trivial and it hasn’t been a priority.  My application is using the framework and I’ve tested it with other applications as well.  However my unit tests are still using the static library.  Both the framework and the library are creating instances of onceToken.  When I run the unit tests, it injects my unit test code, along with the library, into the application that include the framework.  So when I run unit tests, dispatch_once is called twice.


That was fun tracking down.