29,027
社区成员
发帖
与我相关
我的任务
分享
//
// main.m
// 2.1.4_const_modifier
//
// Created by Cirno MainasuK on 2014-11-30.
// Copyright (c) 2014年 Cirno MainasuK. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
const NSString *foo = @"MY_CONSTANT";
// 执行一些操作
foo = @"SOME_OTHER_VALUE"; // 这会产生一个编译器错误
if ([foo isEqualToString:@"MY_CONSTANT"]) {
NSLog(@"%@", foo);
// 执行操作
}
NSLog(@"%@", foo);
return 0;
}
//
// main.m
// 2.1.4_const_modifier
//
// Created by Cirno MainasuK on 2014-11-30.
// Copyright (c) 2014年 Cirno MainasuK. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSString const *foo = @"MY_CONSTANT";
// 执行一些操作
foo = @"SOME_OTHER_VALUE"; // 这会产生一个编译器错误
if ([foo isEqualToString:@"MY_CONSTANT"]) {
NSLog(@"%@", foo);
// 执行操作
}
NSLog(@"%@", foo);
return 0;
}
const char* s = xxx;
char const * s = xxx;
char* const s = xxx;
面试时,这道题答错的人,我不会怪他;
答对的人,我才担心呢.
会特别交待,不能使用这么生偏的技巧,特别是接口中出现
const int init(const char const* const level) const;
看到类似的接口,还得我拿起书本复习一下才能接着往下做事.
对于数据,谁创建,谁释放,别人只能使用,不能修改....
一旦跨过了这个规则,就需要在文档里面写明.....
const char* s = xxx;
char const * s = xxx;
char* const s = xxx;
中s指针本身能不能改,或者s指向的数据能不能改
NSString只能要求指针本身能不能改,
NSString的数据是不能改的吧?
NSMutableString的数据才可以