美文网首页八天学会OC
第05天OC语言(09):多个对象内存管理练习实现

第05天OC语言(09):多个对象内存管理练习实现

作者: liyuhong | 来源:发表于2017-07-22 08:57 被阅读8次
    • 不要等到明天,明天太遥远,今天就行动。
    须读:看完该文章你能做什么?

    能够管理好多个类之间的内存管理,学习一些逼格的代码

    学习前:你必须会什么?(在这里我已经默认你具备C语言的基础了)

    什么是类,
    怎么实现setter/getter方法(内存管理下).
    @property关键字的修饰符的<内部实现>是如何处理setter/getter方法的


    一、本章笔记
     一、
        在dealloc里面写上self.text = nil;
        相当于
             // 给nil发送 retain release消息是不会报错
             - (void)setText:(NSString *)text // nil
             {
             //假如上一次的值 @"abc"
             //传进来的值 是 nil
             if (_text != text) { // abc !=nil
             [_text release]; // [_abc release];
             _text = [text retain]; // _text = [nil retain];
             }
             }
    
    
    二、code
    main.m
    #pragma mark 09-多个对象内存管理练习实现
    #pragma mark 概念
    /*
     一、
        在dealloc里面写上self.text = nil;
        相当于
             // 给nil发送 retain release消息是不会报错
             - (void)setText:(NSString *)text // nil
             {
             //假如上一次的值 @"abc"
             //传进来的值 是 nil
             if (_text != text) { // abc !=nil
             [_text release]; // [_abc release];
             _text = [text retain]; // _text = [nil retain];
             }
             }
     */
    #pragma mark - 代码
    #import <Foundation/Foundation.h>
    #pragma mark 类
    
    #import "Status.h"
    
    #pragma mark - main函数
    int main(int argc, const char * argv[])
    {
        /*
         模拟场景:
         * lyh 在 2012-1-1 22:30:44 注册了一个账号
         (名称: lyh@163.com 密码:123456)
         * lyh的生日 是 1992-12-12 23:22:44
         * lyh 发布一条说说
         * 文字内容 @"学学 以色列"
         * 图片 @"test.png"
         * 发表时间 : 2013-12-11 23:21:45
         * 作者: lyh
         * 被转发的说说 : 没有
         * 评价数 :  100
         * 转发 :  33
         * 点赞数 :  222
         
         
         
         * lys 在 2013-1-1 22:10:33 注册了一个账号
         {名称 : lys@163.com 密码: 123456}
         * lys 的生日1989 - 3 - 4 ,16: 14: 22
         lys 早2015-6  13: 12 :44
         
         lys 在 2015-12-23 20:44:23时, 换发了lyh之前发布的说说, 并且还附带了一句话  @“会玩呀”
         
         */
        
        
    #warning 分析 拆分类
        /**
         至少应该有三个类:
         账号类(Account)
         注册的时间 (registerTime)
         账号      (email)
         密码      (pwd)
         
         用户类:(Author)
         用户的昵称   (name)
         用户的头像   (icon)
         用户的是否是会员 (vip)
         用户对应的账号 (account)
         用户的生日 (用来做营销, 到生日的时候 提示或者送一些其他小礼物) (birthday)
         
         微博类:   (Status)
         微博的正文   (text)
         微博配图     (pricture)  (可能有一张、多张、高清、中图、小图)(可以抽取为一个类)
         微博的发布时间 (createTime)
         微博对应的作者(用户)(author)
         评论数             (commoentCount)
         转发数             (retweetCount)
         赞数              (likeCount)
         转发微博            (repostStatus)
         
         微博中用户, 用户中有账号
         顺序
         1. 账号  2.用户  3.微博
         
         */
     
    #pragma 1.
        // 1.创建lyh账号
        Account *lyhAc = [[Account alloc]init];
        lyhAc.email = @"lyh@163.com";
        lyhAc.pwd = @"123456";
        lyhAc.registerTime = (myDate){2012,1,1,22,30,44};
        // 2.根据账号设置用户信息
        Author *lyhAuthor = [[Author alloc]init];
        lyhAuthor.name = @"lyh";
        lyhAuthor.icon = @"lyh.png";
        lyhAuthor.vip = YES;
        lyhAuthor.account = lyhAc;
        lyhAuthor.birthday =  (myDate){1992,12,12,23,22,44};
        // 3.发布微博
        Status *lyhStatus = [[Status alloc]init];
        lyhStatus.text = @"学学 以色列";
        lyhStatus.pricture = @"test.png";
        lyhStatus.createTime = (myDate){2013,12,11,23,21,45};
        lyhStatus.author = lyhAuthor;
        lyhStatus.commoentCount = 100;
        lyhStatus.retweetCount = 33;
        lyhStatus.likeCount = 222;
        
    #pragma 1.
        /*
         lys 在 2013-1-1 22:10:33 注册了一个账号
         {名称 : lys@163.com 密码: 123456}
         * lys 的生日1989 - 3 - 4 ,16: 14: 22
         lys 早2015-6  13: 12 :44
         
         lys 在 2015-12-23 20:44:23时, 换发了lyh之前发布的说说, 并且还附带了一句话  @“会玩呀”
         */
        // 1.给lys创建账号
        Account *lysAc = [[Account alloc]init];
        lysAc.email = @"lys@163.com";
        lysAc.pwd = @"123456";
        
        // 2.根据账号设置用户信息
        Author *lysAuthor = [[Author alloc]init];
        lysAuthor.name = @"lyh";
        lysAuthor.icon = @"lyh.png";
        lysAuthor.vip = NO;
        lysAuthor.account = lysAc;
        lysAuthor.birthday = (myDate){1989,3,4,16,14,22};
        // 3.发布微博
        Status *lysStatus = [[Status alloc]init];
        lysStatus.text = @"会玩呀";
        lysStatus.pricture = nil;
        lysStatus.createTime = (myDate){2013,12,11,23,21,45};
        lysStatus.author = lysAuthor;
        lysStatus.commoentCount = 0;
        lysStatus.retweetCount = 0;
        lysStatus.likeCount = 0;
        lysStatus.repostStatus = lyhStatus;
        
        [lyhAc release];
        [lyhAuthor release];
        [lyhStatus release];
        
        [lysAc release];
        [lysAuthor release];
        [lysStatus release];
        return 0;
    }
    
    
    Account
    >>>.h
    #import <Foundation/Foundation.h>
    
    typedef struct {
        int yaer;
        int month;
        int day;
        int hour;
        int minute;
        int second;
    }myDate;
    
    @interface Account : NSObject
    /*
     注册的时间 (registerTime)
     账号      (email)
     密码      (pwd)
     */
    @property (nonatomic,assign) myDate registerTime;
    @property (nonatomic,retain) NSString *email;
    @property (nonatomic,retain) NSString *pwd;
    
    @end
    
    >>>.m
    #import "Account.h"
    
    @implementation Account
    
    - (void)dealloc
    {
        NSLog(@"%s",__func__);
        [_email release];
        [_pwd release];
        [super dealloc];
    }
    @end
    
    
    Author
    >>>.h
    #import <Foundation/Foundation.h>
    #import "Account.h"
    @interface Author : NSObject
    /*
     用户的昵称   (name)
     用户的头像   (icon)
     用户的是否是会员 (vip)
     用户对应的账号 (account)
     用户的生日 (用来做营销, 到生日的时候 提示或者送一些其他小礼物) (birthday)
     */
    
    @property (nonatomic,retain) NSString *name;
    @property (nonatomic,retain) NSString *icon;
    @property (nonatomic,assign,getter=isVip) BOOL vip;
    @property (nonatomic,retain) Account *account;
    @property (nonatomic,assign) myDate birthday;
    
    @end
    
    >>>.m
    #import "Author.h"
    
    @implementation Author
    
    - (void)dealloc
    {
        NSLog(@"%s",__func__);
        [_name release];
        [_icon release];
        [_account release];
        [super dealloc];
    }
    
    @end
    
    
    Status
    >>>.h
    #import <Foundation/Foundation.h>
    #import "Author.h"
    
    @interface Status : NSObject
    /*
     微博类:   (Status)
     微博的正文   (text)
     微博配图     (pricture)  (可能有一张、多张、高清、中图、小图)(可以抽取为一个类)
     微博的发布时间 (createTime)
     微博对应的作者(用户)(author)
     评论数             (commoentCount)
     转发数             (retweetCount)
     赞数              (likeCount)
     转发微博            (repostStatus)
     */
    @property (nonatomic,retain) NSString *text;
    @property (nonatomic,retain) NSString *pricture;
    @property (nonatomic,assign) myDate createTime;
    @property (nonatomic,retain) Author *author;
    @property (nonatomic,assign) int commoentCount;
    @property (nonatomic,assign) int retweetCount;
    @property (nonatomic,assign) int likeCount;
    @property (nonatomic,retain) Status *repostStatus;
    
    @end
    
    >>>.m
    #import "Status.h"
    
    @implementation Status
    
    
    
    - (void)dealloc
    {
        NSLog(@"%s",__func__);
    #warning 新手装逼写法
        /*
        [_text release];
        _text = nil;
        
        [_pricture release];
        _pricture = nil;
        
        [_author release];
        _author = nil;
        
        [_repostStatus release];
        _repostStatus = nil;
         */
        
    #warning 高手装逼写法
        /*
         下面这句话 相当于调用了set方法
         先release 旧值,  然后再将新值 赋值给属性
         */
        self.text = nil;
        self.pricture = nil;
        self.author = nil;
        self.repostStatus = nil;
        
        /*
         // 上面的 self.text = nil; 等同下面的
         // 逼格
         // 给nil发送 retain release消息是不会报错
         - (void)setText:(NSString *)text // nil
         {
         //      假如上一次的值 @"abc"
         //      传进来的值 是 nil
         if (_text != text) {
         [_text release];
         _text = [text retain]; // _text = nil
         }
         }
         */
        [super dealloc];
    }
    @end
    
    

    相关文章

      网友评论

        本文标题:第05天OC语言(09):多个对象内存管理练习实现

        本文链接:https://www.haomeiwen.com/subject/qlsohxtx.html