Saturday, December 31, 2011

MVC Pattern

Separate Your Concerns II
This report is aimed at discussing a table view application that sources data from the Internet. The application should have the following features :
  • The application should separate data model, control logic, and display code following the MVC pattern. It is acceptable to control views from your controllers (e.g. set the background color of the view); it is not acceptable to implement how the views are drawn (e.g. implementing drawing commands that actually color the background).
  • The user should be able to refresh, which makes a call to the Internet, updates the application’s data, and then updates the table view.
  • Application must be able to work offline.
Good object-oriented design will help code be resilient to changes, new features, and portability. Apple uses the Model-View-Controller design pattern as part of the UIKit framework on which most iOS apps are built. In the following, how Model-View-Controller design pattern is used to implement class structure and communication between classes is explained. In addition, how this design will resiliently handle possible changes is also explained.

The application
The following screenshots illustrates an application which gets the current foreign exchange rates for approximately 66 currencies from Time Genie website (URL is provided in references section). The base rate is the Euro and so the Euro's value will always be 1. The application saves the data in a comma-separated format file and presents it in a UITableView to users. The file containing the data can be synchronized with its source (Time Genie website) using the Refresh Data button provided (Data is updated on daily basis by the Time Genie website).

A link to the source code of the application is provided at the end of the post.

Image 1: Application in 3 states (from left to right, before refreshing, refreshing and after refreshing)

Note that in the image above, before refreshing the data, the first 7 items in the file was manipulated manually to show the effect of the refresh button.

MVC pattern in exchange rates viewer application
The exchange rates viewer application presents currency rates for approximately 66 countries, based on Euro, in a table view format. The data is sourced from a website in comma-separated text format. The application saves a copy of the file the first time it connects to the source website; thereafter, the data is loaded from the file. This functionality and all other relevant operations are handled in the model layer of the application, which is a class called DataProcurer and is a subclass of NSObject.

Model layer checks the existence of the data file in its init method using an NSFileManager. In case the file does not exist, information is read from the source website and is saved to a file in application directory so that it can be used by the application for the next times. It provides the model with the ability to produce data even if there is no connection to the internet. Model layer has an instance variable of type NSMutableArray called currencyRates using which information is communicated to other layers. Instance variable currencyRates can be accessed from outside of the model layer via its accessor methods (accessor methods are generated using @synthesize notation).

There are other methods in model layer that are used to manipulate data. Method refreshData simply deletes the data file using a NSFileManager and tries to repopulate the instance variable currencyRates using the same method that is called in init method. Therefore, because the file does not exist anymore, the updated version will be provided from the source website. The source code for important methods of the DataProcurer class is provided in the following.

-(id)init
{
    self = [super init];
    if (self != nil)
    {  
        currencyRates = [[NSMutableArray alloc] init];
        currencyRates = [self giveMeData];
    }

    return self;
}

-(void) refreshData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                         NSDocumentDirectory,
                                         NSUserDomainMask, YES);
    NSString *docDirPath = [paths objectAtIndex:0];
    NSString *filePath = [docDirPath
                  stringByAppendingPathComponent:@"mydata.txt"];
      
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath])
    {
        [fileManager removeItemAtPath:filePath error:nil];      
    }   
    currencyRates = [self giveMeData];
}

