// // ViewController.m // OC高效率52之多用類型常量,少用#define預處理指令 /** * 1. 不要用預處理定義常量。這樣定義出來的常量不含類型信息,編譯器只是會在編譯前據此執行查找與替換操做。即時有人從新定義了常量值,編譯器也不會產生警告信息,這將致使應用程序中得常量值不一致。 2。在實現文件中使用static const 來定義「只在編譯單元內可見的常量」。因爲此類常量不在全局符號表中,因此無需爲其名稱加前綴。 3.在頭文件中使用extern來聲明全局變量,並在相關實現文件中定義其值。這種常量要出如今全局符號列表中,因此其名稱應加以區隔,一般用與之相關的類名作前綴。 */ #import "ViewController.h" #define ANIMATION_DURTION 0.3 //定義出的常量,沒有類型信息 static const NSTimeInterval kAnimationDuration = 2.0;//知道常量類型有助編寫開發文檔 //變量必定要用static const 來聲明,若是視圖修改const所聲明的變量,編譯器會報錯 @interface ViewController () @end NSString *const EOCLoginManagerDidLoginNotificaton = @"EOCLoginManagerDidLoginNotificaton"; @implementation ViewController -(void)animate { [UIView animateWithDuration:kAnimationDuration animations:^{ //Perform animations; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 80, 80)]; view.backgroundColor = [UIColor orangeColor]; [self.view addSubview:view]; } completion:^(BOOL finished) { }]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self animate]; // [self login]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 300, 100, 30)]; btn.backgroundColor = [UIColor greenColor]; [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchDragInside]; [btn setTitle:@"點擊通知" forState:UIControlStateNormal]; [self.view addSubview:btn]; } -(void)login { [[NSNotificationCenter defaultCenter]postNotificationName:EOCLoginManagerDidLoginNotificaton object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end