Categories and Private Methods in Objective-C

In an Objective-C class it is possible to declare instance variables with different levels of visibility. For public variables, any class or function that has a pointer to the object can access the content. Private ivars, however, are available only to the class itself.

Objective-C also provides protected instance variables, which can be read or written by the class where they are defined. These variables can also be accessed by any class that inherits from the containing class. These accessibility levels provide some assurance that the compiler will enforce the visibility of a particular variable. This, in turns, contributes to a better code organization.

It would be great to have the same levels of accessibility for methods, as well as for ivars. However, Objective-C does not work in this way. Messages are resolved only at run time, which means that the compiler cannot enforce such access rules during compilation.

While there is no way to enforce access rules on methods, we can use categories as a simple technique to reduce methods visibility. This is possible because a category can be created as an effective way to encompass one or more methods that are known only in the local (file) context.

Defining Categories

We need to define a new category at the beginning of an implementation file. You can use any name for that category. Traditionally, people have used the name Private for this purpose, but Objective-C also allows the name of the category to be empty, resulting in anonymous categories.

In that case, we don’t need to worry about the right name for the category. But the greatest advantage of using anonymous categories is that there is no need to create a separate implementation section in your file, thus reducing the amount code to maintain.

The content of the anonymous category will be composed of all methods we would like to make private. These methods can be accessed only by code pertaining to the original class and contained in the same implementation file. Here is an example, where we implement a Library class with an anonymous category. The only method in that category is getPrivateBookTitles.

// file: Library.h
@interface Library : NSObject
{
	// instance variables here
}
// standard methods here
@end

// file: Library.m
@interface Library ()

- (NSArray *) getPrivateBookTitles;

@end

@implementation Library

@synthesize numericProperty = _numberOfBooks; // give a different name

- (NSArray *) getPrivateBookTitles
{
	NSArray *books = @[];
	// code to find private books 
	return books;
}

@end

Even when using anonymous categories, it is necessary to keep in mind that the mechanism doesn’t offer run time protection for methods declared in this way. After all, it is still possible for the user of a class to send a message that will be resolved to a method in such a category.

Remember that the mechanism of message dispatching works in such a way that doesn’t care about the target of the message – as long as the message can be dispatched successfully. If you really don’t want some code to be executed by external classes, the best way to achieve this would be to use a static C function.

About the Author

Carlos Oliveira is a software engineer and the author of Objective-C Programmers Reference.

Related Books

Here are some other books on this topic:

Programming in Objective-C

Objective-C Programming

How to Handle Debugging Problems

Debugging is hard, and one of the main reasons why programmers spend so much time fixing previous issues. It is no surprise that spending time on code debugging will inevitably reduce the available opportunities for more productive endeavors.

That is why it is so important to learn how to handle bugs efficiently. First, let’s see the main culprits for the introduction of bugs in software. Here is my list of the top items that contribute to their occurrence:

Unclear code: if your code is hard to read, it will also be hard to maintain. Problems usually happen in areas that are poorly understood. You should strive to make your code as clear as possible, and reduce the possibility of errors caused by hard-to-read software.

Badly engineered code: another big culprit is code that hasn’t been designed correctly. If your design doesn’t work from the beginning, there is not way to avoid bugs.

Bad requirements: sometimes the problem is not on the programmer, but on requirements that change, provoking problems down the road when the system is integrated and used.

To handle these problems, you need to do some reverse engineering, and think about the problems I mentioned above.

Is the code unclear? If so, how would you write it in a clear way? Even if you cannot rewrite the system to improve it, you will at least have a mental model of how it should work. And in the process of fixing the bug, you can most certainly clarify the way the code is used, so that future problems will occur less frequently.

Is the system badly engineered? When this happens, you can propose ways to fix the system. Redesigning how the whole system works can avoid big trouble in the future. So, the occurrence of a bug may very well be the sign that the system needs additional work on the design side.

Bad requirements are the problem? In this case, you should clarify the requirements, and probably write them down so that in the future you can refer to them. Business requirements are a common source of confusion, because different users have a different view of how the system should work. As a developer, your work should be to come up with a clear solution that can satisfy the basic needs of all users, if possible.

Objective-C Programmer’s Reference

41BgjpodVWL._SY300_Have you ever wished to find a concise and complete reference to Objective-C? Objective-C Programmer’s reference teaches everything you need to know about the language to become a productive developer of iPhone, iPad, and Mac OS X apps. The book can now be preordered from its Amazon web page.

Here is a list of topics covered in the book:

Introduction

