Objective-C none nil 数组在 if/else 语句中显示为 nil
以下代码未按预期工作。我在创建视图之后但在显示之前设置一个数组。我使用 NSLog 来测试数组是否已设置,但 if/else 将数组视为空。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Planlist is nil %d / has %d objects", (planListArr == nil), [planListArr count]);
if (planListArr == nil || [planListArr count] == 0) { ... }
else {
NSLog(@"Planlist is empty");
}
}
日志
2011-09-25 13:54:39.764 myVI[2938:13303] Planlist is nil 0 / has 8 objects
2011-09-25 13:54:39.765 myVI[2938:13303] Planlist is empty
计划列表定义为
NSArray *planListArr;
@property (nonatomic, retain) NSArray *planListArr;
The following code is not working as expected. I am setting an array after creating a view but before displaying. I used NSLog to test that the array is set but the if/else sees the array as empty.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Planlist is nil %d / has %d objects", (planListArr == nil), [planListArr count]);
if (planListArr == nil || [planListArr count] == 0) { ... }
else {
NSLog(@"Planlist is empty");
}
}
Logs
2011-09-25 13:54:39.764 myVI[2938:13303] Planlist is nil 0 / has 8 objects
2011-09-25 13:54:39.765 myVI[2938:13303] Planlist is empty
PlanList is defined as
NSArray *planListArr;
@property (nonatomic, retain) NSArray *planListArr;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
展开后,这就变成了:
所以基本上,看起来你的
NSLog()
语句位于错误的分支中。Expanded, this becomes:
So basically, it looks like you have your
NSLog()
statement in the wrong branch.