// // main.m // 多个对象内存管理练习 // // ARC是Xcode帮我们生成内存释放的代码,MRC是需要我买自己写retain和release。想研究内存管理只能在MRC,管理对象就是在管理引用计数器,计数器为0对象就释放。 // 给空指针发消息不会报错,给野指针发消息就会报错。 // 对象与对象之间有关系时候就用retain。 #import <Foundation/Foundation.h> #import "Status.h" int main(int argc, const char * argv[]) { /* 模拟场景: * 老王在2010-1-1 17:56:34注册了一个账号 (名称:xiaomage@520it.com, 密码:haomage) * 老王的生日是1986-3-8 18:18:18 * 老王发布一条说说 * 文字内容 @“爆米花手机比逼格更有逼格” * 图片 @“phone.png” * 发表时间: 2015-6-20 10:23:23 * 作者: 老王 * 被转发的说说: 没有 * 评论数: 100 * 转发数: 90 * 点赞数: 200 * 王大锤在2012-8-8 19:26:54注册了一个账号 (名称:dachuimeimei@520it.com, 密码:654321) * 王大锤的生日是1989-9-6 14:16:28 * 王大锤在2015-6-21 20:47:09时,转发了老王之前发布的说说, 并且还附带了一句话:@“真的很有逼格” */ /* 至少应该有三个类: 账号类(Account): 注册的时间(registerTime) 账号(email) 密码(pwd) 用户类(Author): 用户昵称(name) 用户头像(icon) 用户是否是会员(vip) 用户对应的账号(account) 用户的生日(birthday) 微博类(Status): 微博正文(text) 微博配图(picture) 微博发布的时间(createTime) 微博对应的作者(用户)(author) 评论数(commentCount) 转发数(retweetCount) 赞数(likeCount) 转发微博(repostStatus) 微博中有用户, 用户中有账号 1.账号 2.用户 3.微博 */ // 1.给老王创建账号 Account *lwAccount = [[Account alloc] init]; lwAccount.email = @"xiaomage@520it.com"; lwAccount.pwd = @"haomage"; lwAccount.registerTime = (MyDate){2010, 1, 1, 17, 56, 34}; // 2.根据账号设置用户信息 Author *lwAuthor = [[Author alloc] init]; lwAuthor.name = @"老王"; lwAuthor.icon = @"lw.png"; lwAuthor.vip = YES; lwAuthor.account = lwAccount; lwAuthor.birthday = (MyDate){1986, 3, 8, 18, 18, 18}; // 3.发布微博 Status *lwStatus = [[Status alloc] init]; lwStatus.text = @"爆米花手机比逼格更有逼格"; lwStatus.picture = @"phone.png"; lwStatus.createTime = (MyDate){2015, 6, 20, 10, 23, 23}; lwStatus.author = lwAuthor; lwStatus.commentCount = 100; lwStatus.retweetCount = 90; lwStatus.likeCount = 200; // 1.给王大锤创建账号 Account *dcAccount = [[Account alloc] init]; dcAccount.email = @"dachuimeimei@520it.com"; dcAccount.pwd = @"654321"; dcAccount.registerTime = (MyDate){2012, 8, 8, 19, 26, 54}; // 2.根据账号设置用户信息 Author *dcAuthor = [[Author alloc] init]; dcAuthor.name = @"王大锤"; dcAuthor.icon = @"wdc.png"; dcAuthor.vip = NO; dcAuthor.account = dcAccount; dcAuthor.birthday = (MyDate){1989, 9, 6, 14, 16, 28}; // 3.发布微博 Status *dcStatus = [[Status alloc] init]; dcStatus.text = @"真的很有逼格"; dcStatus.picture = nil; dcStatus.createTime = (MyDate){2015, 6, 21, 20, 47, 9}; dcStatus.author = dcAuthor; dcStatus.commentCount = 0; dcStatus.retweetCount = 0; dcStatus.likeCount = 0; dcStatus.repostStatus = lwStatus; [lwAccount release]; [lwAuthor release]; [lwStatus release]; [dcAccount release]; [dcAuthor release]; [dcStatus release]; return 0; }
// Status.h #import <Foundation/Foundation.h> #import "Author.h" @interface Status : NSObject @property(nonatomic, retain)NSString *text; @property(nonatomic, retain)NSString *picture; @property(nonatomic, assign)MyDate createTime; //结构体诗基本数据类型 @property(nonatomic, retain)Author *author; @property(nonatomic, assign)int commentCount; @property (nonatomic, assign) int retweetCount; @property(nonatomic, assign)int likeCount; @property(nonatomic, retain)Status *repostStatus; @end
// // Status.m #import "Status.h" @implementation Status - (void)dealloc { NSLog(@"%s", __func__); /* [_text release]; _text = nil; //释放完了之后要致为空 [_picture release]; _picture = nil; [_author release]; _author = nil; [_repostStatus release]; _repostStatus = nil; */ // 由于加了retain,下面点语法相当于调用了set方法:先release旧值, 然后再将新值赋值给属性 self.text = nil; self.picture = nil; self.author = nil; self.repostStatus = nil; [super dealloc]; } /* self.text = nil: - (void)setText:(NSString *)text // nil { // 加入上一次的值是@"abc"; if (_text != text) { [_text release];//release旧值 _text = [text retain]; // 新值加1后赋值过来,_text = nil; } }*/ @end
// // Author.h #import <Foundation/Foundation.h> #import "Account.h" @interface Author : NSObject @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
// // Author.m #import "Author.h" @implementation Author - (void)dealloc { NSLog(@"%s", __func__); [_name release]; [_icon release]; [_account release]; [super dealloc]; } @end
// Account.h #import <Foundation/Foundation.h> typedef struct{ int year; int month; int day; int hour; int minute; int second; } MyDate; @interface Account : NSObject @property(nonatomic, assign)MyDate registerTime; @property(nonatomic, retain)NSString *email; // copy @property(nonatomic, retain)NSString *pwd; @end
// // Account.m #import "Account.h" @implementation Account -(void)dealloc { NSLog(@"%s", __func__); [_email release]; [_pwd release]; [super dealloc]; } @end