NSUserDefaults - 如何判断密钥是否存在 [英] NSUserDefaults - How to tell if a key exists
问题描述
我正在开发一个小型iPhone应用程序,我使用 NSUserDefaults
作为我的数据持久性。它只需要跟踪一些事情,比如一些名字和一些数字,所以我想我也可以保持简单。
I'm working on a small iPhone app, and I am using NSUserDefaults
as my data persistence. It only has to keep track of a few things, such as some names and some numbers so I figure I might as well keep it simple.
我发现此页面供参考,但我不认为它可以回答我的问题。基本上,我希望能够检查 NSUserDefaults
中是否已存在值(或密钥),然后相应地执行某些操作。
I found this page for some reference, but I don't think it can answer my question. Basically, I want to be able to check if a value (or a key) already exists in the NSUserDefaults
and then do something accordingly.
一些例子:应用程序启动,如果这是第一次启动它会输出一个表示欢迎的警报。要判断这是否是第一次打开,它会读取 UserDefaults
并进行检查。
Some examples: The app starts up, if this is the first time it starts up it outputs an alert saying welcome. To tell if this is first time it has opened it reads the UserDefaults
and checks.
示例2:它说,Hello [Name],其中Name是您输入的内容。如果你打开了应用程序但没有名字,那就应该说Hello World。我需要检查您是否已经输入了名称并采取相应措施。该名称将存储在 NSUserDefaults
中。
Example 2: It says, "Hello [Name]", where Name is something you have entered. If you have opened the app and there is no name, it should say "Hello World." I need to check if you have entered a name already and act accordingly. The name would be stored in NSUserDefaults
.
这里有一些帮助吗?我真的很感激!
Some help here? I'd really appreciate it!
推荐答案
objectForKey:
将返回 nil
如果它不存在。
objectForKey:
will return nil
if it doesn't exist.
这篇关于NSUserDefaults - 如何判断密钥是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!