怎么#define不需要指定变量类型?

Tap*_*eak 0 objective-c

为什么这段代码有效?

// in a constants file:

#define ADFadeOutSpeed 1.1



// then, later, in another file:

-(void)fadeOut:(UIView *)sender{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:ADFadeOutSpeed];
    sender.alpha = 0.0;
    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)

我原以为编译器会抱怨ADFadeOutSpeed没有强类型.

Ron*_*gge 6

Because #define doesn't create a variable or object, it's a compiler command that says 'replace all instances of foo with bar' -- so what's happening, quit eliterally, is that ADFadeOutSpeed is read as 1.1 every time it shows in your code. The compiler doesn't see:

[UIView setAnimationDuration:ADFadeOutSpeed];
Run Code Online (Sandbox Code Playgroud)

it sees

[UIView setAnimationDuration:1.1];
Run Code Online (Sandbox Code Playgroud)