Swift "AnyClass" Type

TL;DR you're looking for MyClass.self When working with ConnectSDK, I came across something I hadn't before in Swift: the AnyClass type. ConnectSDK is an Objective-C based library so I'd imagine this is a rarity when working with native Swift code. Here's what it looks like: discoveryManager?.registerDeviceService(deviceClass: AnyClass!, withDiscovery: AnyClass!) I mistakenly though it was looking for an instance (i.e. MyClass()) but that threw an error. It turns out that in Objective-C each class has the static class property. In the case of ConnectSDK it's looking for the following: [AirPlayService class] [DIALService class] [CastService class] This…

Creating a sharedInstance in Swift

Or, "How the fuck to I make a singleton?" One of the patterns that seems to be very common in Cocoa is the sharedInstance pattern. You can see it in a few of the native classes to UIKit and Cocoa. For example: In UIApplication you havesharedApplication which returns the current application. You'll use it when wanting to display the network indicator for instance. On OS X NSNotificationCenter has the defaultCenter. NSUserNotificationCenter has the defaultUserNotificationCenter which again follows this principal. While the above may not share the same name, the all follow the same pattern and return an instance that is…