COPY
Woman *w1 = [woman copy]; // w1 和 woman 的丈夫是同一个人 // 把 woman 的所有拷贝给 w1
[w1 live];
[w1 release];
[woman release];
[man release];
[boss release];
//输出 woman 和 w1 的地址
NSLog(@"%p", woman);
NSLog(@"%p", w1);
*/
/*
//初始化对象
Mother *mother = [[Mother alloc] init];
Student *stu = [[Student alloc] init];
Doctor *doctor = [[Doctor alloc] init];
Teacher *teacher = [[Teacher alloc] init];
//建立代理人关系
[mother setTeacher:teacher];
[mother setStudent:stu];
[mother setDoctor:doctor];
[mother live];
//释放内存
[stu release];
[doctor release];
[teacher release];
[mother release];
*/
NSString *string = [NSStringstringWithFormat:@"%@%d",@"abc", 123];
NSLog(@"%@", string);
//不可变字符串对其发送拷贝消息 跟retain是一样的,只是对原来计时器加一,不会创建新的父本
NSString *s1 = [string copy];
NSLog(@"%p", string);
NSLog(@"%p", s1);
//可变拷贝 mutableCopy //只能作为方法用,不能放在属性中,属性中只能放retain,copy,assign
NSMutableString *s2 = [string mutableCopy];
[s2 insertString:@"iii"atIndex:0];
NSLog(@"%p", s2);
//打印string的类名
NSLog(@"%@", [stringclassName]);
NSLog(@"%@", [s1className]);
NSLog(@"%@", [s2className]);
NSLog(@"%@", s2);
//规则
// 1.对不可变字符串发送copy消息,跟retain一样
// 2.对可变字符串发送copy消息,会拷贝出一个全新的不可变字符串出来
// 3.无论对可变或不可变字符串发送mutableCopy的消息,拷贝出来的对象都是全新的不可变的字符串
NSString *s3 = [s2 copy];
NSLog(@"%p", s3);
NSLog(@"%@", [s3class]);
//assign:基础变量
//retain:对象
//copy:字符串对象
Woman.m
#import "Woman.h"
@implementation Woman
- (void)makeUp
{
NSLog(@"making up");
}
- (void)makeMoney
{
NSLog(@"making money");
}
- (void)shopping
{
NSLog(@"happy shopping");
}
- (void)washCloth
{
NSLog(@"washing cloth");
}
- (void)cook
{
NSLog(@"cooking");
}
- (void)makeDown
{
NSLog(@"making down");
}
- (void)live
{
[selfmakeUp];
[self.husbandmakeMoney];
[selfshopping];
if ([(NSObject *)self.husbandrespondsToSelector:@selector(washCloth)]) {
//@selector(washCloth)相当于确认了方法名
[self.husbandwashCloth];
}else {
[selfwashCloth];
}
if ([(NSObject *)self.husbandrespondsToSelector:@selector(cook)]) {
[self.husbandcook];
}else {
[selfcook];
}
[selfmakeDown];
}
//copy/init/alloc/开头的方法(我们不允许使用其做字头) 产生引用计数器加一 在外面手动释放
- (id)copyWithZone:(NSZone *)zone
{
Woman *woman = [[WomanallocWithZone:zone] init];
[womansetHusband:self.husband]; //属性一起拷贝
return woman;
}
#import <Foundation/Foundation.h>
#import "MarrayProtocol.h"
@interface Woman :NSObject <NSCopying>
@property (nonatomic,assign) id<MarrayProtocol> husband;
- (void)live;
- (void)makeUp;
- (void)makeMoney;
- (void)shopping;
- (void)washCloth;
- (void)cook;
- (void)makeDown;
@end
MarrayProtocol.h
#import <Foundation/Foundation.h>
@protocol MarrayProtocol
@required //必须的
- (void)makeMoney;
@optional //可选的
- (void)washCloth;
- (void)cook;
@end
#import <Foundation/Foundation.h>
#import "MarrayProtocol.h"
#import "Assistant.h"
@interface Man : NSObject <MarrayProtocol,Assistant>
- (void)drink;
@end
#import "Man.h"
@implementation Man
- (void)makeMoney
{
NSLog(@"man making money");
}
- (void)washCloth
{
NSLog(@"man washing cloth");
}
- (void)drink
{
NSLog(@"happy drinking");
}
- (void)draft
{
NSLog(@"man draft");
}
- (void)sendMail
{
NSLog(@"man sendMail");
}
#import "Woman.h"
@implementation Woman
- (void)makeUp
{
NSLog(@"making up");
}
- (void)makeMoney
{
NSLog(@"making money");
}
- (void)shopping
{
NSLog(@"happy shopping");
}
- (void)washCloth
{
NSLog(@"washing cloth");
}
- (void)cook
{
NSLog(@"cooking");
}
- (void)makeDown
{
NSLog(@"making down");
}
- (void)live
{
[self makeUp];
[self.husband makeMoney];
[self shopping];
if ([(NSObject *)self.husband respondsToSelector:@selector(washCloth)]) {
//@selector(washCloth)相当于确认了方法名
[self.husband washCloth];
} else {
[self washCloth];
}
if ([(NSObject *)self.husband respondsToSelector:@selector(cook)]) {
[self.husband cook];
} else {
[self cook];
}
[self makeDown];
}
//copy/init/alloc/开头的方法(我们不允许使用其做字头) 产生引用计数器加一 在外面手动释放
- (id)copyWithZone:(NSZone *)zone
{
Woman *woman = [[Woman allocWithZone:zone] init];
[woman setHusband:self.husband]; //属性一起拷贝
return woman;
}