Fetching the Application Delegate

When developing for the Mac or the iPhone we frequently run into the situation of needing to grab the delegate.  Thankfully this is stored in a singleton object and very easy to fetch:

#import "MyAppDelegate.h"
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
Posted in Uncategorized | Leave a comment

Patterns in Objective-C: Strategy Pattern using Forwarding

When working with the Observer Pattern and the Singleton Pattern it is easy to see how they relate to the Java or C++ versions of the same concept. Everything basically plugs into the same places you would expect, coming from another Object Oriented language. The implementation isn’t all that different.

We are going to see something a little more interesting when working with the Strategy Pattern in Objective-C. The Strategy Pattern is one of the most basic patterns and is even covered in the introduction to Head First Design Patterns. While the concept is identical in Objective-C as it is in Java and C++, the implementation is much closer to languages with first-class functions such as Python.

To do this, we will take advantage of one of the more powerful features of Objective-C: Forwarding.
Continue reading

Posted in Uncategorized | Leave a comment

Variable Arguments (varargs) in Objective-C

Creating variable argument functions (also vararg or variadic function) in Objective-C is done the same way that it is done in C, using the stdarg.h library.

For those of us working in Cocoa, Apple has already included the import in NSObjCRuntime.h, so the only thing we have to do is start using it.
Continue reading

Posted in Uncategorized | Leave a comment

Importing a Framework

Commonly when working with the iPhone SDK we run into a situation where the specific library we need is in a precompiled framework. These frameworks are libraries: collections of functionality thrown together into one package.

Using a framework is easy:

  1. Import it into your project
  2. Import the specific header file into your code

To start off right-click on the “Frameworks” folder in XCode and select Add->Existing Frameworks…

The frameworks for iPhone development are stored in: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/System/Library/Frameworks (where iPhoneOS2.2.sdk can be substituted for the appropriate operating system version) so navigate here and select the framework that you are interested in. For our purposes here we’ll assume that it is MediaPlayer.framework.

After that is loaded select which header file you want (we’ll assume it is MPMoviePlayerController.h) and import it using angle-bracket notation, using the framework as the path:

#import <MediaPlayer/MPMoviePlayerController.h>

Here it is the MediaPlayer framework, so MediaPlayer is the “directory” and MPMoviePlayerController.h is the header file you want to import.

Posted in Uncategorized | Leave a comment

Patterns in Objective-C: Observer Pattern

One of the most common patterns, after the Singleton, in Java is the Observer Pattern. Also referred to as a Broadcaster/Listener or a Publish/Subscribe pattern. The basic principle is that there are two components: An Observer (or Listener) and a Subject (or Broadcaster).

The basic idea is that a listener registers with a broadcaster using some predefined protocol. At some later point, the broadcaster is told to notify all of its listeners, where it calls some function on each of its listeners and passes certain arguments along. This allows for asynchronous message passing between two different objects that don’t have to know about one-another, they just have to know about the broadcaster.

Examples of where this gets used are to track database updates (i.e., listeners want to know when the database updates in specific ways), handle click events, or implement shutdown hooks.

Apple has provided an Observer Pattern in the Cocoa library called the NSNotificationCenter. There are three primary tasks that are performed with the NSNotificationCenter:

Adding a Observer
[[NSNotificationCenter defaultCenter] addObserver:listener selector:@selector(notify:) name:@"Event" object:notificationSendert];

Here we see several components:

  • [NSNotificationCenter defaultCenter] – While one can create their own NSNotificationCenters, for most purposes it is sufficient to use the default.
  • addObserver:listenerlistener is the object that will be notified by the NSNotificationCenter. For many applications the observer will “self register” with the observer, and thus the call will be addObserver:self.
  • selector:@selector(notify:) – Indicates which method should be called when the observer is notified.
  • name:@”Event” – The name of the event that will cause the NSNotificationCenter to notify the observer. It is a good practice to use an extern string constant instead of directly inserting the string.
  • object:notificationSender – The notificationSender represents the object that will post the notification. This is frequently used when the object in question needs to be released once a notification happens.

This is frequently nil, meaning that the NSNotificationCenter will not consider the sender when deciding whether to fire the event. If it is not nil, then only notifications sent from that specific object will fire the event (see below).

Note that if name is left as nil then all notifications sent by the notificationSender will fire the event.

The notify function can be any function that takes a single argument:

