Objective-C none nil 数组在 if/else 语句中显示为 nil

发布于 2024-12-06 04:48:23 字数 686 浏览 0 评论 0原文

以下代码未按预期工作。我在创建视图之后但在显示之前设置一个数组。我使用 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 技术交流群。

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

发布评论

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

评论(2

你不是我要的菜∠ 2024-12-13 04:48:23
if (planListArr == nil || [planListArr count] == 0) { ... }
else {
    NSLog(@"Planlist is empty");
}

展开后,这就变成了:

if (planListArr == nil || [planListArr count] == 0) {
    ...
} else {
    NSLog(@"Planlist is empty");
}

所以基本上,看起来你的 NSLog() 语句位于错误的分支中。

if (planListArr == nil || [planListArr count] == 0) { ... }
else {
    NSLog(@"Planlist is empty");
}

Expanded, this becomes:

if (planListArr == nil || [planListArr count] == 0) {
    ...
} else {
    NSLog(@"Planlist is empty");
}

So basically, it looks like you have your NSLog() statement in the wrong branch.

往事随风而去 2024-12-13 04:48:23
(!plainListArray && [plainListArray count]>0) ? NSLog(@"PlainList array has %d items",[plainListArray count] : NSLog(@"Oops! Array is not been initialized or it has no items");
(!plainListArray && [plainListArray count]>0) ? NSLog(@"PlainList array has %d items",[plainListArray count] : NSLog(@"Oops! Array is not been initialized or it has no items");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文