- (NSMutableArray*)giveMeData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                       NSDocumentDirectory,
                                       NSUserDomainMask, YES);
    NSString *docDirPath = [paths objectAtIndex:0];
    NSString *filePath = [docDirPath
                     stringByAppendingPathComponent:@"mydata.txt"];
    NSURL* url;
    NSString* content;   
    NSFileManager* fileManager = [NSFileManager defaultManager];
   
    if ([fileManager fileExistsAtPath:filePath])
    {
        content = [[NSString alloc]
                   initWithContentsOfFile:filePath
                   encoding:NSUTF8StringEncoding error:nil];      
    }
    else
    {
        url = [[NSURL alloc]
              initWithString:@"http://rss.timegenie.com/forex.txt"];
        content = [[NSString alloc]
                   initWithContentsOfURL:url
                   encoding:NSUTF8StringEncoding error:nil];
        [url release];
        [content writeToFile:filePath atomically:YES
                    encoding:NSUTF8StringEncoding error:nil];
    }
   
    NSMutableArray* tmpMutableArray = [[NSMutableArray alloc]
                               initWithArray:[content
                               componentsSeparatedByString:@"\n"]];
    [content release];
   
    NSMutableString* currency = [[NSMutableString alloc] init];
    NSMutableString* rate = [[NSMutableString alloc] init];
    NSMutableArray* mutableArray = [[NSMutableArray alloc] init];
    for (int i=0; i < [tmpMutableArray count]; i++)
    {
        NSMutableString* tmp = [[NSMutableString alloc] init];
        [tmp setString:[tmpMutableArray objectAtIndex:i] ];
       
        int l = [tmp length];
        if (l > 0)
        {
            [currency setString: [[tmp
                                  componentsSeparatedByString:@"|"]
                                 objectAtIndex:0] ];
            [rate setString: [[tmp
                               componentsSeparatedByString:@"|"]
                             objectAtIndex:2] ];
       
            [tmp setString:@"Every 1 EURO is "];
            [tmp appendString:rate];
            [tmp appendString:@" "];
            [tmp appendString:currency];
       
            [mutableArray addObject:tmp];
        }
       
        [tmp release];
    }
   
    [tmpMutableArray release];
    [currency release];
    [rate release];
   
    NSMutableArray* r = [[NSMutableArray alloc]
                         initWithArray:mutableArray];
    [mutableArray release];
   
    return r;
}

The controller layer, Assignment6ViewController class which is a subclass of UIViewController, mediates between model and view layers. To do so, it has an instance variable of type DataProcurer, the model layer, and an IBOutlet of type UITableView that is associated with the table view in the view layer. In the viewDidLoad method of the controller class, the instance variable of type DataProcurer is set to act as the datasource of the table view. It should be mentioned that DataProcurer class adopts the UITableViewDataSource protocol.

Touching Refresh Data button triggers an IBAction method in the controller class. In this method all that has to be done is to send a refreshData message to the model layer so that information can be sourced from the internet again. Consequently, the table data will need to be reloaded which is done after calling refreshData in the IBAction method. The source code for important parts of the Assignment6ViewController class is provided in the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataProcurer = [[DataProcurer alloc] init];
    tableView.dataSource = dataProcurer;
}

-(IBAction) btnRefreshDataPressed:(id)sender
{
    [self.view addSubview:modalView];
   
    [self performSelectorInBackground:@selector(background)
                                           withObject:nil];
}

-(void)background
{
    NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
    [dataProcurer refreshData];  
    [tableView reloadData];
   
    [self performSelectorOnMainThread:@selector(backgroundDone)
                                     withObject:nil
                                      waitUntilDone:NO];
    [p release];
}

-(void)backgroundDone
{
    [self.modalView removeFromSuperview];
}
@end

The view layer, generated by Interface Builder, uses a table view to show the data. As discussed in previous sections, the view layer gets its required data from the model layer via an intermediary object that is the view controller. Using Interface Builder, no code is written and everything is exclusively focused on the View of the application (Cocoa 2011).

There are some points at which exchange rates viewer application could be improved. The application is currently storing its data in a flat file in a comma-separated format. It would be better if information was stored in NSManagedObjects using core data. In that case, firstly, information could be sourced incrementally if needed. Whereas now, the data file is deleted and regenerated every time an update is necessary. Secondly, the data model could be expanded to keep the date of the information so that it could be checked whether or not the data needs to be updated when the application runs. This check could happen automatically in model layer or instead, model layer could provide a method for controllers so that they can ask the model to do so.

Switching the model layer from using flat files to core data will be simple in this application. All the changes will be limited to the model layer and the way in which the data is stored. Communication of information between model and view controller will remain unchanged. The view controller does not know and does not need to know how information is stored. All it needs is an array of strings to use as the data source of the table view IBOutlet that it is keeping; no matter how the array is generated or where it is sourced from.

Diagram




References

iOS App Life Cycle

iOS applications don’t always live in the foreground. Applications can be foreground, background, suspended, and active or inactive, depending on the circumstances. UIApplication object uses different methods in its delegate to provide information about when important changes in application’s state occur or is about to occur. Implementing delegate methods helps the application avoid crashing and gives it the ability to continue where it stopped.

