OC3_Copy及MultableCopy

//
//  main.m
//  OC3_Copy及MultableCopy
//
//  Created by zhangxueming on 15/6/19.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
//copy mutableCopy
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //copy 把可变对象及不可变对象 copy生成不可变对象
        NSString *str = @"hello world";
        NSString *str1 = [str copy];
        //[str1 insertString:@"qianfeng" atIndex:4];
        NSLog(@"str = %p str1 = %p", str, str1);
        NSLog(@"str1 = %@", str1);

        NSMutableString *str2 = [NSMutableString stringWithString:@"hello world"];
        NSString *str3 = [str2 copy];
        //[str3 insertString:@"qianfeng" atIndex:4];
        NSLog(@"str3 = %@", str3);

        //mutableCopy 把可变或者不可变对象copy生成可变对象

        NSString *str4 = @"qianfeng";
        NSMutableString *str5 = [str4 mutableCopy];
        [str5 insertString:@"hello" atIndex:4];
        NSLog(@"str5 = %@", str5);

        NSMutableString *str6 = [NSMutableString stringWithString:@"qianfeng"];
        NSMutableString *str7 = [str6 mutableCopy];
        [str7 insertString:@"world" atIndex:4];
        NSLog(@"str7 = %@", str7);

    }
    return 0;
}
时间: 2024-10-13 16:15:40

OC3_Copy及MultableCopy的相关文章

iOS copy 和 mutableCopy 学习

(参考 iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期)的对象部分 关于Copy  有个经典问题”大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有什么区别呢” 或者说”为什么 NSString 类型成员变量的修饰属性用 copy 而不是 strong (或 retain ) ?” 明显 第一句比第二句 严谨多了. @property (strong,nonatomic) NSString *strongString; & @property

【Object-C】Copy

-copyWithZone: Returns a new instance that's a copy of the receiver. (required) -mutableCopy Returns the object returned by mutableCopyWithZone: where the zone is nil. Declaration: - (id)copyWithZone:(NSZone *)zone - (id)mutableCopy 简单的说,copy方法用于复制对象