TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
本帖最后由 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 = [[NSString alloc] initWithString:@"String"];
- NSMutableString * s2 = [[NSMutableString alloc] initWithString:@"MutableString"];
- NSString * s11 = [s1 copy];
- NSString * s12 = [s1 mutableCopy];
- NSMutableString * s21 = [s2 copy];
- NSMutableString * s22 = [s2 mutableCopy];
- [s12 appendString:@" to mutableString"];
- //[s21 appendString:@" to String"];
- [s22 appendString:@" to mutableString"];
- 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 appendString:]",s21);
- NSLog(@"s22 is "%@" mutableCopy to:"%@"",s2, s22);
- [s1 release];
- [s2 release];
-
-
- }
- return 0;
- }
复制代码 运行结果:- 2014-01-22 19:12:35.398 Copy[1254:303] s1 is "String"
- 2014-01-22 19:12:35.399 Copy[1254:303] s2 is "MutableString"
- 2014-01-22 19:12:35.399 Copy[1254:303] s11 is "String" copy to:"String"
- 2014-01-22 19:12:35.400 Copy[1254:303] s12 is "String" mutableCopy to:"String to mutableString"
- 2014-01-22 19:12:35.400 Copy[1254:303] s21 is "MutableString" copy to ,Can't use [s21 appendString:]
- 2014-01-22 19:12:35.400 Copy[1254:303] s22 is "MutableString" mutableCopy to:"MutableString to mutableString"
- Program ended with exit code: 0
复制代码
|
|