• ObjectiveC学习之旅(四)内存管理2retain点语法


    一、retain属性的主要作用

      1、O-C内存管理和点语法

        1>OC内存管理正常情况要使用大量的retain和relrese操作

        2>点语法可以减少使用retain和release的操作  

    二、@property(retain)编译器如何申明

      编译器对于@property中的retain展开是不一样的

      主要是要释放上一次的值,增加本次计数器

      在dog.h中声明的:

      @property(retain)Dog *dog;

      展开后为:

      -(void) setDog:(Dog *)aDog;

      -(Dog *)dog;

    三、@synthesize编译器如何实现展开

      在dog.m中实现:

      @synthesize dog=_dog;

      展开后为:

      -(void) setDog:(Dog *)aDog{

        if(_dog!=aDog){

        [_dog release];

        _dog=[aDog retain];

        }

           }

      -(Dog *)dog{

        return _dog;

      }

    四、dealloc需要注意内容

      dealloc必须要释放dog(retain)

      在dog.m中

      -(void) dealloc

      {

        self.dog=nil;

        [super dealloc];

      }

    五、copy属性的主要作用

      copy属性是完全把对象重新拷贝了一份,计数器从新设置为1,和之前拷贝的数据完全脱离关系。

      @property(copy) NSString* str;

      //表示属性的getter函数

      -(double) str

      {

        return str;

      }

      //表示属性的setter函数

      -(void) setStr:(NSString *)newStr

      {

        str=[newStr copy];

      }

    六、assign,retain,copy

      1、foo=value;//简单的引用赋值

      2、foo=[value retain];//引用赋值,并且增加value的计数器

      3、foo=[value copy];//将value复制了一份给foo,复制后,foo和value就毫无关系

    Person.h中的代码

    View Code
    #import <Foundation/Foundation.h>
    #import "Dog.h"
    @interface Person : NSObject
    {
        Dog *_dog;
    }
    //- (void) setDog:(Dog *)aDog;
    //- (Dog *) dog;
    @property (retain) Dog *dog;
    
    
    @end

    Person.m

    View Code
    #import "Person.h"
    
    @implementation Person
    @synthesize dog = _dog;
    //- (void) setDog:(Dog *)aDog
    //{
    //    if (aDog != _dog) {
    //        [_dog release];
    //        
    //        _dog = [aDog retain];
    //        //让_dog技术器 +1
    //    }
    //}
    //- (Dog *) dog
    //{
    //    return _dog;
    //}
    
    - (void) dealloc
    {
        NSLog(@"person is dealloc");
        // 把人拥有的_dog释放
        //[_dog release], _dog = nil;
        self.dog = nil;
        //[self setDog:nil];
        [super dealloc];
    }
    
    @end

    Dog.h

    View Code
    #import <Foundation/Foundation.h>
    
    @interface Dog : NSObject
    {
        int _ID;
    }
    @property int ID;
    
    @end

    Dog.m

    View Code
    #import "Dog.h"
    
    @implementation Dog
    
    @synthesize ID = _ID;
    
    - (void) dealloc
    {
        NSLog(@"dog %d is dealloc", _ID);
        [super dealloc];
    }
    @end

    main.m

    View Code
    #import <Foundation/Foundation.h>
    #import "Person.h"
    #import "Dog.h"
    // Person
    // Dog.
    // Person Dog.
    
    int main (int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSLog(@"Hello, World!");
            
            Dog *dog1 = [[Dog alloc] init];
            [dog1 setID:1];
            
            Dog *dog2 = [[Dog alloc] init];
            [dog2 setID:2];
            
            Person *xiaoLi = [[Person alloc] init];
            [xiaoLi setDog:dog1];
            
            [xiaoLi setDog:dog2];
            
            [dog1 release];
            [xiaoLi release];
            [dog2 release];
            
    #if 0
            Dog *dog1 = [[Dog alloc] init];
            [dog1 setID:1];
            
            Person *xiaoLi = [[Person alloc] init];
            // 小丽要遛狗
            [xiaoLi setDog:dog1];
            
            Person *xiaoWang = [[Person alloc] init];
            [xiaoWang setDog:dog1];
            
            NSLog(@"dog1 retain count is %ld", 
                  [dog1 retainCount]);
            // dog1 retain count is 3
            
            [dog1 release];
            NSLog(@"dog1 retain count2 is %ld", 
                  [dog1 retainCount]);
            // dog1 retain count2 is 2
            
            [xiaoWang release];
            
            NSLog(@"dog1 retain count3 is %ld", 
                  [dog1 retainCount]);
            // person is dealloc
           //  dog1 retain count3 is 1
        
            [xiaoLi release];
    
            // person is dealloc
            // dog 1 is dealloc
    #endif
    
        }
        return 0;
    }
  • 相关阅读:
    简单的java socket 示例
    Java Socket编程
    session的常用方法。
    Request与session与application的区别
    百度echart使用心得,百度图表。
    SpingMVC ModelAndView, Model,Control以及参数传递
    Flume理论研究与实验
    RocketMq在SparkStreaming中的应用总结
    数据实践过程中理论总结
    flink-vs.-spark
  • 原文地址:https://www.cnblogs.com/caishuhua226/p/2829877.html
Copyright © 2020-2023  润新知