IOS开发之1-----copy语法的基本数据类型的使用01

1、NSString NSMutableString NSDictionary NSMutableDictionary NSArray NSMutableArray 使用copy和mutableCopy方法


/**
 1、拷贝的作用
    新指针和旧指针指向的对象内容要一样
    修改旧的对象不会改变新的对象
 2、拷贝类型
    深拷贝:内容拷贝,会产生新的对象,不同的对象,内容相同
    浅拷贝:指针拷贝,不会产生新的对象,指向的是同一个对象
 */

#import <Foundation/Foundation.h>
#pragma String类型调用mutableCopy方法
void stringMutableCopy1(){
    NSString *new = [[NSString alloc] initWithFormat:@"Hello world"];
    NSMutableString * mutableString = [new mutableCopy];
    [mutableString appendString:@"Jack"];
    NSLog(@"new -- %@",new);
    NSLog(@"mutableString -- %@",mutableString);
}
#pragma String类调用copy方法的时候,产生新的String对象,如果使用string对象调用相关的方法,这个对象本身不会改变,返回的数据会发生改变
void stringCopy(){
    NSString *old = [[NSString alloc]initWithFormat:@"Java"];
    NSString *new = [old copy];
    NSString *third = [new stringByAppendingString:@"world"];
    NSLog(@"old ---%@",old);
    NSLog(@"new ---%@",new);
    NSLog(@"third -- %@",third);
    
}
void mutableStringCopy(){
    NSMutableString * old = [[NSMutableString alloc]initWithFormat:@"Jake"];
    NSString * new = [old copy];
    [new stringByAppendingString:@" ios is good"];
    NSLog(@"old %@",old);
    NSLog(@"new %@",new);
}
void mutableStringMutableCopy(){
    NSMutableString * old = [[NSMutableString alloc]initWithFormat:@"Jake"];
    NSMutableString * new = [old mutableCopy];
    [new appendString:@" ios is good"];
    NSLog(@"old %@",old);
    NSLog(@"new %@",new);
}
void directoryCopy(){
//    NSNumber * number = [NSNumber numberWithInt:15];
    NSDictionary * oldDictionary = @{@"name":@"zhangsan",@"age":[NSNumber numberWithInt:15] };
    NSDictionary * newDictionary = [oldDictionary copy];
    NSLog(@"oldDictionary--%@",oldDictionary);
    NSLog(@"newDictionary--%@",newDictionary);
}
void dictionaryMutcopy(){
    NSMutableDictionary * oldMutableDictionary = [NSMutableDictionary dictionary];
    [oldMutableDictionary setObject:@"zhangfeifei" forKey:@"name"];
    [oldMutableDictionary setObject:@25 forKey:@"age"];
    NSMutableDictionary * newMutableDictionary = [oldMutableDictionary mutableCopy];
    [newMutableDictionary setObject:@190 forKey:@"weight"];
    NSLog(@"oldMutableDictionary---%@",oldMutableDictionary);
    NSLog(@"newMutableDictionary---%@",newMutableDictionary);
}
void arrayMutableCopy(){
    NSMutableArray * mutableArray = [[NSMutableArray alloc]init];
    [mutableArray addObject:@12];
    [mutableArray addObject:@12];
    NSMutableArray * newMutableArray = [mutableArray mutableCopy];
    [newMutableArray addObject:@17];
    NSLog(@"mutableArray  %@",mutableArray);
    NSLog(@"newMutableArray  %@",newMutableArray);
}
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        stringMutableCopy1();
        stringCopy();
        
        mutableStringCopy();
        
        directoryCopy();
        dictionaryMutcopy();
        
        arrayMutableCopy();
        
        
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值