- (void)notify:(NSNotification *)notification {
	id notificationSender = [notification object];
	//do stuff
Notifying the Observers
In some cases, such as when registering for events with Cocoa’s built-in libraries (e.g., MPMoviePlayerController) the notification event is called by the library when a certain condition is met, for example when the movie ends.

In other cases, we will want to trigger that event:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Event" object:notificationSender];

The components here are similar to when we added the observer above. The postNotificationName is identical to the name, and the object is the notificationSender>.

Removing an Observer
When we are done with the observer, we will want to unregister it with the NSNotificationCenter:

[[NSNotificationCenter defaultCenter] removeObserver:listener name:@"Event" object:notificationSender];

The arguments here are the same as when we added the observer.

Posted in Uncategorized | 1 Comment

Static Constant Strings in Objective-C

One of the most commonly used features in Java is the ability to avoid string “magic constants” by putting those strings into static variables. These values are then readily accessible inside of the class, not necessarily visible outside of it, and are easy to modify on the fly if and when such is called for. They also allow me to create the string once, saving both memory and time.

When I started trying to build my iPhone application, I ran into the immediate problem: how can I abstract out my strings?
Continue reading

Posted in Uncategorized | 1 Comment

Patterns in Objective-C: Singleton Pattern

One of the challenges in switching to a new language is figuring out how design patterns adapt.   Sometimes the language has built-in facilities for the pattern, or features that work-around what the pattern was designed to do in the first place.
Continue reading

Posted in Uncategorized | Leave a comment

Python, Subversion, and revision diffs

One of my hobbies is working as an amateur translator for scripts (of the textual kind) and assorted short stories. Since I move around to different machines often, it just makes sense to put everything on subversion. Of course, when you have a subversion repository lying around, you start wondering what sorts of data is lurking in it right?
Continue reading

Posted in Uncategorized | Leave a comment

Types of Data

There are four types of data, or levels of measurement, that show up in databases.  Everything that goes into the database or shows up on a survey corresponds to one of these levels, from address fields to income.  It is important to recognize which is which, because not all measures can be used on each of the types, and it is easy to make the wrong conclusions about the data by using the wrong measures.

The four types of data are:

Nominal data is where the numerical value is irrelevant to the label. It cannot be meaningfully ranked, and no meaningful math can be performed on it. An example might be a survey that asked what a user’s political party is (“1) Democrat, 2) Republican…”) and “What kind of computer do you own?” (“1) Apple, 2) Dell, 3) HP…”).

Data that is nominal cannot be directly compared. You can’t say that Apple is greater than or less than Dell, or that Red is greater than or less than Blue. Addition is also meaningless: what is Laptop + Desktop equal to?

The most common statistics for nominal data relate to frequency. We can compare the frequency that each of the items shows up, find the mode, and perform unions and intersections of sets of data.

Ordinal data can be ranked (1st, 2nd, 3rd), but cannot be added or subtracted meaningfully. Examples include questions such as “On a scale of 1-5, with 1 being ‘least satisfied’ and 5 being ‘most satisfied,’ how satisfied are you with your work station” and “How much do you make each year? 1) < $30k 2) $30k—$50k, etc.”

In these cases we can make direct comparisons, but we still cannot add, subtract, multiply or divide meaningfully.

One important characteristic of Ordinal data is that it cannot be averaged since the 5th number’s distance from the first number is entirely arbitrary. What we can do, to determine central tendency, is take the median. As an example of how this might work, consider a questionnaire where people have been asked to rank their job satisfaction on a scale of 1 to 5. 10 people report 1, 5 people report 2, 10 report 3, 20 report 4, and 10 report 5.

The mean of this sample is 3.3 and doesn’t mean much, but the median indicates that at least 50% of the people are at 4 or above and is a better measure of central tendency. It is very common to see companies take the arithmetic mean of ordinal data as part of their standard reports, which can lead to incorrect conclusions about the data.

Interval data consists of numbers on a scale. With interval data not only can two values be compared, but the difference between those values is meaningful. Addition and multiplication still are not meaningful, but the arithmetic mean is.

An example here might be an individual’s year of birth. If we have three people who were born in 1980, 1978, and 1982 we know that there is a four year difference between them and that the average birth year is 1980, but 1980+1978=3958 isn’t particularly meaningful.

We can perform most basic analysis on interval data. We can take the mean and the median, for example, and can also perform weighted averages and calculate standard deviations. However some of the more advanced options, most of which aren’t used in businesses, are unavailable.

With ratio data the ratio between two numbers is meaningful. One can meaningfully say that “a is twice b.” In order to achieve this ratio measures have a non-arbitrary zero value.

An example of a ratio measurement would be income: 0 is the breaking point between owing money and having money, and we can meaningfully say that \$200k is twice \$100k.

All statistical measures can be used here, including the geometric mean and the arithmetic mean. A word of caution though: just because they can be used does not mean that they should be used. Arithmetic means are sensitive to outliers, and certain tests depend on the data being normally distributed. More on this at a later date.

Posted in Uncategorized | Leave a comment

Arrays in Python

One of the first hurdles with doing data process in python is the format of the data. Our data naturally exists in rows and matrixes, but Python generally only allows us to work with one variable at a time. We can use for loops, list comprehensions, and a variety of other tools, but wouldn’t it be nice if we could simplify everything?

Using NumPy we can work directly with arrays and matrixes rather than with the individual values. It also provides a variety of tools for simple data processing.

The best way to install NumPy starts by installing setuptools. Once setuptools is installed (using the directions for your specific platform) run:

 easy_install numpy

After it finishes installing, you will see the following messages:

Processing dependencies for numpy
Finished processing dependencies for numpy

Now you can import numpy with one of the following commands:

from numpy import *
import numpy

A good description of the package along with the full documentation can be found online, and I’ll be providing some examples of common functions in a later edition.

Posted in Uncategorized | Leave a comment