关键字(2018-04-12)

2018-04-12  本文已影响0人  小的小碰撞

const

-(void)click{
   int c = 5;
   [self test:&c];
}
-(void)test:(int *)b{
    
    int a = *b;
    NSLog(@"%d",a);
    
}

const与宏的对比

名称 编译时刻 编译检查 优点 缺点
预编译 没有编译检查 可以定义方法 、函数 使用过多 会编译时间过长
const 编译 有编译检查
    /*****修饰基本变量*******/
    int const a = 3;
    const int b = 5;
    // 以上两种方式等价于(都可以的)
    // a = 4; 报错
    //Cannot assign to variable 'a' with const-qualified type 'const int'
    // 不能将具有const限定类型的const int赋值给变量'a'。
    // 修饰指针变量
    
    /*****修饰指针*******/
    int c = 5;
    int d;
    int const *p = &c;
    c = 7;
    //*p = 9; 报错 Read-only variable is not assignable
    p = &d;

    int * const p;  // p:只读  *p:变量
    int const * p1; // p1:变量 *p1:只读
    const int * p2; // p2:变量 *p2:只读
    const int * const p3; // p3:只读 *p3:只读
    int const * const p4; // p4:只读  *p4:只读

static

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    static int a = 0;//延长生命周期
    a ++;
    
    NSLog(@"%d",a);
}

extern

#import "ViewController.h"
int i = 10;

#import "AppDelegate.h
extern int i;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSLog(@"%d",i);
    
    // Override point for customization after application launch.
    return YES;
}

static和const

static修饰全局变量,修改作用域
static NSString * const unite = @"联合";

extern和const

.H
#import <UIKit/UIKit.h>
// 可写可不写  不写用 UIKIT_EXTERN
{
#ifdef __cplusplus
#define CWQKIT_EXTERN        extern "C" __attribute__((visibility ("default")))
#else
#define CWQKIT_EXTERN            extern __attribute__((visibility ("default")))
#endif
}
typedef enum {
    CWQTopicTypeAll = 1,
    CWQTopicTypePicture = 10,
    CWQTopicTypeWord = 29,
    CWQTopicTypeVoice = 31,
} CWQTopicType;

CWQKIT_EXTERN NSString * const CWQconstant;

/** 精华-顶部标题的高度 */
CWQKIT_EXTERN CGFloat const CWQTitilesViewH;
/** tabBar被选中的通知名字 */
CWQKIT_EXTERN NSString * const CWQTabBarDidSelectNotification;



.M
#import <UIKit/UIKit.h>

NSString * const CWQconstant = @"常量";
/** 精华-顶部标题的高度 */
CGFloat const CWQTitilesViewH = 35;
/** tabBar被选中的通知名字 */
NSString * const CWQTabBarDidSelectNotification = @"CWQTabBarDidSelectNotification";
上一篇 下一篇

猜你喜欢

热点阅读