設計簡單的微博模型:用User類和Status類來模擬實現
在非ARC機制下有兩種方式,兩者沒有太大的區別之所以寫了兩種只是為了方便學習和對比兩種寫法!
第一種:沒有使用atuorelease和自動釋放池代碼容易理解,但是開發中一般不這么寫:
/****************************** User.h文件 **************************************//* 設計微博用戶模型: 姓名、微博號碼、密碼、頭像、性別、手機、生日 */#import <Foundation/Foundation.h>// 枚舉和結構體不用關心內存問題typedef enum { SexMan, // 男 SexWoman // 女} Sex;typedef struct { int year; int month; int day;} Date;@interface User : NSObject// 用戶姓名@PRoperty (nonatomic, retain) NSString *name;// 微博賬號@property (nonatomic, retain) NSString *account;// 微博密碼@property (nonatomic, retain) NSString *passWord;// 用戶頭像圖片 設置為圖片地址URL 比如: http://weibo.com/a.png@property (nonatomic, retain) NSString *icon;// 用戶性別@property (nonatomic, assign) Sex sex;// 用戶手機@property (nonatomic, retain) NSString *phone;// 用戶生日@property (nonatomic, assign) Date birthday;@end/****************************** User.m文件 **************************************/#import "User.h"@implementation User- (void)dealloc{ NSLog(@"User對象在釋放之前先釋放User對象引用的對象"); // 也可以理解為在User類中聲明的retain類型在User類銷毀后要全部銷毀 [_name release]; [_account release]; [_icon release]; [_password release]; [_phone release]; [super dealloc];}@end/****************************** Status.h文件 ***********************************//* 設計微博模型: 微博內容、微博配圖、發送時間、微博發送人、轉發的微博、被評論數、被轉發次數 */#import <Foundation/Foundation.h>#import "User.h"@interface Status : NSObject// 微博內容@property (nonatomic, retain) NSString *text;// 微博配圖 存放圖片地址@property (nonatomic, retain) NSString *icon;// 發送時間 計算機是從1970-01-01 00:00:00 開始計算到現在,一共度過了多少毫秒@property (nonatomic, assign) long time;// @property (nonatomic) time_t time; 時間也可以用這個// 微博發送人@property (nonatomic, retain) User *user;// 轉發的微博 依據面向對象的思想:將自己的類也封裝成屬性呢@property (nonatomic, retain) Status *retweetStatus;// 被評論數@property (nonatomic, assign) int commentsCount;// 被轉發次數@property (nonatomic, assign) int retweetsCount;@end/****************************** Status.m文件 ***********************************/#import "Status.h"@implementation Status- (void)dealloc{ NSLog(@"Status對象在釋放之前先釋放Status對象引用的對象"); [_text release]; [_user release]; [_retweetStatus release]; [_icon release]; [super dealloc];}@end/****************************** main.m文件 ***********************************/#import <Foundation/Foundation.h>#import "User.h"#import "Status.h"int main(){ // 新建兩個用戶 并設置用戶姓名 User *user1 = [[User alloc] init]; user1.name = @"張三"; User *user2 = [[User alloc] init]; user2.name = @"李斯"; // 新建兩條微博 并設置微博的屬性 Status *status1 = [[Status alloc] init]; status1.user = user1; // 這條微博是user1發出的 status1.text = @"今天真高興呀!"; Status *status2 = [[Status alloc] init]; status2.user = user2; // 這條微博是user2發出的 status2.text = @"非ARC基本掌握了!"; status2.retweetStatus = status1; // user2用戶在發出微博后又轉發了條微博 [status2 release]; [status1 release]; [user2 release]; [user1 release]; return 0;}
第二種:使用atuorelease和自動釋放池,在開發中建議的寫法
/****************************** User.h文件 **************************************//* 設計微博用戶模型: 姓名、微博號碼、密碼、頭像、性別、手機、生日 */#import <Foundation/Foundation.h>// 枚舉和結構體不用關心內存問題typedef enum { SexMan, // 男 SexWoman // 女} Sex;typedef struct { int year; int month; int day;} Date;@interface User : NSObject// 用戶姓名@property (nonatomic, retain) NSString *name;// 微博賬號@property (nonatomic, retain) NSString *account;// 微博密碼@property (nonatomic, retain) NSString *password;// 用戶頭像圖片 設置為圖片地址URL 比如: http://weibo.com/a.png@property (nonatomic, retain) NSString *icon;// 用戶性別@property (nonatomic, assign) Sex sex;// 用戶手機@property (nonatomic, retain) NSString *phone;// 用戶生日@property (nonatomic, assign) Date birthday;+ (instancetype)user;@end/****************************** User.m文件 **************************************/#import "User.h"@implementation User+ (instancetype)user{ return [[[self alloc] init] autorelease]; }- (void)dealloc{ NSLog(@"User對象在釋放之前先釋放User對象引用的對象"); [_name release]; [_account release]; [_icon release]; [_password release]; [_phone release]; [super dealloc];}@end/****************************** User.m文件 **************************************/#import "User.h"@implementation User+ (instancetype)user{ return [[[self alloc] init] autorelease]; }- (void)dealloc{ NSLog(@"User對象在釋放之前先釋放User對象引用的對象"); [_name release]; [_account release]; [_icon release]; [_password release]; [_phone release]; [super dealloc];}@end/****************************** User.m文件 **************************************/#import "User.h"@implementation User+ (instancetype)user{ return [[[self alloc] init] autorelease]; }- (void)dealloc{ NSLog(@"User對象在釋放之前先釋放User對象引用的對象"); [_name release]; [_account release]; [_icon release]; [_password release]; [_phone release]; [super dealloc];}@end/****************************** main.m文件 ***********************************/#import <Foundation/Foundation.h>#import "User.h"#import "Status.h"int main(){ @autoreleasepool { // 新建2個用戶 User *user1 = [User user]; user1.name = @"張三"; // NSString內部已經做好了封裝不用關心內存 只是需要在引用的時候做次釋放 User *user2 = [User user]; user2.name = @"李斯"; // 新建2條微博 Status *status1 = [Status status]; status1.text = @"今天真高興呀!"; status1.user = user1; Status *status2 = [Status status]; status2.text = @"非ARC基本掌握了!"; status2.retweetStatus = status1; status2.user = user2; } return 0;}
新聞熱點
疑難解答