1.const與宏的區(qū)別
// 宏常見(jiàn)用法:
// 1.常用的字符串抽成宏
// 2.常用的代碼抽成宏
/*
const:常量
const:當(dāng)有字符串常量的時(shí)候,蘋果推薦我們使用const
const與宏的區(qū)別
1.編譯時(shí)刻: 宏:預(yù)編譯 const:編譯時(shí)刻
2.編譯檢查: 宏:不會(huì)檢查錯(cuò)誤 const:會(huì)檢查錯(cuò)誤
3.宏的好處:可以定義代碼
4.宏的壞處:編譯時(shí)間過(guò)長(zhǎng),因此常用的字符串通常使用const修飾
*/
// blog:經(jīng)常使用宏會(huì)造成內(nèi)存不段增加,每次使用宏,都會(huì)分配一個(gè)內(nèi)存
2.const簡(jiǎn)單實(shí)用
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
/*
const作用:
1.僅僅是用來(lái)修飾右邊的變量(只能修飾變量:基本變量,指針變量,對(duì)象變量)
2.const修飾的變量,表示只讀
const書(shū)寫規(guī)范:一定要放在變量的左邊
*/
// 宏:替代常用字符串常量
// 只讀
NSString * const abc = @"123";
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",abc);
// 用const修飾基本變量
// 定義int只讀變量
// 方式一:
// int const a = 10; // a:只讀變量
// 方式二:
// const int a = 10; // a:只讀變量
// 用const修飾指針變量
/*
int * const p = &a; // p:只讀變量 *p:變量
const int *p = &a; // *p:只讀變量 p:變量
int const *p = &a; // *p:只讀 p:變量
int const * const p = &a; // *p:只讀 p:只讀\
const int * const p = &a; // *p:只讀 p:只讀
*/
// 定義int變量
int a = 10;
int b = 20;
// 定義執(zhí)行a的指針變量
// 修改p的地址
// p = &b;
//
// *p = 30;
// 修飾對(duì)象變量
NSString * const name = @"123";
// name = @"321";
NSLog(@"%d",b);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
3.開(kāi)發(fā)中const使用場(chǎng)景
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
/*
const開(kāi)發(fā)中使用場(chǎng)景
// 1.定義一個(gè)全局只讀變量
// 2.在方法中定義只讀參數(shù)
*/
NSString * const name = @"123";
// 修飾對(duì)象
- (void)test:(NSString * const)name
{
}
// 修飾基本變量
- (void)test1:(int const)a
{
// a = 3;
}
// 修飾指針變量
- (void)test2:(int const *)p
{
// *p = 2;
NSLog(@"%d",*p);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self test:@"123"];
[self test1:2];
int a = 10;
[self test2:&a];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.static和extern使用
#import "ViewController.h"
@interface ViewController ()
@end
/*
static作用:
1.修飾局部變量
* 延長(zhǎng)這個(gè)局部變量的生命周期,只要程序運(yùn)行,局部變量就會(huì)一直存在
* 局部變量只會(huì)分配一次內(nèi)存,為什么?用static修飾的代碼,只會(huì)在程序一啟動(dòng)就會(huì)執(zhí)行,以后就不會(huì)在執(zhí)行
2.修飾全局變量
* 只會(huì)修改全局變量的作用域,表示只能是當(dāng)前文件內(nèi)使用
extern作用:
1.聲明一個(gè)變量,不能定義變量
注意:extern修飾的變量不能初始化
使用場(chǎng)景,一般用于聲明全局變量
*/
// 定義靜態(tài)全局變量
static int i = 2;
int a = 3;
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// static:修飾的變量,程序一運(yùn)行就會(huì)分配一次內(nèi)存
static int i = 0;
i++;
NSLog(@"%d",i);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// extern int a;
// NSLog(@"%d",a);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
5.static和const聯(lián)合使用
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
// 當(dāng)前字符串只能在本文件使用,并且只讀,不能改
static NSString * const name = @"123";
/*
static和const修飾全局變量
static修飾全局變量,修改作用域.表示在當(dāng)前文件中使用
const修飾變量.變量只讀
靜態(tài)全局只讀變量
*/
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
6.extern和const聯(lián)合使用
GlobeConst.h
// 聲明
// UIKIT_EXTERN = extern
/***********************首頁(yè)***************************************/
UIKIT_EXTERN NSString * const name ;
UIKIT_EXTERN NSString * const name1 ;
UIKIT_EXTERN NSString * const name2 ;
UIKIT_EXTERN NSString * const name3 ;
/***********************首頁(yè)***************************************/
GlobeConst.m
#import <Foundation/Foundation.h>
// 定義全局變量
// 全局只讀變量
/***********************首頁(yè)***************************************/
NSString * const name = @"213";
NSString * const name1 = @"213";
NSString * const name2 = @"213";
NSString * const name3 = @"213";