我想检查变量名是否已经定义。 我不在乎它是否等于nil
。我会在一个宏定义一个变量,如果它尚未定义使用此。但是如果定义了它,宏就会设置变量而不是重新定义它(实际上阻止了编译器的构建)。检查当前方法中是否使用了变量名称
#define newAnimation()\
if (/* variableName is being used */) {\
anim = [CABasicAnimation animation];\
} else {\
CABasicAnimation* anim = [CABasicAnimation animation];\
}
然后,我会使用像这样的宏。
newAnimation();
anim.keyPath = @"position.x";
anim.toValue = @10;
[self.namedSubview addAnimation:anim forKey:nil];
newAnimation();
anim.keyPath = @"position.y";
anim.toValue = @50;
[self.otherSubview addAnimation:anim forKey:nil];
注:与宏的部分只是我的使用情况这个问题。你的答案应该在方法中工作。
你想完成什么?你更大的目标是什么? –
@JoshCaswell我刚澄清。 – aleclarson
在什么情况下你不知道变量是否已被声明? –