OC之Copy语法
转载请注明:http://www.cnblogs.com/letougaozao/p/3631105.html
- 概念
- 内存管理
- NSString的copy实例
- 对象的copy实例
一、概念
目的:在改变原有对象的时候,不会改变新对象的值
- Copy:实现NSCopying协议,创建的是一个不可变副本
- MutableCopy:实现NSMutableCopying协议,创建的是一个可变副本
二、内存管理
- 深拷贝:产生新的对象,所以源对象计数器不变>>>对象拷贝
- 浅拷贝:不产生新对象,所以源对象计数器加一>>>指针拷贝(因为对象本身不可以变,所有没有必要再创建一个对象)
三、NSString的copy实例
//
// main.m
// Copy语法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h> void test1()
{
NSString *str = [NSString stringWithFormat:@"age is %i", ];
NSString *str1 = [str copy]; NSLog(@"%i", str == str1); NSString *str2 = [str mutableCopy]; NSLog(@"%i", str2 == str);
} void test2()
{
NSMutableString *str = [NSMutableString stringWithFormat:@"age is %i", ]; NSString *str1 = [str copy];
NSMutableString *str2 = [str mutableCopy]; [str appendFormat:@""]; NSLog(@"%i", str == str2);
NSLog(@"%i", str == str1);
NSLog(@"%@", str);
NSLog(@"%@", str1);
} int main(int argc, const char * argv[])
{ @autoreleasepool { test2(); }
return ;
}
四、对象拷贝的实例
对象的拷贝,主要注意点
- 必须实现NSCopying协议
- 需要重写- (id)copyWithZone:(NSZone *)zone方法
- 代码中 self class的引用
1⃣️GoodStudent.h
//
// GoodStudent.h
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "Student.h" @interface GoodStudent : Student @property (nonatomic, assign) int age; +(id)goodStudentWithName:(NSString *)name withAge:(int)age; @end
2⃣️GoodStudent.m
//
// GoodStudent.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "GoodStudent.h" @implementation GoodStudent +(id)goodStudentWithName:(NSString *)name withAge:(int)age
{
GoodStudent *stu = [super studentWithName:name];
stu.age = age; return stu;
} -(id)copyWithZone:(NSZone *)zone
{
GoodStudent *copy = [super copyWithZone:zone];
copy.age = self.age; return copy;
} -(NSString *)description
{
return [NSString stringWithFormat:@"%@-%i", self.name, self.age];
} @end
3⃣️Student.h
//
// Student.h
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h> @interface Student : NSObject <NSCopying> @property (nonatomic, copy) NSString *name; +(id)studentWithName:(NSString*)name; @end
4⃣️Student.m
//
// Student.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "Student.h" @implementation Student
+(id)studentWithName:(NSString *)name
{
Student *stu = [[[[self class] alloc] init] autorelease];
stu.name = name; return stu;
} - (id)copyWithZone:(NSZone *)zone
{
Student *copy = [[self class] allocWithZone:zone]; copy.name = self.name; return copy;
} -(NSString *)description
{
return [NSString stringWithFormat:@"%@", self.name];
} -(void)dealloc
{
[_name release];
[super dealloc];
}
@end
main.m
//
// main.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h>
#import "GoodStudent.h" void test1()
{
Student *stu = [Student studentWithName:@"name1"];
Student *stu1 = [stu copy]; NSLog(@"%@", stu);
NSLog(@"%@", stu1);
} void test2()
{
GoodStudent *stu1 = [GoodStudent goodStudentWithName:@"name1" withAge:]; GoodStudent *stu2 = [stu1 copy]; NSLog(@"%@", stu1);
NSLog(@"%@", stu2); } int main(int argc, const char * argv[])
{ @autoreleasepool { test2(); }
return ;
}
OC之Copy语法的更多相关文章
- copy语法
copy 和 mutableCopy 一个对象使用copy或者mutableCopy方法可以创建对象的副本 --------------- copy - 需要先实现NSCopying协议,创建的是不可 ...
- OC基础 点语法的使用
OC基础 点语法的使用 1.创建一个Student类继承于NSObject,Student.h文件 #import <Foundation/Foundation.h> @interface ...
- 「OC」点语法和成员变量的作用域
一.点语法 (一)认识点语法 声明一个Person类: 1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObje ...
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
一. 分类-Category 1. 基本用途:Category 分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...
- 【Swfit】Swift与OC两种语法写单例的区别
Swift与OC两种语法写单例的区别 例如写一个NetworkTools的单例 (1)OC写单例 + (instancetype)sharedNetworkTools { static id inst ...
- [OC Foundation框架 - 17] copy语法
一个对象使用copy或mutableCopy方法可以创建对象的副本 1.copy 需要实现NSCopying协议 创建出来的是不可变副本,如NSString, NSArray, NSDictionar ...
- OC:Block语法、Block使用、Block实现数组排序
Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return a > b ...
- Object-c学习之路十二(OC的copy)
oc中的拷贝分为:copy(浅拷贝)和mutablecopy(深拷贝). 浅拷贝也为指针拷贝,拷贝后原来的对象计数器会+1: 深拷贝为对象拷贝,原来的对象计数器不变. 注意:自定义对象拷贝时要实现NS ...
- 黑马程序员——OC语言基础语法 面向对象的思想
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结)(一)基础语法 1)关键字 @interface.@implementati ...
随机推荐
- mysql show命令集合
a. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称b. show databases; -- 显示mysql中所有数据库的 ...
- JQ避免出现多次执行一个事件的解决方案
点击按钮之后会多次执行一个事件的话,就在方法结尾加入如下代码,这样的话事件就可以只执行一次了 //避免出现多次执行事件的问题 event.stopPropagation(); 此外,时间的重复绑定也有 ...
- Unable to load dll 的解决方案
前几天在做项目时,需要用到一个非托管的 dll 库,其实使用 .Net 的互操作技术可以很方便地调用非托管 dll 文件中的函数,但是在执行时出现了“Unable to load dll HRESUL ...
- How Tomcat Works(四)
Servlet容器有两个主要的模块,即连接器(connector)与容器(container),本文接下来创建一个连接器来增强前面文章中的应用程序的功能,以一种更优雅的方式来创建request对象和r ...
- WinForm控件使用文章收藏整理完成
对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...
- Linux下修改hostname
我维护两三个机房的数十台机器,开发用机器,运营用机器,自己工作机器也是ubuntu,有时开很多ssh,干的还是同样的事情,很容易搞混.所以需要一目了然的知道某台机器的情况,避免犯晕.这就需要修改主机名 ...
- 项目视频讲解_[HeyJava][尚学堂][CMS文章内容管理系统]
[HeyJava][尚学堂][CMS文章内容管理系统] http://pan.baidu.com/s/1c0imHrE
- 用Javascript编写Chrome浏览器插件
原文:http://homepage.yesky.com/62/11206062.shtml 用Javascript编写Chrome浏览器插件 2010-04-12 07:30 来源:天极网软件频道 ...
- C# try catch finally 执行
try { //dosomething eg: int a = 1; int b = 2; int c = a + b; if(c>2) { return; } } catch(Exceptio ...
- JSP页面上用横线代替文本框
<tr> <td class="content" colspan = 4> <input type="hidden" name=& ...