const修饰符修饰的对象的内存地址是不可变的;
注意:
(1)const修饰的对象不绝对是常量(除几个特殊状况外, 能够理解为常量)
(2)const修饰的对象不绝对是只读的
(3)关键是内存地址, 内存地址是不可变的bash
NSString * string1 = @"string1";
NSLog(@"1 --- 变量的内存地址: %p", string1);
NSLog(@"&1 --- 指针的内存地址: %p", &string1);
string1 = @"string1***string";
NSLog(@"2 --- 变量的内存地址: %p", string1);
NSLog(@"&2 --- 指针的内存地址: %p",&string1);
输出结果:
1 --- 0x110087c20
&1 --- 0x7ffedfdfe4b0
2 --- 0x110087c80
&2 --- 0x7ffedfdfe4b0
复制代码
从输出结果能够看出: 改变字符串变量:
(1)指针的内存地址没有改变;
(2)变量的内存地址改变了;app
NSString * const string1 = @"string1";
NSLog(@"1 --- 变量的内存地址: %p", string1);
NSLog(@"&1 --- 指针的内存地址: %p", &string1);
string1 = @"string1***string";//报错: Cannot assign to variable 'str1' with const-qualified type 'NSString *const __strong'
复制代码
从报错信息能够看出: 由于const修饰了字符串变量string1, 因此string1就是一个常量了, 不能再改变它的值了, 不然会报错的;ui
NSString const * string1 = @"string1";
NSLog(@"1 --- 变量的内存地址: %p", string1);
NSLog(@"&1 --- 指针的内存地址: %p", &string1);
string1 = @"string1***string";
NSLog(@"2 --- 变量的内存地址: %p", string1);
NSLog(@"&2 --- 指针的内存地址: %p",&string1);
输出结果:
1 --- 变量的内存地址: 0x1038c4c20
&1 --- 指针的内存地址: 0x7ffeec5c14b0
2 --- 变量的内存地址: 0x1038c4c80
&2 --- 指针的内存地址: 0x7ffeec5c14b0
复制代码
从输出结果能够看出: const修饰指针时: (1)指针的内存地址没有改变, (2)变量string1的内存地址改变了, 变量的值是能够改变的.spa
const NSString * string1 = @"string1";
NSLog(@"1 --- 变量的内存地址: %p", string1);
NSLog(@"&1 --- 指针的内存地址: %p", &string1);
string1 = @"string1***string";
NSLog(@"2 --- 变量的内存地址: %p", string1);
NSLog(@"&2 --- 指针的内存地址: %p",&string1);
输出结果:
1 --- 变量的内存地址: 0x10e7cfc20
&1 --- 指针的内存地址: 0x7ffee16b64b0
2 --- 变量的内存地址: 0x10e7cfc80
&2 --- 指针的内存地址: 0x7ffee16b64b0
复制代码
从输出结果能够看出: const修饰NSString等同于修饰 *;指针
NSMutableString * const variable = @"string".mutableCopy;
NSLog(@"1 --- 变量的内存地址: %p",variable);
NSLog(@"&1 --- 指针的内存地址: %p",&variable);
[variable appendString:@"___variable"];//改变变量内存中的内容
NSLog(@"2 --- 变量的内存地址: %p",variable);
NSLog(@"&2 --- 指针的内存地址: %p",&variable);
//variable = @"mString".mutableCopy; //报错:Cannot assign to variable 'variable' with const-qualified type 'NSMutableString *const __strong'
输出结果:
1 --- 变量的内存地址: 0x600000ef80c0
&1 --- 指针的内存地址: 0x7ffee91eb4b8
2 --- 变量的内存地址: 0x600000ef80c0
&2 --- 指针的内存地址: 0x7ffee91eb4b8
复制代码
从输出结果能够看出:const修饰可变字符串的变量时:
(1)能够改变变量内存中的内容(内存地址不改变);
(2)变量的内存地址不改变;
(3)变量不能够指向其余的内存code
NSMutableString const *variable = @"string".mutableCopy;
NSLog(@"1 --- 变量的内存地址: %p",variable);
NSLog(@"&1 --- 指针的内存地址: %p",&variable);
[variable appendString:@"___variable"];//改变变量内存中的内容
NSLog(@"2 --- 变量的内存地址: %p",variable);
NSLog(@"&2 --- 指针的内存地址: %p",&variable);
variable = @"mString".mutableCopy;
NSLog(@"3 --- 变量的内存地址: %p",variable);
NSLog(@"&3 --- 指针的内存地址: %p",&variable);
输出结果:
1 --- 变量的内存地址: 0x600003931590
&1 --- 指针的内存地址: 0x7ffeeb6b04b8
2 --- 变量的内存地址: 0x600003931590
&2 --- 指针的内存地址: 0x7ffeeb6b04b8
3 --- 变量的内存地址: 0x6000039314d0
&3 --- 指针的内存地址: 0x7ffeeb6b04b8
复制代码
从输出结果能够看出:const修饰可变字符串的指针 *时:
(1)指针的内存地址始终保持不变;
(2)可变字符串能够修改内存中的内容(内存地址不改变);
(3)可变字符串能够指向其余的内存(内存地址改变)对象
const NSMutableString * variable = @"string".mutableCopy;
NSLog(@"1 --- 变量的内存地址: %p",variable);
NSLog(@"&1 --- 指针的内存地址: %p",&variable);
[variable appendString:@"___variable"];//改变变量内存中的内容
NSLog(@"2 --- 变量的内存地址: %p",variable);
NSLog(@"&2 --- 指针的内存地址: %p",&variable);
variable = @"mString".mutableCopy; //报错:Cannot assign to variable 'variable' with const-qualified type 'NSMutableString *const __strong'
NSLog(@"3 --- 变量的内存地址: %p",variable);
NSLog(@"&3 --- 指针的内存地址: %p",&variable);
输出结果:
1 --- 变量的内存地址: 0x600000344210
&1 --- 指针的内存地址: 0x7ffeee6724b8
2 --- 变量的内存地址: 0x600000344210
&2 --- 指针的内存地址: 0x7ffeee6724b8
3 --- 变量的内存地址: 0x600000370d20
&3 --- 指针的内存地址: 0x7ffeee6724b8
复制代码
从输出结果能够看出:const修饰NSMutableString等同于const修饰可变字符串的指针;内存