Imagine that you are asked to develop a first-person shooter 3D game for iOS in OpenGL (an open standard for 3D graphics written in C). The game has levels, multiplayer capabilities, and a global leaderboard (connected through the Internet). The aim here is to create a diagram that explains what work your game will do when transitioning between each of the application’s states. The diagram must cover at least the following possibilities:
  • User receives a text message in the middle of a single player game (iOS4).
  • User receives a phone call in the middle of a multiplayer game and decides to answer the call.
  • User closes the app with the home button in the middle of a single player game.
  • User presses the lock button while on the Main Menu of the application, outside of a game.
Attention should be paid to iOS requirements (e.g. iOS apps that use OpenGL in the background will be terminated by iOS!).

In the first-person shooter 3D game for iOS example, when playing a single player, after an interruption like receiving an SMS or user pressing home button, user data including which level, points and generally current circumstances should be saved. If Internet connection is in use for global leaderboard, it should be cancelled otherwise the connection object attempts to call its delegate, which does not exist anymore, when it is finished. For the same reason, any timers in use should be invalidated and finally OpenGL frames must stop working as the program is about to enter background as any OpenGL operation will cause the application to be closed by iOS.

Similar scenario is true when a multiplayer game is in progress and an interruption such as a phone call happens. Except that in a multiplayer game there is less or no user data to be saved and of course internet connection may not be an issue. However, the connectivity among the device and other pairs should be cancelled.

When a game is not in progress and the application is in the menu and an interruption occurs, there is still OpenGL operation which needs to be stopped. Furthermore, the application may already have an internet connection in use, which should be terminated, for instance, in case user have checked the global leaderboard.

The application should be able to recover its last state if necessary. This can happen when for instance user ends a phone call or decides not to open an SMS. In that case, user wants to be able to continue the game where it was paused. This reversal operation could take place in delegate methods that are indicating the application is about to go back to the foreground.

In the end, it is a plus for an application to handle these situations properly. However, some of the processes discussed above, both in moving to background and recovering from a halt, take a lot of time to be completed which can be frustrating for users if they happen many times. For example, 3D graphical objects consume a lot of resources to be created and eliminated. Moreover, not all the conditions are recoverable. For example when playing a multiplayer game, previous state is not achievable and user must re-join the game and start from the beginning.

Diagram


Tuesday, December 27, 2011

Delegates, Data Sources and Table Views

Separate Your Concerns
Multiple Data Sources!
A UITableView object requires two other objects as its data source and delegate to perform. Data source object, which should conform to UITableViewDataSource protocol, provides data for populating and information about data structure for table view objects. For this matter, there are two methods in UITableViewDataSource protocol, cellForRowAtIndexPath and numberOfRowsInSection, that data source objects are required to implement. Hence, it is compulsory for every table-view object to have a data source object. However a table-view object might not have a delegate as all the methods in UITableViewDataDelegate protocol are optional (Table View Programming Guide for iOS 2011).

In addition to data and its structure, configuration of table view objects is also determined by data sources. For example, whether or not table rows can be edited or deleted and if table rows can be reordered (UITableViewDataSource Protocol Reference 2010). A table-view object may need to act or be presented differently under certain circumstances. The argument here is having multiple data source objects which table-view object can switch to in different situations instead of having multiple table-view objects, or one table-view instance having a single data source with complicated controlling structures for various situations.

One situation when a single table view object with multiple data sources can come in handy is showing different data in similar formats. Imagine a twitter app in which tweets are categorized into 3 groups: re-tweets, mentions and home. Every type of tweet can be loaded into one data source and the table view switches among data sources when user wants to. It certainly does not make much sense implementing 3 table views. The reason for this is the fact that, every change, major or minor, in the way tweets are shown, should be applied on 3 table view objects. A multiple data sources design obviously needs less effort in this case. The diagram below shows how this design works in MVC.


Depending on the number of rows, a table-view object might have to present its data in different number of sections. In an application where dataset is to be provided from dynamic external sources like the web, the number of to be populated rows is not predefined. Therefore, the application should be ready to structure its table-view, during the runtime of the application, in ways suitable for the dataset it is encountering. It is simply achievable by having multiple data source objects prepared for different purposes, and defining the appropriate table-view data source as current data source when necessary. This design is also helpful in applications where users have the facility to change the appearance of their application, here particularly table-view objects, regardless of the amount of the data and the source from which the data is provided. In addition, situations when for instance the device rotates can be handled easily and effectively using the same approach. It can be a useful feature that an application represents its data in a table-view object differently depending on the device orientation.

