How do I add settings to my cocoa application in swift? - xcode

I'm pretty new to programming in Swift, and I'd like to know if there is an easy way to add settings/preferences to my Cocoa application in Swift. If possible, I'd like a step by step guide. I mostly want to know how you store the user's preferences on disk and the code part. In my current code it will need to check which setting the user has chosen, and based on that perform an action. I'm using Xcode 7.1 and Swift 2. Thanks in advance!

The NSUserDefaults class is very easy to use in code, and its shared instance is readily available for binding to controls in Interface Builder.
For example, if I wanted to have an integer preference named "elmer" and set its value to 7, it's as easy as:
NSUserDefaults.standardUserDefaults().setInteger(7, forKey: "elmer")
To read the value back:
let elmer: Int = NSUserDefaults.standardUserDefaults().integerForKey("elmer")
To bind the value to a control in Interface Builder, set the Controller Key to "values", and the preference name for the Model Key Path:
I would recommend reading the "Preferences and Settings Programming Guide", and also to familiar yourself with the "NSUserDefaults Class Reference".

SWITF 5.x
The class changed the name so now you do:
UserDefaults.standard.set("1234", forKey: "userID")
To set a key that can hold any type. Or you can be type specific like this
UserDefaults.standard.bool(forKey: "IsConfigured")
The UI binding still working in the same fashion #ElmerCat well explained.

Related

Accessing keyboard settings in MacOS via Swift

So here's what I'm trying to do:
I want to build a small application that places an icon in my top bar in MacOS. When clicking this icon, it should switch the setting "System Preferences > Keyboard > Use keys F1, F2, ... as standard function keys". So it basically works like Caffeine just for a different setting.
This is my current code (which is still in the test phase):
class AppDelegate: NSObject, NSApplicationDelegate {
func test() {
let defaults = NSUserDefaults.standardUserDefaults().dictionaryRepresentation()
for value in defaults.keys {
print(value + " - " + String(defaults[value]))
}
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
test()
}
}
The idea so far is to get the NSUserDefaults and get an overview of all they keys and the values stored in it. And here's my problem: How do I know which key I need to access the setting I want? I couldn't find any keys containing "keyboard" (I was expecting to see keys like "com.apple.keyboard.*").
I haven't found any documentation covering the range of possible keys to access certain settings.
If anyone could help me out I'd really appreciate it!
Thanks and have a good day!
Well, ironically I found it out just minutes after posting the question, but maybe this can help people to find other keys to access certain settings.
So first, the key I was looking for was "com.apple.keyboard.fnState".
Now here's how you can find keys you're looking for:
Before accessing the NSUserDefaults make sure to change the setting you try to manipulate at least once. This way you guarantee that an entry for this setting will be written to NSUserDefaults. And that's exactly why I wasn't able to find the key, because I never touched this setting (which could be seen as stupid considering what I'm trying to achieve, but that's up to you...).
On a side note: I still think it's pretty sad that Apple has no documentation whatsoever covering this topic.
Hope this helps others!

Cocoa Data Binding - One view object bound to two 'enabled' properties

Using Cocoa Data Binding I have bound a button's 'enabled' property to two separate 'Model Key Paths' (Enabled and Enabled2).
The idea is that the button will become enabled if either of these properties is true.
Unfortunately, it only works if both become true.
Can anyone help me change this logic from AND to OR?
Cool question-- I'm going to make a shot in the dark, I can't confirm it for you until tomorrow, but try this maybe?
Create a new property, enabledOrEnabled2 (maybe with a better name).
Override the getter (-(BOOL)enabledOrEnabled2) for this property,
to just return (Enabled || Enabled2)
Set the Key Path to be enabledOrEnabled2
I'm a little skeptical if this will work because Cocoa Bindings work by using KVC and KVO. So atm, your view controller is observing your Enabled and Enabled2 properties. What happens during runtime is that Cocoa will override your properties w/ their own KVO properties that look and act just like you think they would. Except the setters have been overridden, to send out notifications to observers.
I think the problem with my solution is that -(void)setEnabledOrEnabled2 will never be called, because you will only be setting your Enabled and Enabled2 properties. Therefore, the Cocoa overridden -(void)setEnabledOrEnabled2 will never notify your observing view controller
EDIT: Just read #stevesilva's comment, and didn't even know that dependent keys was a thing. Looks like you could definitely implement it this way if you wanted to
Actually, a first-thought hack would be to switch from overriding the getter (like I first recommended), and instead override the setters: -(void)setEnabled and -(void)setEnabled2, and add a line, something like: _enabledOrEnabled2 = _Enabled || _Enabled2. That way anytime you set your two BOOL properties, it will update enabledOrEnabled2, which will then send a notification out to it's observer, your view controller.
But now that I've written that out, I was wondering about the second part of Cocoa Bindings, KVC to modify the model whenever the view sees changes.
And because you are actually using Bindings on an Enabled state-- I actually don't think you should be using Bindings. This is because the view can't actually be modified (i.e, you can't modify the enabled state of a button). So you wouldn't ever need to use KVC to change enabledOrEnabled2, you only need your button to observe BOOLs to know if it should be enabled or not.
So scrap everything I said thusfar--
What you should do is still modify the setters ( -(void)setEnabled and -(void)setEnabled2 ), and before setting the ivar you should add a line:
[self.button setEnabled:(_Enabled || _Enabled2)];
That should do the trick for ya :)
Sorry for rambling a bit, I feel like the info I wrote initially may be helpful so I decided not to delete it

How do I create a cocoa binding in interface builder?

Real simple . . . say I create a slider, and I want to bind it to the value of a variable somewhere in my app. How do I do that?
In particular, I downloaded Apple's Temperature Converter sample code. But if I delete one of the bindings, I can't see any way to bring it back.
Select the slider in Interface Builder and choose Tools -> Bindings Inspector. There, select Value and enter the specifics of the binding.

Recording KeyCommands in a round view like tweetie or things preferences

How would you record the keystrokes in view and set them to the NSUserdefaults because every key has a numeric value and there are also modifier keys…how would u do this
Thanks
There's Waffle Software's Shortcut Recorder: http://wafflesoftware.net/shortcut/
(And to toot my own horn) I've written a class to make registering shortcuts easier than using the Carbon API or a wrapper like PTHotKey: http://github.com/davedelong/DDHotKey

What is the name of this Mac OS X control?

Does this control have a name? Or is it just a bunch of simple controls merged together? If so, what controls are they?
http://img8.imageshack.us/img8/3002/picture2xrb.png
It looks like an NSTableView with an a custom cell type and no column header. Have a look at the documentation for NSTableView's tableView:dataCellForTableColumn:row:. For columns which have the same type for all rows you may also set the cell class in interface builder.
I doubt the search box is part of the same control.
You could open the Application's Nib file to see what is in there. Look inside the application bundle. If the application is called Example then you should be able to find the Nib at Example.app/Contents/Resources/English.lproj/MainMenu.nib.
The best tool for investigating this is fscript, specifically FScriptAnywhere which will let you determine the class and much other information about any visual element of any Cocoa program (and do a lot of other interesting things with Cocoa programs).
In addition to what toholio said, an easy way to get the look and feel of the bottom button bar is with BWToolkit.

Resources