美文网首页
【iOS】copy和mutableCopy

【iOS】copy和mutableCopy

作者: cod_mm | 来源:发表于2015-12-02 14:43 被阅读0次

    NSString

    NSString *str = @"copy and mutableCopy";
    NSLog(@"NSString: %@ %p", NSStringFromClass([str class]), str);
    
    NSString *copyStr = [str copy];
    NSLog(@"NSString copy: %@ %p", NSStringFromClass([copyStr class]), copyStr);
    
    NSMutableString *mutableCopyStr = [str mutableCopy];
    NSLog(@"NSString mutableCopy: %@: %p", NSStringFromClass([mutableCopyStr class]), mutableCopyStr);
    
    • NSString copy 没有生成新的字符串
    • NSString mutableCopy 生成了新的字符串,且是可变的字符串

    NSMutableString

    NSMutableString *str0 = [@"test" mutableCopy];
    NSLog(@"NSMutableString: %@: %p", NSStringFromClass([str0 class]), str0);
    
    NSMutableString *str1 = [str0 copy];
    NSLog(@"NSMutableString copy: %@: %p", NSStringFromClass([str1 class]), str1);
    
    NSMutableString *str2 = [str0 mutableCopy];
    NSLog(@"NSMutableString mutableCopy: %@: %p", NSStringFromClass([str2 class]), str2);
    
    • NSMutableString copy 生成了新的字符串,且是不可变的字符串
    • NSMutableString mutableString 生成了新的字符串,且是可变字符串

    相关文章

      网友评论

          本文标题:【iOS】copy和mutableCopy

          本文链接:https://www.haomeiwen.com/subject/tmzjhttx.html