如何快速将设置添加到我的可可应用程序? [英] How do I add settings to my cocoa application in swift?

查看:67
本文介绍了如何快速将设置添加到我的可可应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Swift 编程还很陌生,我想知道是否有一种简单的方法可以在 Swift 中为我的 Cocoa 应用程序添加设置/首选项.如果可能,我想要一个分步指南.我主要想知道您如何在磁盘和代码部分存储用户的首选项.在我当前的代码中,它需要检查用户选择了哪个设置,并根据该设置执行操作.我使用的是 Xcode 7.1 和 Swift 2.提前致谢!

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!

推荐答案

NSUserDefaults 类在代码中非常易于使用,并且它的共享实例很容易用于绑定到 Interface Builder 中的控件.

The NSUserDefaults class is very easy to use in code, and its shared instance is readily available for binding to controls in Interface Builder.

例如,如果我想要一个名为elmer"的整数首选项并将其值设置为 7,则很简单:

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")

读回值:

let elmer: Int = NSUserDefaults.standardUserDefaults().integerForKey("elmer")

 

要将值绑定到 Interface Builder 中的控件,请将控制器键设置为值",并将模型键路径的首选项名称设置为:

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:

 

我建议阅读 "首选项和设置编程指南",以及熟悉"NSUserDefaults 类参考".

I would recommend reading the "Preferences and Settings Programming Guide", and also to familiar yourself with the "NSUserDefaults Class Reference".

这篇关于如何快速将设置添加到我的可可应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