第二十四讲:Objective-C内存管理之拷贝简述
本帖最后由 Sian 于 2014-1-22 19:42 编辑1、创建一个字符串NSString,创建一个可变字符串NSMutableString;
2、分别使用copy与mutableCopy方法分别对NSString与NSMutableString类型对象进行赋值
3、效果是NSString copy后为NSString、NSString mutableCopy后为MutableString、NSMutableString copy后为NSString无法使用appendString方法、NSMutableString mutableCopy后为NSMutableString;
main.m
//
//main.m
//Copy
//
//Created by yusian on 14-1-22.
//Copyright (c) 2014年 yusian. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString * s1 = [ initWithString:@"String"];
NSMutableString * s2 = [ initWithString:@"MutableString"];
NSString * s11 = ;
NSString * s12 = ;
NSMutableString * s21 = ;
NSMutableString * s22 = ;
;
//;
;
NSLog(@"s1 is \"%@\"", s1);
NSLog(@"s2 is \"%@\"", s2);
NSLog(@"s11 is \"%@\" copy to:\"%@\"",s1, s11);
NSLog(@"s12 is \"%@\" mutableCopy to:\"%@\"",s1, s12);//NSString may not respond to 'appendString:
NSLog(@"s21 is \"%@\" copy to ,Can't use ",s21);
NSLog(@"s22 is \"%@\" mutableCopy to:\"%@\"",s2, s22);
;
;
}
return 0;
}
运行结果:
2014-01-22 19:12:35.398 Copy s1 is "String"
2014-01-22 19:12:35.399 Copy s2 is "MutableString"
2014-01-22 19:12:35.399 Copy s11 is "String" copy to:"String"
2014-01-22 19:12:35.400 Copy s12 is "String" mutableCopy to:"String to mutableString"
2014-01-22 19:12:35.400 Copy s21 is "MutableString" copy to ,Can't use
2014-01-22 19:12:35.400 Copy s22 is "MutableString" mutableCopy to:"MutableString to mutableString"
Program ended with exit code: 0
页:
[1]