indexOfObject 第一次返回 0 而不是 NSNotFound

发布于 2024-12-18 04:29:50 字数 1003 浏览 2 评论 0原文

我在 UIPopoverController 中有一个表,在 viewDidAppear 上我检查存储在 standardUserDefaults 中的单元格标签的值。 (这样我就可以突出显示最后选择的选项)。

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *lastMenu = [prefs objectForKey:@"location"];

NSUInteger index = [__menuTitles indexOfObject:lastMenu];

NSLog(@"lastMenu is %@",lastMenu);
NSLog(@"lastMenu index is %i",index);

第一次激活菜单时产生此结果

lastMenu is 
lastMenu index is 0

后续点击正确报告 2147483647 意味着 NSNotFound

lastMenu is 
lastMenu index is 2147483647

为什么第一次返回 0???

这是数组

__menuTitles = [[NSArray alloc] initWithObjects:
                @"North America", 
                @"Western Europe", 
                @"Asia Pacific", 
                @"Latin America", 
                @"Central & Eastern Europe", 
                @"Middle East", 
                @"Africa", 
                nil];

I have a table in a UIPopoverController, on viewDidAppear I check for the value of the cell label which is stored in standardUserDefaults. (So I can hilight the last selected option).

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *lastMenu = [prefs objectForKey:@"location"];

NSUInteger index = [__menuTitles indexOfObject:lastMenu];

NSLog(@"lastMenu is %@",lastMenu);
NSLog(@"lastMenu index is %i",index);

Produces this the first time the menu is activated

lastMenu is 
lastMenu index is 0

Subsequent clicks correctly report 2147483647 meaning NSNotFound

lastMenu is 
lastMenu index is 2147483647

Why does 0 get returned the first time???

Here's the array

__menuTitles = [[NSArray alloc] initWithObjects:
                @"North America", 
                @"Western Europe", 
                @"Asia Pacific", 
                @"Latin America", 
                @"Central & Eastern Europe", 
                @"Middle East", 
                @"Africa", 
                nil];

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

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

发布评论

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

评论(1

潦草背影 2024-12-25 04:29:50

可能意味着__menuTitles第一次是nil。向 nil 发送消息将导致返回 0。

Likely means that __menuTitles is nil the first time around. Sending a message to nil will result in 0 being returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文