zoukankan      html  css  js  c++  java
  • OC4_内存管理法则

    //
    //  Dog.h
    //  OC4_内存管理法则
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Dog : NSObject
    
    @end
    //
    //  Dog.m
    //  OC4_内存管理法则
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "Dog.h"
    
    @implementation Dog
    
    @end
    //
    //  main.m
    //  OC4_内存管理法则
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Dog.h"
    //对于引用计数来说,有一套内存管理的黄金法则:
    //The basic rule to apply is everything that increases the reference counter with alloc, [mutable]copy[withZone:] or retain is in charge of the corresponding [auto]release.
    //如果对一个对象使用了alloc、[mutable]copy、retain,那么你必须使用相应的release或者autorelease。
    //通俗一点的说法就是谁污染谁治理,谁杀的谁埋。
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Dog *xiaoHei = [[Dog alloc] init];//1
            Dog *xiaoBai = [xiaoHei retain];
            Dog *xiaoHui = [xiaoHei copy];//copy用于字符串,不能用于对象,所有这里报错.
            
            [xiaoHei release];
            [xiaoBai release];
            [xiaoHui autorelease];
        }
        return 0;
    }
  • 相关阅读:
    Web开发技术大会演讲
    方法的长度
    避免复制与粘贴
    引入工厂方法支持扩展
    利用多态重构为带参方法
    代码质量对系统的影响
    《重构之美》开篇
    老鸟对菜鸟的一些建议
    jQuery 使用 AJAX解决跨域问题jQuery 使用 AJAX解决跨域问题
    uniapp renderjs通信
  • 原文地址:https://www.cnblogs.com/0515offer/p/4586950.html
Copyright © 2011-2022 走看看