OC7_ Composite Memory Management (setter method)

Source: Internet
Author: User

////Person.h//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"@interfaceperson:nsobject{Dog*_dog;}-(DOG *) Dog;- (void) Setdog: (Dog *) Dog;@end
////person.m//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "Person.h"@implementation Person-(DOG *) dog{return_dog;}//method One://-(void) Setdog: (dog *) Dog//{//_dog = dog;//}//Method Two://-(void) Setdog: (dog *) Dog//{//_dog = [dog retain];//}//Method Three:- (void) Setdog: (Dog *) dog//Xiaohei _dog count = 1;{[_dog release];//0//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");//NSLog (@ "Lajfalfjal");//NSLog (@ "JSLFJSLFJL");_dog =[dog retain];}//Standard notation//-(void) Setdog: (dog *) Dog//{//if (_dog!=dog) {//[_dog release];//_dog = [dog retain];//    }//}- (void) dealloc{[_dog release]; [Super Dealloc];}@end
// //   Dog.h//  oc7_ Composite Memory Management (setter method)////  Created by Zhangxueming on 15/6/18. //   Copyright (c) 2015 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h>@interface  dog:nsobject@end
////DOG.M//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "Dog.h"@implementationDog//-(void) Dealloc//{//    //NSLog (@ "12321313");//[Super Dealloc];//}@end
////main.m//oc7_ Composite Memory Management (setter method)////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"#import "Person.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Dog*xiaobai =[[Dog alloc] init]; person*xiaoxin =[[Person alloc] init]; //method One: Not OK, the person does not really hold the dog, if in the main function [Xiaobai release], let dog's reference count minus 1, will become 0,dog destroyed//Xiaoxin.dog = Xiaobai;//NSLog (@ "Retaincount =%li", xiaobai.retaincount);//[Xiaobai release];//       //[Xiaoxin release]; //Method Two://not OK, if people hold another dog, it will cause the first dog not to release, memory leaks. //Xiaoxin.dog = Xiaobai;//NSLog (@ "Xiaobai Retaincount =%li", xiaobai.retaincount);//Dog *xiaohei = [[Dog alloc] init];//Xiaoxin.dog = Xiaohei;//[Xiaobai release];//[Xiaohei release];//[Xiaoxin release]; //method Three: Not OK, if it was to hold a dog, and reset the dog, first to release, this time, it is likely that the dog destroyed, and then, can not be retain again. Xiaoxin.dog =Xiaobai; Dog*xiaohei = Xiaobai;//[Xiaobai retain]NSLog (@"Xiaohei retaincount =%li", Xiaobai.retaincount);       [Xiaobai release]; NSLog (@"Xiaohei retaincount =%li", Xiaohei.retaincount); Xiaoxin.dog=Xiaohei;                            [Xiaoxin release]; }    return 0;}

OC7_ Composite Memory Management (setter method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.