Parent class creating Child Class Instance


The Problem

The problem started when I was looking at NSURLSession’s dataTaskWithRequest:theRequest method.

dataTaskWithRequest returns an instance of NSURLSessionDataTask, which is a child class of NSURLSession.

Usually, in objective c, a class has an address. It instantiates another object (in the heap), which has a different address.

But how does a parent class return an object of a child class? The child’s parent class is the calling parent? or a different parent?

Basically, it works like this.


Create a parent class. Then create a child class. If you were to instantiate the child class, the whole hierarchy sits on one address. Print out the address of self in both the init methods for Parent/Child. You will see that they are the same.

Parent .h/.m

Child .h/.m

What this means is that if you were to instantiate a child with an address, the parent (grand parent, grand grand parent…etc) class all sits on top of the child class. They are all ONE object, that takes up ONE address.

However, say in your parent class, you instantiate a child class. How would this work?


The parent (with its own address of 0x1001072f0) instantiates the child class (with its own address 0x100400080) in the heap.

That itself should tell you that we are dealing with 2 separate objects here. If the child accesses its parent properties, it would be doing so in its OWN parent. (The parent that sit on top of the child) The parent that shares the same address with the child.

IF you want the child to access the calling parent, simply pass in the calling parent as a parameter.

So what’s up in NSURLSession’s dataTaskWithRequest

Hence, this is what’s happening when I see NSURLSession call a method, and returns a child class object.

inside dataTaskWithRequest, its creating a child class NSURLDataSession on the heap, which its own NSURLSession parent. They both sit on another address. This other object will process the request, then return execution to the completion block.

mutex vs semaphore


Is a key to a toilet. One person can have the key – occupy the toilet – at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: “Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.” Ref: Symbian Developer Library

(A mutex is really a semaphore with value 1.)


Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count – the count of keys – is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: “A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).” Ref: Symbian Developer Library

HTTP request using iOS

Example 1 – pass data using Header fields

On the server side, the receiving script will strip the information from the request object and use the data, query the database, and return it.

On our iOS client side, we create a NSURL object that takes in the string of the web api address that we want to hit.

For example in our case it would be email)

NSString –> NSURL –> NSMutableURLRequest

  • So first, we assemble a string to satisfy the HTTP url.
  • Then throw it in a NSURL.
  • Create a NSMutableURLRequest object, and init it with our NSURL.
  • Manipulate the request object with http header field/value combinations.
  • set the HTTP VERB

NSMutableURLRequest object (iOS)


The key/value you assign in your NSMutableURLRequest object will be read on the server side (node) on the request object’s header’s properties.

Reading values for HTTP Header fields (node)

After you have set up your URL Request object, you insert it into a NSURLConnection, and make it run.

NSURLConnection object (iOS)

Full Source

Example 2 – authenticate a user

Custom UIPickerView example


ref –



We are going to create a custom UIPickerView. This Picker View get the string of your selection, then send the delegating class the result. Hence a UIViewController can create this custom class, conform to the protocol, connect the delegate, and start receiving strings of the selected entries in the picker view.

Create class and override

Implement the protocol, create the delegate

For our case, we are going to create a single delegate which forces the UIViewController to implement our single delegate’s methods.

All default UIPickerViewDelegate and UIPickerViewDataSource will delegate to our custom RTPickerView.

If you want to group your custom delegate methods along with UIPickerViewDelegate, UIPickerViewDataSource, under RTPickerDelegate, you can declare it like so:

In your ViewController, when you conform to RTPickerDelegate
and you go self.pickerView.delegate = self,

Then you will be receiving all messages from all delegates.

However, for the sake of demonstration, we are going to create a custom protocol. When the ViewController uses it, it can receive 2 delegates. One is ‘delegate’ from UIPickerViewDataSource and UIPickerViewDelegate. One is RTPicker_Delegate, which is for taking care of our custom protocol methods.

That way, you can pick and choose which protocol you will want to conform to.

The RTPicker_Delegate delegate that we want our calling UIViewController to access should be weak. It should not be strong because as an object, we don’t want the controlling UIViewController to keep us in memory.


Our Custom object conforms to View Delegates and Data Source delegates. Thus, we
take care of these protocol in this class. The calling ViewController is welcome to access the delegate property, and then implement these protocol methods too if it wants.

UIPickerViewDataSource methods

UIPickerViewDelegate protocol methods

RTPickerDelegate protocol method

When we are processing didSelectData, we don’t want the calling UIViewController to suddenly disappear on us. Thus, we strong ViewController temporarily. The local stack has a strong pointer on it, it makes the delegated UIViewController process messages, and this method will finish. Our strongDelegate is declared on the stack, and thus, will pop. Free-ing this class from the UIViewController

How to Use

Conform to the protocol

Create the object, make sure you add it to the UIViewController, and then connect the delegate. After connecting the delegate, all messages from that particular action which triggers the protocol method, will be sent to the ViewController for answers. You, as the developer of the UIViewController will need to implement it.

Implementation of the protocol method

Mac: Install MySQL and use Terminal to use it

ref –
See whether you have the 32 or 64 bit processor

Go download the mysql dmg file from mysql’s developer page:


Locate the DMG file compatible for your machine and download it


Let’s double click on the .dmg file, and double click the .pkg file to install it. Leave all the default options and continue through the installation wizard.


At a certain point, you’ll be given a default password. copy and paste it into a note editor because you’ll need it for the first log in, later in time.

Once you logged in the first time, you will be prompted to create a new password.


Go to your preferences, and you’ll see the SQL icon. Click on it and start the SQL server



Working with SQL in terminal

Open up the terminal.
Then go to the location:

cd /usr/local/mysql/bin

You’ll see all your mysql run commands in there.

/usr/local/mysql/bin/mysql -u root -p

enter your password

Showing all Database(s)

Let’s see all the databases in the SQL server.

mysql> show databases;

| Database |
| information_schema |
| mysql |
| performance_schema |
| sys |
4 rows in set (0.00 sec)


Switch to Database

There are 4 databases so far. Let’s choose one and see what kind of tables are in there.

mysql> use mysql
Database changed
mysql> show tables;

Create a Database

But, we want to create our own custom database. Let’s do so.

Query OK, 1 row affected (0.03 sec)

Then create a table inside of our database

mysql> use MyLocalDB
Database changed

mysql> CREATE TABLE Persons
-> (
-> PersonID int,
-> LastName varchar(255),
-> FirstName varchar(255),
-> Address varchar(255),
-> City varchar(255)
-> );
Query OK, 0 rows affected (0.35 sec)

Insert a row into your table

mysql> insert into Persons (PersonID, LastName, FirstName, Address, City) values (‘123’, ‘Saor’, ‘Rick’, ‘132 Nowhere street’, ‘Liberty Hall’);
Query OK, 1 row affected (0.07 sec)