将 NSUserdefaults 读入 NSArray

发布于 2025-01-02 08:41:06 字数 446 浏览 4 评论 0原文

我正在尝试将 Root.plist 键的值读取到 NSArray 中,以便我可以为我的应用程序设置填充表格视图。

我使用以下代码来检索键“country”的值:

NSArray *arrayCountry = [[NSUserDefaults standardUserDefaults] arrayForKey:@"country"];
NSLog(@"Number of items in array >>>>>> %d", arrayCountry.count);

NSLog 返回:

数组中的项目数>>>>> 0

我在不同的键上尝试了相同的操作,但仍然得到相同的结果。

谁能解释一下这个问题。我在这里错过了什么吗?

I am trying to read the values of Root.plist key into NSArray so I can populate a table view for my App settings.

I am using the following code to retrieve the values of the key "country":

NSArray *arrayCountry = [[NSUserDefaults standardUserDefaults] arrayForKey:@"country"];
NSLog(@"Number of items in array >>>>>> %d", arrayCountry.count);

NSLog returns:

Number of items in array >>>>>> 0

I tried the same on different keys but still get the same result.

Can anyone shed some light on this issue. Did I miss anything here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-01-09 08:41:06

确保您的 arrayCountry 不是 nil。如果是,arrayCountry.count 将返回零,即使它是 nil。

试试这个:

NSUserDefaults* std = [NSUserDefaults standardUserDefaults];
[std setObject:[NSArray arrayWithObjects:@"US",@"GB",@"CN",nil] forKey:@"country"];
NSArray *arrayCountry = [std arrayForKey:@"country"];
if(arrayCountry == nil){
    NSLog(@"Something's strange in the neighborhood.");
}else{
    NSLog(@"Number of items in array >>>>>> %d", arrayCountry.count);
}

Make sure your arrayCountry isn't nil. If it is, arrayCountry.count will return zero even if it is nil.

Try this:

NSUserDefaults* std = [NSUserDefaults standardUserDefaults];
[std setObject:[NSArray arrayWithObjects:@"US",@"GB",@"CN",nil] forKey:@"country"];
NSArray *arrayCountry = [std arrayForKey:@"country"];
if(arrayCountry == nil){
    NSLog(@"Something's strange in the neighborhood.");
}else{
    NSLog(@"Number of items in array >>>>>> %d", arrayCountry.count);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文