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 ...
随机推荐
- ASP.NET的分页方法(三)
第三讲是应用于MVC上面的分页,很多时候MVC要和EF表达式一起使用,但是我这个使用的最原始的ADO.NET,一样实现了这个效果.要实现这个效果首先要得到MvcPager.dll,引用之后,前台操作如 ...
- react native listview 一个有用的属性,用作两列布局
contentContainerStyle:设置listview包裹内容的属性 <ListView contentContainerStyle={{flexDirection:'row',fle ...
- static和extern的区别
extern(外部) 1.对函数 完整的定义一个外部函数(可以省略extern) 完整的声明一个外部函数(可以省略extern) 2.对变量 只能声明一个外部变量(不能省 ...
- yii缓存设置使用
'filecache'=>array( 'class'=>'system.caching.CFileCache', 'directoryLevel'=>'3',), //在main. ...
- ASP.NET|跳转(redirect)到其它站点,提示:当前线程终止。
在XAF中,如果使用HttpContext.Response.Redirect()进行跳转,会出现”当前线程终止“的报错,跳转失败. 这时候,应该改用WebApplication.Redirect() ...
- 实现带查询功能的Combox控件
前言 ComBox 还可以实现查询功能,通过设置 ComBox 控件的 AutoCompleteSource 属性和 AutoCompleteMode 属性,可以实现从 Combox 控件中查询已存在 ...
- GetSafeHwnd()函数解释[转]
当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数,通过下面的例子来看其理由: CWnd *pwnd = FindWindow(“ ...
- JSP+Servlet+JavaBean
MVC是三个单词的缩写:M,Model(模型):V,View( 视图 ),C,Control(控制). MVC模式的目的就是实现Web系统的职能分工, Model层:实现系统的业务逻辑,即javaBe ...
- Flex坐标
flash和flex针对不同的目的,提供了3种不同的坐标系. 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容坐标系(相对于本地坐标系说的) 这些坐标系的点是可以转换的,并且有相应的方法 ...
- myeclipse、eclipse中项目复制后(可能无法访问)注意事项 .
.myEclipse 复制后修改名称,访问不到项目 这是因为,你只是改了项目的名称,而没有改 下面是解决方法: 方法 1.右击你的项目,选择“properties”,在“type filter tex ...