I am learning Objective-C. This post contains some Objective-C trivia which I picked up over the last few weeks.

Concatenating Strings

In Objective-C, NSString class represents strings. Concatenating two strings is not easy. To ease the pain, there is a NSMutableString class. It has an append method. The StackOverflow post summarises the different methods available to concatenate strings. Usually, we have problems with XML strings which run into multiple lines. The example below shows an easy way to concatenate strings broken into multiple lines.

Attributed Strings

NSAttributedString and NSMutableAttributedString class is used to set text attributes to the string. For example, consider displaying the text: Seats\n%d. The word Seats should appear as bold. Objective-C code to do that is shown below.

ARC and property attributes

ARC stands for Automatic reference counting. With ARC, the compiler takes care of reference counts of an object. When the reference count of an object reaches zero, the object is no longer in use. And can be deallocated from memory. To retain an object reference, we define the property with a strong reference.

The above code ensures that the allocated button is retained. In some cases, there is a parent-child relation between objects. For example, when objects are added to array or when subviews are added to view. In such cases, a weak reference is sufficient. In the code below, the addSubview method adds a reference count to keep the object in memory.

Sometimes, the parent object is NOT strongly referenced. In that case, the copy attribute ensures that the object is not released.

Atomic and Non-atomic property attribute deserves attention. Atomic property attribute is the default. If a property is atomic, only one thread can access the object. Atomic properties are slow. Non-atomic property attribute improves performance. Because now, multiple threads can access the object. Objective-C has a lot of optimisation techniques for the advanced developer.

Composing Dates

Composing dates involves using NSDate, NSDateComponent and NSCalendar. The typical way to format dates in Objective-C is shown below.

To compose a date, use the NSDateComponent class.

As you can see, composing a date object is not simple enough. So, wrap the code in an utility class with static methods. Below code show how to get the day, month and year components from a NSDate object.

And here is the usage of DateHelper class.


The NSTimer class creates a timer with the scheduledTimerWithTimeInterval static method.

Stop the timer using the invalidate method.

An important point to note. The thread that creates the timer and invalidates the timer should be the same. Preferably the UI thread. If a different thread calls the invalidate method, then the timer does not stop.

Objective-C – Strings, Property attributes and Dates
Tagged on: