Software development is a daunting business, even with the legendary ease of use of the Mac, and that is one reason that most computer owners don't do it. Few people want to grapple with the intricacies of Java or C++ and even fewer want to learn how to design complex software systems.
So would you be interested in being able to write applications by doing not much more than drawing the user interface for your software and without writing one single line of code? Of course you would. Read on.
This short tutorial is about writing a simple database application for the Mac without writing one line of code. We'll be using the Apple XCode tools - notably the Project Builder, Interface Builder and Data Designer. These terms may be new to you, but don't worry: you won't have to go too deeply into any of the tools for this tutorial.
Most recent Macs have come with the XCode tools on DVD as part of the installation disk set. If you don't have it already, though, don't worry - you can get it for free from the Apple Developer website (http://developer.apple.com/tools/download/). You do have to sign up to the Apple Developer Program but this is free and painless.
In this short tutorial I am going to write a subscription manager for a Mac User Group. This will record simple contact information for members.
Although there will be no code involved in writing this program, there is a certain amount of planning needed. The main thing I need to plan is what information I want to record in my database. I came up with these items:
With XCode you always start by creating a project. This is basically where all of your code, user interface, database design and so on are kept together.
Start up XCode by double-clicking on the Blue Hammer icon. You'll find this in [disk name]:Developer:Applications. Slightly unnervingly, XCode starts up with a blank screen, but once you get into it you can tailor to suit your preferred way of working.
I am going to create a new project. File->New Project... (see below). There is a huge range of projects to choose from, and here I am choosing a Core Data Application. Core Data is Apple's collection of user-friendly tools for building database applications. In the next window I choose a name and location for my project. I am choosing MMUGDB and just electing to put the project folder in my home folder. Click on Finish.
Yes, at this stage we already have a working program. You will see an interface like the one below, and if you hit the Build and Go button you will see the program running. It doesn't do much, for sure, but this illustrates how much work Apple have done to take the headache out of writing software.
I am not going to bore you with the complexities of the XCode interface, because we are really not going to be using much of it today. In fact the only thing we are going to do here is to write down our list of data items from above. In the list of components in the main XCode window you will see MMUGDB_DataModel.xcdatamodel. Geeky or what. However double-click on this and we will see the tool we need to use - the Data Modeller.
We need to use this to capture our list of items for our database application. For a database as simple as this we can put all of the items in one place - called a table in database parlance, and for data modelling programs like this an entity. So I click on the little plus button at the bottom of the Entity list, and create a table called subscribers.
Now I just have to add in the list of items. These are called fields in databases and Attributes in the Data Modeller. So using the plus sign again I add in each of the items above. As I add each one, I define the type of data I want to store. It comes down to String for text, Integer 16 for numbers and Date for dates. The finished list of items - it's really a data model now - looks like this:
The grid-patterned area below the list of entities and attributes is used to graphically display your data structure. For complex databases you might have many entities and many attributes, as well as relationships between those entities. But there I go, talking techie - we don't need any of this complexity for what we're doing here. Now I can save the data model but I'm not going to close it just yet - I have a neat trick to play with this.
We are actually nearly there for our application, and the nicest bit is next. If I click on the blue Project button at the top right I can see the Project window again. Now I am going to build the user interface. Looking in the list of components in the Project window I see an item called MainMenu.nib. If I double-click on this I see the Interface Builder tool. This is used to create the UI for the program.
Now, prepare to be amazed. I need to arrange my windows so that I can see both the user interface window (just a blank window at present) and the data model window. Now, I select the data model and opt-drag it over the user interface window.
A dialog pops up asking whether I want to create an interface for one or many objects. I choose many. And as you can see, Interface Builder creates me an interface for all of the data objects.
Isn't that brilliant? All of the interface components have been added to the window for us, making a complete working interface for adding, finding, editing and deleting records with this database. But it is even better than that: what you don't see here, but has happened all the same, is that all of the database access code, search code and everything else has also been written for you under the covers. Naturally you can make any changes you want to this interface to make it look the way you want it. I'm not going to do that right now. Instead I am going to finish the application. Save and close Interface Builder, and close the Data Model tool - we don't need those any more.
The program is complete now: all we need to do is compile and run the final release version. Back in the XCode Project window, I need to set the project up to create release software (Project->Set Active Build Configuration->Release). Then I just build it - Build->Build or cmd-B.
We are done - really, that's it. I can close down XCode.
So where is the program? Well, you may remember that we put the project in my home folder. Sure enough, there is a folder there called MMUG. If I open this up I can see this folder structure. There is the MMUGDB program.
We can put this anywhere we like to run it. And where is the data? By default when you create a Core Data application the data is stored in an xml data store, and you will find it in ~/Library/Application Support/MMUG. You can opt to store the database in binary format or SQLite if you wish (you need to get into the code for this): in practice I have found the xml format easy, quick and flexible.
Well, I have a working application - and actually I have created a number of utility database programs that I use daily that have had virtually no more work done on them than this. The beauty of Apple's development tools is that you really can build useful software this easily. However, you can explore Interface Builder to enhance the interface. You can build more sophisticated data models that have multiple tables and relationships and default data. And you can write code if you really must!
There are not many examples of the use of Core Data Framework on the web, though there are some examples installed in the Developer folders when you install XCode. However here are some links to resources that I have found useful.
A Quicktime-based video tutorial - very clear, but involves coding
A comprehensive Core Data tutorial - involves coding
Tellura is not a conventional company. When we work with you, we won't just look at your watch and tell you the time. We will take a friendly, common-sense, real-world approach to finding out how you do what you do, how you want to change and where you want to go. We will be clear about where we think our skills and experience can help. We will be clear about our costs. And if we don't think we can help, we will say so, up front.
Get in touch with us. If we can help you we will be delighted to do so.