Apart from the appearance of the data, the way in which table-view objects’ data should be treated in different situations is manageable via data sources. Following the design discussed above, there can be multiple data sources to allow users to edit, delete or to move rows of data in certain times; or prevent users from manipulating the data in some other times. This can be for reasons such as the origin of data or the stage at which the application is. For example, an application might provide its users with two states; one with the facility to change the order of the rows of a table-view object and the other where the content of the table-view object is frozen and cannot be changed.

Having a single UITableView object with multiple data sources in the scenarios discussed above, will lead to a smaller application that requires less memory in comparison to a design in which there are more than one UITableView objects, each of which being controlled by a separate data source. Moreover, it leads to having several objects as data sources, each of which simply responsible in one occasion. This is a lot clearer and easier to maintain than when all situations are handled in one data source that results in complicated controlling structure and an unreadable code.

References

Objective-C Memory management

Introduction
When creating applications for Mac OS X, it is possible to have garbage collection enabled which means you do not need to worry about freeing memory allocated to objects. However, in iOS, due to lack of resources including CPU and memory, you cannot take advantage of a garbage collection mechanism; and  therefore, allocating and releasing objects’ memory should be handled manually.
Memory management in Objective-C is based on object ownership, where an object can have several owners; and it will exist until it loses all of its owners. If an object dose not have any owners anymore, it will be destroyed automatically by the runtime system (Memory Management Programming Guide 2011). This model is referred to as reference-counting in which all that needs to be taken care of is: keeping track of references; and when there are no references to an object, it is the runtime system responsibility to free the memory allocated to the object.
The reference-counting model works based on the following rules:
  • You are the owner of the object you create. An object is created by alloc, new, copy and mutableCopy methods. When an object is created for the first time, the reference-count for it is 1.
  • You can take ownership of an object by sending it a “retain” message. A retain message increases the reference-count by 1. You take ownership of an object to make sure you do not lose it before you are finished with it.
  • You have to release an object after you are finished with it. This is done by sending the object a release or autorelease message. Every time an object receives a release message, its reference-count decreases by 1. Figure one shows how reference-counting works.
  • You should not attempt to release an object unless you have already created or retained it.
Reference counting
Figure 1: reference-counting and object validity in Objective-C (Stevenson 2008)
Retain
One occasion that an object is sent a retain message is when the object’s value is to be stored in a property of a class. This normally happens in “setter” and “initialization” methods to prevent the property from losing its value after the referring object (the input parameter of the setter or init method) is released by its owners. Another situation to use retain is wherever an existing object’s value is going to be assigned to a secondary object; and that secondary object should exist longer than the primary object. For instance, in situations when the primary object or its parent, is going to be destroyed in other parts of the program. Finally, when assigning an object returned by a method to another object, the returning object is retained (if necessary) because its reference count may become zero after the method finished executing. 

Release
Objects should be released as soon as they are no longer needed. To do so, a release or autorelease message is sent to the object. The difference of the two messages lies in the fact that the ownership of an object, sent a release message, is relinquished immediately and in case of having no more owners, the object is destroyed and the allocated memory is freed. Whereas in autorelease, it is guaranteed that the object will exist at least until the end of the current block. Therefore, autorelease seems to be safer than release. Nevertheless, in applications that have too many objects loaded into memory, defining all the objects as autorelease can cause memory inefficiencies. There are two specific occasions that autorelease is preferred to release. First, in setters, the property is sent and autorelease before it gets the new value provided by the input parameter. Reasoning behind this is the possibility that both the property and the input parameter could point to the same location (Stevenson 2008).
- (void) setProperty: (NSObject*) inputParameter
{
    [property autorelease];
    property = [inputParameter retain];
}
The second occasion to use autorelease is when the object is supposed to be returned by the current method (Memory Management Programming Guide 2011).
- (NSString*) methodName 
{ 
    NSString* s =  [[[NSString alloc] initWithString:@”Some text”] autorelease]; 
    /*
    Some operation
    */
    return s;
}
One important point to consider is that only objects created by alloc, new, copy, or mutableCopy should be released manually. For example, an object defined as follows will not need to be sent a release or autorelease message.
NSString* s = [NSString stringWithString:@”Some text”];
In fact the definition of object “s” above is similar to the following definition:
NSString* s = [[[NSString alloc] initWithString:@”Some text”] autorelease];

