1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var bluetoothSwitch: NSButton! func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application let defaults = NSUserDefaults.standardUserDefaults() if (defaults.objectForKey("SwitchState") != nil) { bluetoothSwitch.state = defaults.integerForKey("SwitchState") } } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application } @IBAction func saveSwitchState(sender: NSButton) { let defaults = NSUserDefaults.standardUserDefaults() defaults.setInteger(bluetoothSwitch.state, forKey: "SwitchState") } |
NSUserDefaults的使用
发表回复