DPTransparentEdgesTableView (Swift and Objective-C implementation) for iOS - Cocoa Controls
Table and scroll view with transparent top and bottom edges. Written in Objective-C and Swift
Get Started for FREE
Sign up with Facebook Sign up with X
I don't have a Facebook or a X account
Table and scroll view with transparent top and bottom edges. Written in Objective-C and Swift
A UIButton subclass with a custom, built-in activity indicator
Back in February I mentioned an interesting library providing navigation bar buttons with slick animations between them. Here's a nice tutorial showing how to create a slick transition between two ...
One of the issues you'll notice when using Swift is that native key-value-observing is not available and only works with NSObject subclasses. Swift is changing rapidly and a solution does appear to...
An Android Wear style animated confirmation view for iOS - Written in Swift
App development training for programmers and designers, mobile development services for companies of any size, and best selling programming guides.
This GitHub repo is a minimum viable implementation of a UITableView in Swift. Here’s a swift (badum, tish) tutorial on creating a UITableView …
In this tutorial learn how to integrate Facebook Login into your iOS Swift project using a Bridging Header.
A couple of weeks ago I mentioned a library providing a a nice clean syntax for working with JSON in Swift. Here's a Swift library that wraps NSURLSession providing a simplifed syntax called SwiftH...
In this tutorial learn how to communicate with a web API from your iOS Swift project.
I've mentioned a number of libraries for working with Core Data, the most popular of which being Magical Record. Here's an open source Swift based library providing a very nice syntax for querying ...
Late last year I mentioned a nice library for creating custom alert views that are styled similar to the native iOS alert views, but adding in nice features such as support for images, and extra bu...
Singletons in Swift
In this post, I will to demonstrate one particular way of creating singletons in Swift. But, before starting, let me just say: Swift is a very powerful programming language that allows developers to construct the same functionality in multiple ways. Therefore, the following example is just one way of building a singleton in Swift.
Generally, I discourage the use of singletons, as instantiating an object that will last forever is not good design. Instead, I prefer letting ARC do the memory management and letting ARC decide when to release an object or keep it alive. Additionally, there’s always an alternative way to build what you are trying to do with a singleton.
Nonetheless, despite my recommendations, I will demonstrate a way of building a singleton or a shared object in Swift, just in case you absolutely need one.
Let’s suppose that you need a shared instance of a class Monitor to continuously monitor the status of a running algorithm in your application. The Monitor class will be a subclass of NSObject and you want to instantiate this class only once. Here’s how to build this class in Swift:
import Foundation
class Monitor : NSObject {
class func sharedMonitor() -> Monitor {
return _sharedMonitor
}
}
var _sharedMonitor: Monitor = { Monitor() }()
In the above source code, first, I imported the Foundation module to be able to subclass NSObject. Then, I created a class Monitor subclassing NSObject. The class contains a class function sharedMonitor. This function doesn’t take any input arguments and returns a class type Monitor. The _sharedMonitor variable is defined as a global variable and I initialized it to a closure that simply instantiated the Monitor class.
But, how can you use it? Well, suppose you need to reference this singleton from another object. You can write:
let monitor = Monitor.sharedMonitor()
Now, let’s build a small example. Using Xcode 6 (currently in beta), create a Single View Application and name the project Singleton. Choose Swift as the programming language and save the project wherever you like. Then, add a new class to the project. Choose iOS -> Source -> Cocoa Touch Class. Name the class Monitor and make it a subclass of NSObject. Choose Swift as the language and save the class.
Next, open the Monitor.swift file and modify it as I previously described:
import Foundation
class Monitor: NSObject {
class func sharedMonitor() -> Monitor {
return _sharedMonitor
}
}
var _sharedMonitor : Monitor = { Monitor() }()
Then, open the ViewController.swift file and in the -viewDidLoad method create two constants of type Monitor and check that they are identical:
let monitor1 = Monitor.sharedMonitor()
let monitor2 = Monitor.sharedMonitor()
if monitor1 === monitor2 {
println(
About a year and a half ago I mentioned a library for asynchronous flow control in Objetive-C called Sequencer. Sequencer definitely helps to make your asynchronous code more readable and clean. He...
Apple has done an excellent job with the documenting the Swift programming language and I've been asked a couple of times about tutorials and so far by the best are on Apple's own Swift Progra...
Check out the first draft of our official Swift style guide - and submit your comments!
I've mentioned a number of custom iOS alert view components, most recently AMSmoothAlert which features a clean style and nice popping animations. Here's an open source component created with Swift...
Yesterday I mentioned a component allowing you to easily replicate the animated view bending effect as seen in the Skype app. Here's another project created to show how to replicate the effect of a...
Last week I mentioned a nice library for working with JSON in Swift as using the JSON parsing API's in the iOS SDK can become tedious. Documentation generation in Swift is another issue to be tackl...
A Swift based reimplementation of the Apple HUD (Volume, Ringer, Rotation,…) for iOS 8.