Conclusion
On the whole, by following some certain rules, practical memory management in Objective-C can be none or less challenging.
  1. Using accessors, that are written based on what is discussed, to get or mutate instance variables, as having retain and release on them throughout the code makes it harder to keep their reference-count track.
  2. @property notation defines both of the accessors in a standard and safe way. All needed, is to synthesize the methods by @synthesize notation on properties.
  3. All of the instance variables should be sent their final release message in dealloc method of the class.
Complying with the rules mentioned above, memory allocated to instance variables is being managed properly. The only matter to take into account is local variables which should be explicitly released, by either release or autorelease message, whichever is appropriate based on the context, if and only if they are created using alloc method.

References

Saturday, December 24, 2011

Objective-C id Data Type

Class Warfare
The point of this task is getting to know the Objective-C anonymous data type, id, and to learn how to control it. This task is a view-based application which initially has a single button called “Random View”. When the button is pressed, a random anonymous object is created and based on the type of the object, the application operates as follows:
  • If the object is a UIView class or subclass thereof, it is added as a subview to the main view.
  • If the object has a background colour property, it is given a random colour before being added to the main view.
Image 1, illustrates screenshots of the application in different states. Note that objects which are recognized as UILabel or UIButton, have their class names as either their text or title properties respectively.

Figure 1: Random Object Application


The point here is taking advantage of , isKindOfClass or isMemberOfClass to determine the dynamic type of the object, and respondsToSelector to see if it has a background colour property or not. The code snippet below shows the IBAction method controlling the “Random View” button.


A paragraph about User Experience in iOS


Usability and user experience of an application, irrespective of its nature or the platform on which the application runs, should be taken into account at very early stages of designing process. When it comes to mobiles, this issue becomes even more important due to the limitations of the device which the application is being designed for. One of the most important aspects in this matter is the structure of the application and the fact that every type of UI controller should be used as it is expected and defined by iOS UX paradigm. Second issue to consider is providing proper and enough feedback for users. iOS has got a wide range of controls for this matter. The expectations and standards of using these controls are fully discussed in “iOS Human Interface Guidelines”. Finally, it is expected from a well-designed application to be able to operate gracefully when limited conditions are applied and to be able to recover itself from interruptions, which are very likely to occur. 

A paragraph about iOS Application Life Cycle


In iOS,specifically in developing for iPhone, it is very important for an application to have proper handler methods for different states that the application may go to. This is because of the fact that in iPhone, the likelihood of your application going to background is reasonably high. This can be due to a phone call, SMS, alarms or even user pressing the home button. There are few matters that should be particularly considered when thinking about your application life cycle. For instance:
  • Applications that use OpenGL in the background will be terminated by iOS.
  • Although networking operations can easily continue to operate in background, special considerations similar to those of concurrency are required.
  • Timers and other periodical tasks should stop functioning.
  • No new task should be initiated.
To handle situations mentioned above, the application object informs its delegate of changes in application state. To provide more flexibility, it also sends notifications about each change to let other interested parts of the program know about the state transition so that they can take action independently if necessary. Basically the changes in application state are application did finish launching, application did become active, application will resign active, application did enter background, application will enter foreground and application will terminate.

A paragraph about Networking and Multitasking in Objective-C


There are a number of ways in iOS in which networking can be handled. Perhaps the simplest method would be using initialization with contents of URL in classes such as NSString, arrays and dictionaries. Moreover, it provides classes including NSURLConnection which basically allows network operations to be handled in the background. There are also other libraries such as ASIHTTPRequest (not an Apple library) that provide an acceptable range of functionalities to implement networking. ASIHTTPRequest also allows the user to choose if an operation should run in background or on the main thread.  Therefore, when developing network based applications, user rarely needs to explicitly indicate that an operation should be performed in background. Nevertheless, as part of support for multitasking in Mac OS X and iOS, Objective C offers using NSObject “performSelector:onThread” and “performSelectorInBackground” methods as well as operation and queue classes. NSThread class can also be taken advantage of. However, it is not encouraged by Apple especially for those who are about to implement a new application and who are not quite familiar with threading design techniques (instead, easier-to-use and more sophisticated operation and queue classes are encouraged).

A paragraph about Core Data in Objective C


