OC语言day05-09多个对象内存管理练习实现

2016-06-12  本文已影响20人  liyuhong165

pragma mark 多个对象内存管理练习实现

pragma mark 概念

/**
 类 就是用来存储数据的
 
 */

pragma mark 代码

#pragma mark 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#include "Status.h"
#pragma mark main函数
int main(int argc, const char * argv[])
{
    
#warning 练习
    /*
     模拟场景:
     * 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.微博
     
     */
    
    
    // 1.给老王创建帐号
    Account *lwAccount = [[Account alloc]init];
    lwAccount.email = @"liyuhong165@163.com";
    lwAccount.pwd = @"123";
    lwAccount.registerTime = (MyDate){2001,1,1,22,15,11}; // 将右边的结构体一一赋值给左边的结构, 如果不强制转换会报错的,因为结构的初始化值是0
    
    // 2.根据帐号设置用户信息
    Author *lwAuthor = [[Author alloc]init];
    lwAuthor.name = @"老王";
    lwAuthor.icon = @"lw.png";
    lwAuthor.vip = YES;
    lwAuthor.account = lwAccount;
    lwAuthor.birthday = (MyDate){1993,1,1,22,15,11};
    
    // 3.发布微博
    Status *lwStatus = [[Status alloc]init];
    lwStatus.text = @"会玩呀";
    lwStatus.pricture = @"p.png";
    lwStatus.createTime = (MyDate){2001,1,1,22,15,11}; ;
    lwStatus.author = lwAuthor;
    lwStatus.commoentCount = 100;
    lwStatus.retweetCount = 90;
    lwStatus.likeCount = 200;
    
    
#warning 6-12晚上
    
    // 给王大锤常见帐号/
    Account *dcAccount = [[Account alloc]init];
    lwAccount.email = @"王大锤";
    lwAccount.pwd = @"123";
    lwAccount.registerTime = (MyDate){2003,1,1,22,15,11}; // 将右边的结构体一一赋值给左边的结构, 如果不强制转换会报错的,因为结构的初始化值是0

    // 2.根据帐号设置用户信息
    Author *dcAuthor = [[Author alloc]init];
    dcAuthor.name = @"老王";
    dcAuthor.icon = @"lw.png";
    dcAuthor.vip = YES;
    dcAuthor.account = dcAuthor;
    dcAuthor.birthday = (MyDate){1993,1,1,22,15,11};
    
    // 3.发布微博
    Status *lwStatus_s = [[Status alloc]init];
    lwStatus_s.text = @"会玩呀";
    lwStatus_s.pricture = @"nil";
    lwStatus_s.createTime = (MyDate){2001,1,1,22,15,11}; ;
    lwStatus_s.author = dcAuthor;
    lwStatus_s.commoentCount = 100;
    lwStatus.retweetCount = 90;
    lwStatus_s.likeCount = 200;
    lwStatus_s.repostStatus = lwStatus;//
    
    [lwStatus release];
    [NSData release];
    return 0;
}
Account.h //帐号类
// 帐号类
#import <Foundation/Foundation.h>

typedef struct
{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

}MyDate;

@interface Account : NSObject

//注册的时间 (registerTime)  [如果学完Foundation框架 最好使用字符串来保持]
@property (nonatomic,assign)MyDate registerTime; // 由于结构体不是对象, 所以直接使用assign即可

//账号      (email)
@property (nonatomic,retain) NSString *email; // 以后字符串最好用copy

//密码      (pwd)
@property (nonatomic,retain) NSString *pwd;

@end
Account.m
#import "Account.h"

@implementation Account

- (void)dealloc
{
    [_email release];
    [_pwd release];
    [super dealloc];
}

@end

Account.h //用户类
// 用户类
#import <Foundation/Foundation.h>
#import "Account.h"


@interface Author : NSObject

//用户的昵称   (name)
@property(nonatomic, retain)NSString *name;

//用户的头像   (icon)
@property(nonatomic, retain)NSString *icon;

//用户的是否是会员 (vip)
@property(nonatomic, assign,getter=isVip) BOOL vip; //如果是BOOL类型,一般情况下重写getter方法

//用户对应的账号 (account)
@property (nonatomic, retain)Account *account;

//用户的生日 (用来做营销, 到生日的时候 提示或者送一些其他小礼物) (birthday)
@property (nonatomic,assign)MyDate birthday;

@end
Author.m
#import "Author.h"

@implementation Author

-(void)dealloc
{
    [_name release];
    [_icon release];
    [_account release];
    [super dealloc];
    
}

@end

Status.h //微博类
#import <Foundation/Foundation.h>
#import "Author.h"

@interface Status : NSObject

//微博的正文   (text)
@property(nonatomic, retain)NSString *text;

//微博配图     (pricture)  (可能有一张、多张、高清、中图、小图)(可以抽取为一个类)
@property(nonatomic, retain)NSString *pricture;

//微博的发布时间 (createTime)
@property (nonatomic,assign)MyDate createTime;

//微博对应的作者(用户)(author)
@property (nonatomic, retain)Author *author;

//评论数             (commoentCount)
@property (nonatomic,assign)int commoentCount;

//转发数             (retweetCount)
@property (nonatomic,assign)int retweetCount;

//赞数              (likeCount)
@property (nonatomic,assign)int likeCount;

//转发微博            (repostStatus)
@property (nonatomic,retain)Status *repostStatus;


@end
Status.m
#import "Status.h"

@implementation Status

#warning 系统默认的setter实现
- (void)setText:(NSString *)text
{
    // 假如上一次的@"abc",
    if (_text != text) {
        [_text release];
        _text = [text retain];
    }
}

- (void)dealloc
{
    // 属性是retain的都需要release
    /*
    [_text release];
    _text = nil;
    
    [_pricture release];
    _pricture = nil;
    
    [_author release];
    _author = nil;
    
    [_repostStatus release];
    _repostStatus = nil;
     */
    
    // 下面这句话相当于调用了set方法
    // 先release旧值,
    self.text = nil;
    self.pricture = nil;
    [super dealloc];
}

@end

上一篇 下一篇

猜你喜欢

热点阅读