如何检查宏内的条件?

How to check for condition inside a macro?

我定义了这样一个宏,

#define SELECTED_SITE_ID (SITE_MANAGER.selectedSite.siteCollectionIdentifier)

它正在返回存储在用户默认值中的双精度值。

在代码中,SELECTED_SITE_ID 宏被用于 1000 多个这样的地方,

int a = SELECTED_SITE_ID;
NSArray *array = [someClassObject objectAtIndex:a-1];

因为我的应用程序是第一次 运行,SELECTED_SITE_ID 宏返回 0.0,它被分配给 int a; 所以 a 将是 0 .

我从数组的哪里写了 a-1 到 0-1,这会导致崩溃问题。

我现在不知道什么是快速解决这个问题的方法,因为它写在大约。 1000 个名额?

我怎么看?

I guess inside macro itself? If I would able to check, what's the value coming? if it's 0 then I will explicitly return 1.

如有任何帮助,我们将不胜感激。

这很简单,

#define SELECTED_SITE_ID ((SITE_MANAGER.selectedSite.siteCollectionIdentifier <= 0.0) ? 1 : SITE_MANAGER.selectedSite.siteCollectionIdentifier)

更好的方法是:

#define SELECTED_SITE_ID (SITE_MANAGER.selectedSite.siteCollectionIdentifier?:1)