indexOfObject 第一次返回 0 而不是 NSNotFound
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能意味着
__menuTitles
第一次是nil
。向nil
发送消息将导致返回 0。Likely means that
__menuTitles
isnil
the first time around. Sending a message tonil
will result in 0 being returned.