Skip to content

Instantly share code, notes, and snippets.

@owenzhao
owenzhao / queue.swift
Created January 15, 2017 12:57
queue
@IBAction func printHello(_ sender: Any) {
// 队列,以消息绑定为例
if shouldWaitSwitch.isOn {
while true {
let isSuccess = WaitingTaskQueue.append(task: "你好!")
if isSuccess { break } else { continue }
}
if let _ = shouldWaitSwitch.actions(forTarget: WaitingTaskQueue.self, forControlEvent: .valueChanged) { // already set
return
@owenzhao
owenzhao / notification.swift
Last active January 15, 2017 12:56
notification
@IBAction func printHello(_ sender: Any) {
// 消息绑定
if shouldWaitSwitch.isOn {
if let _ = shouldWaitSwitch.actions(forTarget: self, forControlEvent: .valueChanged) { // already set
return
}
shouldWaitSwitch.addTarget(self, action: #selector(printHello(_:)), for: .valueChanged)
}
else {
@owenzhao
owenzhao / delay.swift
Last active January 15, 2017 12:55
delay
@IBAction func printHello(_ sender: Any) {
// 延迟
if shouldWaitSwitch.isOn {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { [unowned self] (timer) in
if self.shouldWaitSwitch.isOn { return }
print("你好!")
timer.invalidate()
})
}
@owenzhao
owenzhao / polling.swift
Created January 15, 2017 12:53
polling
@IBAction func printHello(_ sender: Any) {
// 忙等待
DispatchQueue(label: "default").async {
while true {
if self.shouldWaitSwitch.isOn { continue }
print("你好!")
break
}
}
@owenzhao
owenzhao / MenuAction.swift
Created November 23, 2016 10:24
Menu Actions
//MARK: - Help Menu
extension NSApplication {
@IBAction func contactDeveloper(_ sender: Any) {
let mailAddress = "your email address"
let mailBody = NSLocalizedString("Please use Chinese or English in your mail, if you can.", comment: "mail body")
let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = [mailAddress]
service.perform(withItems: [mailBody])
}
}
@owenzhao
owenzhao / progressIndicatorRunLoopPause.swift
Created November 1, 2016 14:54
stop main runloop to change the running order of progress indicator
func userDefaultsDidChange() {
guard let controller = tableViewController else { return }
let progressIndicator = { () -> NSProgressIndicator in
let frame = view.frame
let x = (frame.width - 50) / 2
let y = (frame.height - 50) / 2
let piFrame = NSMakeRect(x, y, 50, 50)
let pi = NSProgressIndicator(frame: piFrame)
pi.style = .spinningStyle
@owenzhao
owenzhao / openFilesDelay.swift
Last active November 1, 2016 15:17
Use delay to gather files
var rawFilenames = [String]()
// instance counter
var timer:NSTimer! = nil
func application(sender: NSApplication, openFiles filenames: [String]) {
if shouldQuitAppAfterConvert == nil {
shouldQuitAppAfterConvert = true
}
if timer != nil {
timer.invalidate()
@owenzhao
owenzhao / userDefaultsDelayByGCD.swift
Created November 1, 2016 13:45
use delay to run away from UserDefaults' bug
class TableViewController: NSViewController, NSTableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
run()
DispatchQueue.main.asyncAfter(wallDeadline: .now() + .milliseconds(100)) { [unowned self] () -> () in
NotificationCenter.default.post(name: TableViewControllerDidAppear, object: self)
}
}
@owenzhao
owenzhao / userDefaultsDelayByTimer.swift
Created November 1, 2016 13:38
use delay to run away from UserDefaults' bug
class TableViewController: NSViewController, NSTableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
run()
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(sendNotification), userInfo: nil, repeats: false)
}
func sendNotification() {
NotificationCenter.default.post(name: TableViewControllerDidAppear, object: self)
// in AppDelegate.swift
let TableViewControllerDidAppear = NSNotification.Name("TableViewController did Appear")
// in PreferencesViewController.swift
class PreferencesViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: UserDefaults.standard)
NotificationCenter.default.addObserver(self, selector: #selector(tableViewDidAppear(noti:)), name: TableViewControllerDidAppear, object: nil)