How to Make a Simple Mac App on OS X 10.7 Tutorial: Part 1/3

This is a post by iOS Tutorial Team Member Ernesto García, a Mac and iOS developer founder of CocoaWithChurros. It’s a good time to be an iOS developer. Not only can you release your apps to both the iPhone and iPad App Stores, but you also have the foundational skills to become a Mac developer, […] By Ernesto García.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

A Scary Data Model: Organization

So far you have a window with a nice table view on it. But it does not do anything at all. You want it to display some information about your scary bugs – but wait, you don’t have any data to display either!

And having no data makes me a very sad panda. So in the next steps, you are going to create a data model for the application, but before that, you are going to show you a way to keep things organized in the Project Navigator.

Note: This is an optional section that shows you how to organize files into groups. If you’ve followed the How To Create A Simple iPhone App on iOS 5 Tutorial or already know how to do that, you can skip to the next section.

This your current organization in the Project Navigator section of XCode:

The default template creates a Group with the application’s name, and a sub-group for the supporting files (plist, resources,etc). When your project grows up, and you have to deal with lots of files, it becomes more and more difficult to find the files you need.

In this section we’re going to show a way to organize your files. This organization is quite subjective, so feel free to change it to any organization you feel comfortable with.

First, you are going to create a group to store your user interface file, and we’re going to name it “GUI”. To create it, control-Click (or click the mouse right button) the ScaryBugsMac group.

In the menu that pops up, choose “New Group”. The group created is automatically selected and you can type de new name “GUI”.

Now, drag the user interface files to that group ( AppDelegate.h/.m , MasterViewController.h/.m/.xib and MainMenu.xib). After dragging them, your Project Navigator should look like this:

Moving files into the GUI group

Now create a second group inside ScaryBugsMac, and name it “Model”. In the next steps we’re going to create the data model files for your application, and you will add those files to this group.

This is the way the Project Navigator looks after adding it.

Adding a model group into Xcode

Before we begin, let’s talk about how we’re going to organize things:

  • ScaryBugData: Contains bug name and rating.
  • ScaryBugDoc: Contains full size image, thumbnail image, ScaryBugData.

The reason we’re setting things up like that is it will make things easier in the follow-up for this tutorial, where we’re going to start saving our data to the disk.

A Scary Data Model: Implementation

Note: If you’ve followed the How To Create A Simple iPhone App on iOS 5 Tutorial, you will find that this section is (almost) identical to that. One of the good things about Mac/iOS programming is that they share most of the SDK, obviously, except the UI classes and some OS specific parts.

So, when you’re creating the model and classes that don’t need user interface, you will find that most of your code will likely just work on Mac, or it will work with some minor changes.

For instance, in this case, changing the ScaryBug model classes from iOS to Mac only required one change. UIImage does not exist in OSX, so you just needed to change it to OSX’s image class, NSImage. And that was it!

Let’s create the model. We’ll begin creating the ScaryBugData file.

In the project navigator, Control-Click the Model Group you just created, and in the menu click “New File…”. Select OS X\Cocoa\Objective-C class template, and click Next.

Name the class ScaryBugData, and enter NSObject for subclass. Click Next.

In the final popup, click Create again. If all went well, your Project Navigator should now look similar to this:

Now, we’re going to add the ScaryBugData source code.

First, select the ScaryBugData.h file, and replace all its content with the following:

#import <Foundation/Foundation.h>

@interface ScaryBugData : NSObject

@property (strong) NSString *title;
@property (assign) float rating;

- (id)initWithTitle:(NSString*)title rating:(float)rating;

@end

This is pretty simple stuff – we’re just declaring an object with two properties – a string for the name of the bug, and a float for how scary you rated it. you use two property attributes for these:

  • strong: This specifies that the runtime should automatically keep a strong reference to the object. This is a fancy way of saying that the ARC runtime will keep the object in memory as long as there’s a reference to it around, and deallocate it when no references remain. For more information, check out our Beginning ARC in iOS 5 Tutorial.
  • assign: This means the property is set directly, with no memory management involved. This is what you usually set for primitive (non-object) types like a float.

You also define an initializer for the class, so you can set the title and rating when you create the bug. Switch over to ScaryBugData.m and replace it with the following:

#import "ScaryBugData.h"

@implementation ScaryBugData

- (id)initWithTitle:(NSString*)title rating:(float)rating {
    if ((self = [super init])) {
        self.title = title;
        self.rating = rating;
    }
    return self;
}

@end

Again, extremely simple stuff here. You reate your initializer to fill in your instance variables from the passed-in parameters. Note there is no need for dealloc, since you are using ARC, and no need to synthesize your properties due to auto-synthesis.

Ok that’s it for ScaryBugData. Now follow the same steps you did above to create another subclass of NSObject, this time named ScaryBugDoc.

Replace ScaryBugDoc.h with the following:

#import <Foundation/Foundation.h>

@class ScaryBugData;

@interface ScaryBugDoc : NSObject

@property (strong) ScaryBugData *data;
@property (strong) NSImage *thumbImage;
@property (strong) NSImage *fullImage;

- (id)initWithTitle:(NSString*)title rating:(float)rating thumbImage:(NSImage *)thumbImage fullImage:(NSImage *)fullImage;

@end

Nothing of particular note here – just creating some instance variables/properties and an initializer.
Replace ScaryBugDoc.m with the following:

#import "ScaryBugDoc.h"
#import "ScaryBugData.h"

@implementation ScaryBugDoc

- (id)initWithTitle:(NSString*)title rating:(float)rating thumbImage:(NSImage *)thumbImage fullImage:(NSImage *)fullImage {
    if ((self = [super init])) {
        self.data = [[ScaryBugData alloc] initWithTitle:title rating:rating];
        self.thumbImage = thumbImage;
        self.fullImage = fullImage;
    }
    return self;
}

@end

And that’s it – your data model is complete!

At this point you should compile and run your application to check that everything runs fine. You should expect to see the same empty list, because you haven’t yet connected the data model with the UI.

Now you have a data model, but you don’t have any data. you need to create a list of ScaryBugDocs, and you will store it in a NSMutableArray. We’ll add a property to the MasterViewController to keep track of your list of Bugs.

Select MasterViewController.h and add the following line, between the @interface and @end lines:

@property (strong) NSMutableArray *bugs;

This will be the instance variable/property that we’ll use to keep track of your list of bugs. Next let’s hook it up!

Ernesto García

Contributors

Ernesto García

Author

Over 300 content creators. Join our team.