We provide here the main motivation for learning Objective-C, explaining how it is used, and how it was adopted for the creation of software in the Apple platform. We also provide an introduction to the Xcode environment, the IDE used to create Objective-C applications.

  1. Why Using Objective-C?
  2. Setting Up the Xcode Environment

Chapter 1: The C in Objective-C

The Objective-C language was created as an extension of C, which means that any program written in C is also a valid Objective-C program. As a result, many of the basic expressions in Objective-C are identical to their equivalents in C. To become a good Objective-C programmer, you need to have a basic understanding of the C language. In this chapter, we provide a quick introduction to the most important concepts from the C language used in Objective-C.

  1. Simple Expressions
  2. Functions
  3. Loops
  4. Conditional Expressions
  5. Structures
  6. Arrays
  7. 7.     Pointers

Chapter 2: Classes

In Objective-C, classes are the building blocks of applications. They are the syntactic construct used to define the methods and variables that become the components of an object. This chapter gives an introduction to the concept of Objective-C classes, and how to define them. One of the main features of classes is how they can be described in separate files: one for the interface and another for the implementation. This clear separation makes it possible to share only the important aspects of a class, while hiding any implementation information that is not needed by clients.

1)     Defining a New Class

2)     Adding Member Variables

3)     Adding and Defining Instance Methods

4)     Using Class Methods

5)     Defining Class Properties

6)     Using Objective-C Frameworks

Chapter 3: Strings and Container Classes

One of the main characteristics of Objective-C programs is the constant use of dynamic objects to simplify the management of memory and other resources. Among the most fundamental classes in the Objective-C runtime are the classes used to handle strings, numbers, and collections of objects. In this chapter we show how to use these basic classes and their methods.

1)     String Classes

2)     Numeric Classes

3)     Mutable and Immutable Containers

4)     Array Containers

5)     Dictionary Containers

 

Chapter 4: Protocols and Categories

Objective-C promotes a clear separation between implementation and interfaces. One of the techniques used to encourage this separation is the concept of protocols. A protocol allows classes to communicate by just describing a subset of the methods they respond to. Another way to promote this separation of concerns is to extend classes through categories: instead of making everyone aware of the methods available at a particular class, a programmer can add categories to classes as they are needed. This results in a much cleaner implementation, which reduces dependencies on other parts of the application.

1)     Declaring Protocols

2)     Understanding Protocols

3)     Creating Categories

4)     Extending Existing Classes

Chapter 5: Inheritance

Object Oriented Programming is the main idea that motivated the creation of Objective-C. An important aspect of OOP is the ability to create new classes that inherit the methods, variables and properties of existing classes. The proper use of inheritance, however, requires a certain care both the in the design phase as well as in the proper implementation of derived classes. In this chapter we cover the main ideas that you need to be familiar with when creating class hierarchies.

1)     The Concept of Inheritance

2)     The init Method in Derived Classes

3)     The Proper Use of the super Keyword

4)     When to Use Inheritance

Chapter 6: Block Syntax

Objective-C provides another technique to reduce programming effort through its block syntax. A block is a piece of code that can be passed around to other parts of the application. A block not only retains information about the code but also about variables that were in scope at the time the block was created. In this way, blocks are similar to the concept of closures available in other languages. By defining blocks, you can make the implementation of certain algorithms more extensible. You can also use blocks to implement code that will run in other threads, as accomplished by some Objective-C libraries.

1)     Defining Blocks.

2)     Sharing Variables with Blocks.

3)     Blocks in The Foundation Framework.

 

 

Chapter 7: Dynamic Binding

One of the biggest advantages of Objective-C is that it allows programmers to choose the right combination of speed and flexibility, depending on their needs. The runtime system of Objective-C is the main mechanism that can be used by programmers to switch between dynamic and static programming.  In this chapter we learn how to explore the run time system to achieve the best combination of static checking and dynamic flexibility in your software.

1)     Using method selectors

2)     Checking for Specific Messages

3)     Intercepting Messages

4)     Dynamically Responding to Messages

5)     Reflection and Instance Variables

Chapter 8: Memory Management

One of the advantages of working with a object oriented language with dynamic features is that memory management can be simplified, and most of the work is done by system libraries. Objective-C uses a reference-based model for memory management, with rules that makes it almost straightforward to allocate and release memory in any application. In this chapter we review the simple rules that you need to master in order to write correct objective-C code. We also cover the latest techniques provided by Apple compilers, which practically remove the need for manual intervention in the memory management mechanisms.

1)     Allocating memory

2)     Releasing memory

3)     The retain method

4)     Memory management rules

