Summary of the basic memory management series of iOS non-ARC 6-design of Weibo Model

Source: Internet
Author: User

A simple microblog model is designed: the User class and Status class are used to simulate the implementation of the non-ARC mechanism in two ways, there is no big difference between the two. The reason is that they are written to facilitate learning and comparison! First, it is easy to understand the code without using atuorelease and the automatic release pool, but it is generally not written as follows during development: copy the code/********************************* User. h file ************************************** // * design the Weibo user model: name, Weibo number, password, profile picture, gender, mobile phone number, birthday */# import <Foundation/Foundation. h> // enumeration and struct do not concern about memory problems typedef enum {SexMan, // SexWoman/female} Sex; typedef struct {int year; int month; int day ;} date; @ interface User: NSObject // User name @ property (nonatomic, retain) NSString * name; // Weibo account @ property (nonatomic, retain) NSString * account; // Weibo password @ property (nonatomic, retain) NSString * password; // user profile picture is set to image Address URL such as: http://weibo.com/a.png@property (nonatomic, retain) NSString * icon; // user gender @ property (nonatomic, assign) Sex; // user's mobile phone @ property (nonatomic, retain) NSString * phone; // user's birthday @ property (nonatomic, assign) Date birthday; @ end/******************************** User. m file * * ***********************************/# Import "User. h "@ implementation User-(void) dealloc {NSLog (@" the object referenced by the User object is released before it is released "); // It can also be understood that the retain type declared in the User class should be completely destroyed after the User class is destroyed [_ name release]; [_ account release]; [_ icon release]; [_ password release]; [_ phone release]; [super dealloc];} @ end/******************************* Status. h file ***********************************//* design Weibo model: weibo content, Weibo image placement, sending time, Weibo sender, and forwarding Number of Weibo posts, comments, and forwarded times */# import <Foundation/Foundation. h> # import "User. h "@ interface Status: NSObject // Weibo content @ property (nonatomic, retain) NSString * text; // Weibo image placement address @ property (nonatomic, retain) NSString * icon; // The sending time is calculated from 00:00:00 on January 1,. How many milliseconds have elapsed since now? @ property (nonatomic, assign) long time; // @ property (nonatomic) time_t time; you can also use this // Weibo sender @ property (nonatomic, retain) User * user; // forwarded Weibo According to the object-oriented idea: encapsulate your class into attributes @ property (nonatomic, retain) Status * retweetStatus; // number of comments @ property (nonatomic, assign) int commentsCount; // Number of forwarded times @ property (nonatomic, assign) int retweetsCount; @ end/******************************* Status. m file **********************************/# import "Status. h "@ implementation Status-(void) dealloc {NSLog (@" the object referenced by the Status object is released before the Status object is released "); [_ text release]; [_ user relea Se]; [_ retweetStatus release]; [_ icon release]; [super dealloc];} @ end/******************************* main. m file **********************************/# import <Foundation/Foundation. h> # import "User. h "# import" Status. h "int main () {// create two users and set the User Name User * user1 = [[User alloc] init]; user1.name = @" James "; user * user2 = [[User alloc] init]; user2.name = @ "Liz"; // create two Weibo posts and set the Weibo attributes Status * status1 = [[Status Alloc] init]; status1.user = user1; // This Weibo post is status1.text = @ "I'm so happy today! "; Status * status2 = [[Status alloc] init]; status2.user = user2; // This Weibo post is the status2.text = @ issued by user2." The non-ARC is basically mastered! "; Status2.retweetStatus = status1; // after posting a microblog, The user2 user forwards the microblog [status2 release]; [status1 release]; [user2 release]; [user1 release]; return 0;} copy the second type of code: Use atuorelease and Auto Release pool, copy the code/********************************** User. h file ************************************** // * design the Weibo user model: name, Weibo number, password, profile picture, gender, mobile phone number, birthday */# import <Foundation/Foundation. h> // enumeration and struct do not care about memory problems typedef enum {SexMan, // male SexWoman // female} Se X; typedef struct {int year; int month; int day;} Date; @ interface User: NSObject // User name @ property (nonatomic, retain) NSString * name; // Weibo account @ property (nonatomic, retain) NSString * account; // Weibo password @ property (nonatomic, retain) NSString * password; // user profile picture is set to image Address URL such as: http://weibo.com/a.png@property (nonatomic, retain) NSString * icon; // user gender @ property (nonatomic, assign) Sex; // user's mobile phone @ property (Nonatomic, retain) NSString * phone; // user's birthday @ property (nonatomic, assign) Date birthday; + (instancetype) user; @ end/******************************** User. m file ************************************** /# import "User. h "@ implementation User + (instancetype) user {return [[self alloc] init] autorelease];}-(void) dealloc {NSLog (@ "the object referenced by the User object is released before the User object is released"); [_ name release]; [_ account release]; [_ icon rel Parameters]; [_ password release]; [_ phone release]; [super dealloc];} @ end/******************************** User. m file ************************************** /# import "User. h "@ implementation User + (instancetype) user {return [[self alloc] init] autorelease];}-(void) dealloc {NSLog (@ "the object referenced by the User object is released before the User object is released"); [_ name release]; [_ account release]; [_ icon release]; [_ password release]; [_ phone release]; [Super dealloc];} @ end/****************************** User. m file ************************************** /# import "User. h "@ implementation User + (instancetype) user {return [[self alloc] init] autorelease];}-(void) dealloc {NSLog (@ "the object referenced by the User object is released before the User object is released"); [_ name release]; [_ account release]; [_ icon release]; [_ password release]; [_ phone release]; [super dealloc];} @ end /************************* * ***** Main. m file **********************************/# import <Foundation/Foundation. h> # import "User. h "# import" Status. h "int main () {@ autoreleasepool {// create two new users * user1 = [User user]; user1.name = @" James "; // The NSString has been encapsulated internally. You don't need to worry about the memory. You just need to release the User * user2 = [User user User] at the time of reference. user2.name = @ "LITH "; // create two new Weibo posts: Status * status1 = [Status status]; status1.text = @ "today! "; Status1.user = user1; Status * status2 = [Status status]; status2.text = @" basic non-ARC knowledge! "; Status2.retweetStatus = status1; status2.user = user2;} return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.