2014年03月30日

来源:互联网 发布:淘宝的港货可以买吗? 编辑:程序博客网 时间:2024/04/30 12:13

iOS(const) const的基本用法

 

基本用法: 修饰变量, 使其值不可更改:

1
2
const int a = 1;
a = 2;//ERROR: Read-only variable is not assignable

const配合指针时:

情况一, const放在*前面, 则指针指向的值不能改变,

1
2
3
int a = 1;
const int *pa = &a;
*pa = 2; // ERROR: Read-only variable is not assignable

情况二, const放到*后面,则指针不能指向别处,

1
2
3
int a = 1, b = 2;
int * const pa = &a;
pa = &b; // ERROR: Read-only variable is not assignable

情况三, *左右皆放置const,则指针既不可改其值, 又不可指向别处

1
2
3
4
int a = 1, b = 2;
const int * const pa = &a;
pa = &b; // ERROR: Read-only variable is not assignable
*pa = 3; // ERROR: Read-only variable is not assignable

综合以上问题,可以解决iOS关于变量于常量之间

Capturing 'self' strongly in this block is likely to lead to aretain cycle

的警告。
0 0