5)     Deallocating objects

6)     Using memory pools

7)     Using Automatic Reference Counting

8)     Using Garbage Collection

 

Chapter 9: Key-Value Programming

A very common way to interact with objects in Objective-C is to set values for particular property names, also referred to as keys. Key-value programming is very important because it is used in so many libraries in Objective-C. It allows an object to be used through a simple interface, avoiding the need to create multiple subclasses when the only difference between objects is the in the set of values they contain.

1)     The Concept of Key-Value Programming

2)     Working with Key-Value Based Objects

3)     Implementing Key-Value Based Interfaces

4)     Examples of Libraries That Use Key-Value Programing

Chapter 10: The Filesystem

Objective-C libraries provide a simplified mechanism for accessing system resources. One example is how the Foundation Framework can be used to access files and directories. In this chapter we show how the classes in this standard Objective-C framework can be used to create files, read and write data to existing files, and manipulate the content of directories.

  1. Creating Files with the Foundation Framework
  2. Reading Directories
  3. Changing Permissions
  4. Reading Binary Data

Chapter 11: The Foundation Framework

Classes in Objective-C are organized into frameworks for easy of distribution and use. The Foundation Framework provides classes needed to write programs interacting with the environment. The basic utility classes such as strings, dictionaries, and arrays are also contained in the Foundation Framework. In this chapter, we provide a reference to the Foundation Frameworks classes, with examples where necessary.

  1. The Main Components
  2. List of Foundation Framework classes.

Chapter 12: The Compiler

To use Objective-C effectively, it is important to understand the basic infrastructure of the language. The compiler plays a special role in the software development cycle, since it determines how source code is translated into executable programs. In the Apple environment, two main compilers are used: gcc, the traditional compiler used by Mac OS X, and the new compiler based on the LLVM (lower level virtual machine), which has been developed by Apple in the last few years. In this chapter we take a look at the several options provided by the Objective-C compilers and how to use them to write fast and reliable code.

  1. Choosing a compiler
  2. Compiler options
  3. Compiler options exclusive to the iOS platform
  4. Code optimization
  5. Debugging options

Chapter 13: The Preprocessor

Objective-C comes with a powerful pre-processor that can simplify the input of repetitive code into a program. The pre-processor, however, may be the source of numerous programming pitfalls, if you are not careful with its usage. In this chapter we present the main features of the pre-processor and how to avoid some of its rough edges.

1)     The #import mechanism

2)     Defining constants

3)     Defining macros

4)     Avoiding Problems with Macros

 

 

Chapter 14: Unit Tests

Test driven development is a software methodology that requires that every feature of a program to be thoroughly tested with unit testing code. Test driven development is a great way to employ the modularity of object oriented languages such as Objective-C. In this chapter we learn how to create unit tests in the Objective-C language. We will also see how to manage and run unit tests using the X-Code IDE.

  1. Unit testing libraries in Objective-C
  2. Creating unit tests
  3. Practicing test driven development
  4. Running test on X-Code

Chapter 15: Debugging

Any programmer knows how difficult it is to write defect-free software. That is why every platform has a number of integrated debugging aids. Xcode provides a complete solution for writing and debugging programs, which makes it much easier to find bugs in a new or existing applications. In this chapter we explain how to use these debugging features. Using a tutorial style, we describe how to use features such as breakpoints, watch windows, conditional expressions, and the logging terminal.

  1. Setting breakpoints
  2. Watching expressions
  3. Investigating local objects
  4. Using the logging terminal

Chapter 16: Building a Mac OS X Application in Cocoa

The most important use of Objective-C is in the implementation of applications that employ the Cocoa Frameworks. As an Objective-C programmer, your will most certainly be involved in the creation of iOS apps or full Mac OS X applications. In this chapter, we will introduce Cocoa, the framework used to develop desktop applications in the Mac OS X.

1)     A simple Mac OS X application

  1. Setting up a Cocoa Project
  2. Writing a Simple Mac OS X Application
  3. c.     Running and Testing the Application

 

Chapter 17: Building an iOS App on Cocoa Touch

Cocoa touch is the framework used to create rich applications for iOS, the operating system that powers the iPhone, iPad, and iPod touch. While using technologies similar to Cocoa, iOS provides a simplified architecture in comparison to the full-featured Cocoa framework. Cocoa Touch also provides access to mobile resources such as cameras, the accelerometer, and other hardware managed by iOS.

1)     A Simple iOS App

  1. Setting a Cocoa Touch Project
  2. Writing a Simple App
  3. Running and Testing the App

2)     Were to Go From Here