Model layer of MVC design pattern in iOS is often implemented with Core Data. It is neither a database nor an ORM. It is an object graph that serves as the entire model layer plus the API which allows object serialization and retrieval. In Cora Data, objects are persisted in text files, XML and SQLite databases. When reading objects from storage, it allows the flexibility to filter or sort information as necessary. In this case, Core Data is very similar to sophisticated databases. On the other hand, when it comes to relationship between objects in the object graph, it is a totally different concept from relations in databases. It might be helpful to look at it as relationship between classes in a UML class diagram. It can be very challenging if not fully understood. Using objects’ relationships in a non-standard way might very easily lead to inconsistent states in object graph (More information can be found in Apple Developer Library “Core Data Programming Guide” under Relationships and Fetched Properties/ Unidirectional Relationships).  

A paragraph about Unit Testing in Objective-C


Unit testing is fairly easy as iPhone Unit Testing framework, which is based on OCUnit, is fully integrated with Xcode and can be used on iPhone Simulator. Apart from being fully integrated with development environment, it is also important to point out that OCUnit covers almost all the aspects that seem to be necessary for unit testing. For example, it can be tested if the output of a method is as expected, if a method throws a specific exception when it should, or if a method ends without being interrupted by exceptions. All of these are implementable in the same way which is appreciable in comparison with for instance JUnit that different approaches are needed to test different conditions. In regards to unit testing in iOS, there are a few other frameworks, some of which seem to be very powerful and interesting. For example, GHUnit which allows testing of UI components and CATCH (C++ Adaptive Test Cases in Headers) as a modern unit test framework, are main priorities.

A paragraph about Observer pattern in Objective-C


Another way of making objects talk to each other in Objective C is using Observer pattern. In this pattern, an object can send its customized notification to the notification centre. Then it is the notification centre that notifies the objects who has registered for that particular notification. This can be for many reasons. For instance, an object working with model layer might think that it would be of other objects’ interest to know that it has created a new NSManagedObject in the model layer. This can be very useful when for example a table view, showing that data, needs to know when to reload itself. From my point of view, one of the most intriguing aspects of observer pattern is the fact that there is no relationship needed between the sender and the receiver of a notification. In addition, a single notification can be observed by many objects at the same time.

Friday, December 23, 2011

A paragraph about Delegation pattern in Objective-C


In most commonly used programming languages, delegation pattern operates based on conformation or even similarity of objects. In this case it is very similar to delegation pattern in Objective C in which it heavily relies on protocols. However, this pattern can never be as effective in other languages as it is in Objective C. The reasons behind this is that Objective C supports categories which allow methods to be added to existing classes without having to subclass them or having to have access to the source code. In addition, Objective C provides @optional notation when creating protocols. Therefore, not all the methods in protocols have to be implemented. This feature, plus the ability to check if an object (in this case the delegate) responds to a specific message (using respondsToSelector), make delegation pattern in Objective C very convenient and useful. In other languages, for instance in Java, all the methods in an Interface should be implemented. As a workaround for this, many Java interfaces come with an adapter class, which is a common approach for not requiring the user to implement a lot of empty methods. However, it limits inheritance and makes the design inflexible. Using delegation pattern in Objective C, the relationship between an object and other objects declared in it, can be bidirectional easily.  

Tuesday, December 20, 2011

A paragraph about Memory Management in iOS

One topic to mention as special in Objective C is its memory management. It becomes more important in developing for iOS than OS X, as there is no garbage collection process due to limitation of resources. What is special here about memory management is reference counting. This concept seems to be problematic at first, but once you realize how it works, memory concerns become even easier to deal with in comparison with languages in which memory issues are handled automatically. For instance, in Java, developer must think of objects’ life cycles. In programs that use a great number of complex objects, it becomes challenging to keep track of all references to an object. Whereas in Objective C, that you should explicitly take and release ownership of an object, all that needs to be taken care of is releasing the ownership of an object, that you own, and that you do not need anymore. This way, memory allocated to an object is handled within the scope of that object. Therefore, new owners from outside of that scope (for instance, when an object returned by a method is being retained) do not need to pay any attention to what is happening in that scope.

A paragraph about Two-Stage Object Creation pattern in Objective-C

Two-Stage Object Creation pattern separates initializers from the way memory is allocated to objects. This pattern provides the developer with the flexibility to initialize objects based on how they are going to be used. Therefore, different behaviors in classes can be taken advantage of with no extra amount of code. This is specifically useful when using temporary objects, which in this way, can be instantiated and immediately used without any further setup. Finally, when creating a subclass, it will not be necessary to override so many initializer methods.