// // 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
时间: 2024-10-01 09:43:03