Blog Archives

Introduction to Protocols

What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts.

Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.

A good example of a protocol is NSCoding, which has two required methods that a class must implement. This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.

@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; - (id)initWithCoder:(NSCoder *)aDecoder; @end

To adopt a protocol, enclose the name of the protocol in <> as below:

// Interface @interface SomeClass : NSObject <NSCoding>  {   ... }   // Implementation @implementation SomeClass   -(void)encodeWithCoder:(NSCoder *)aCoder {   ... }   -(id)initWithCoder:(NSCoder *)aDecoder {   ... }

Defining a Protocol

You can create both required an optional methods within a protocol. What follows is a definion of a protocol named ‘Fubar’:

@protocol Fubar - (BOOL)send:(id)data; - (id)receive; @optional - (int)status; @end

To use the protocol, as with the example above, specify the protocol in the interface and write the required methods in the class implementation:

// Interface @interface AnotherClass : NSObject <Fubar>  {   ... }   // Implementation @implementation AnotherClass   - (BOOL)send:(id)data {   ... }   - (id)receive {   ... }   // Optional methods - (int)status {   ... }   @end

If you are from a Java background, protocols should look familiar as they are analogous to an interface.

Using Application Badges

Several native applications on the iPhone use application badges as an indicator of new messages, think email and SMS. Creating badges is quite straightforward and is nothing more than a method call, passing in the desired number to display.

The image below shows how a badge may look when applied to your application. The code to […]

Single, Double and Triple Taps

If you need to tinker with the threshold (time between clicks) when working with single, double and triple taps on the touch screen, one approach for this follows. What follows is a short example that demonstrates how you can manage the delay between taps.

-(void)oneTap
{
NSLog(@”Single tap”);
}

-(void)twoTaps
{
NSLog(@”Double tap”);
}

-(void)threeTaps
{
NSLog(@”Triple tap”);
}

– (void)touchesBegan:(NSSet *)touches […]

NSNumber and NSInteger

If you’ve ever found yourself scratching your head thinking “now which one should I be using, NSNumber or NSInteger?” the short summary below should help.
NSInteger is nothing more than a synonym for an integer. What follows is how NSInteger is defined:

#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
[…]

Read and Write User Preferences

Reading and writing user preferences within iPhone applications is surprisingly easy given the NSUserDefaults class does most all the work for you.
What follows is a short example to show how you can read/write two values, a boolean and an integer. The example assumes you want to save state as to whether a user wants […]

Date Formatter Examples – Take 3

While working on an iPhone application recently, I needed to convert a date read from an XML stream that was in the following format: 20081122 to a nicely formatted string for display on the device: Saturday November 22, 2008.

How to get there from here is now obvious, however, when I first encountered this dilemma the […]

Date Formatter Examples – Take 2

In the first post on working with dates several of the examples use the “old style” date format syntax. The examples work, however, I want to show an additional example that uses the ICU (International Components for Unicode) library for format strings.

Here is a short list of sample formats using ICU:

The format specifiers are quite […]

WP Like Button Plugin by Free WordPress Templates