This task is to determine if there is only one instance of an application running. If the program discovers that an instance of it is already running, then it should display a message indicating that it is already running and exit.

import Foundation
 
let globalCenter = NSDistributedNotificationCenter.defaultCenter()
let time = NSDate().timeIntervalSince1970
 
globalCenter.addObserverForName("OnlyOne", object: nil, queue: NSOperationQueue.mainQueue()) {not in
    if let senderTime = not.userInfo?["time"] as? NSTimeInterval where senderTime != time {
        println("More than one running")
        exit(0)
    } else {
        println("Only one")
    }
}
 
func send() {
    globalCenter.postNotificationName("OnlyOne", object: nil, userInfo: ["time": time])
 
    let waitTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC))
 
    dispatch_after(waitTime, dispatch_get_main_queue()) {
        send()
    }
}
 
send()
CFRunLoopRun()