Basics for new Mac and iPhone Programmers

general computer Add comments

Apple provides a great programming environment for OS X and the iPhone. However, if you are new to Mac programming, it can be intimidating even for professionals. Read on to learn a few basics about Cocoa, Objective-C and Xcode.

Xcode and Interface Builder

Xcode is Apple’s “IDE” or Integrated Development Environment. If you have used Microsoft Visual Studio or a Borland product such as Delphi or Kylix, you have used an IDE. Xcode is a code editor, debugger, and project manager.

Apple’s Interface Builder is a separate program, but it works hand in hand with Xcode to provide programmers with a visual drag-n-drop WYSIWYG system to build the user interface of your applications.

If you are coming to the Mac from Visual Studio or Delphi, just remember that on the Mac, you will be using both Xcode and Interface Builder. Since you will be using two separate software packages, you should be prepared that the method for building Mac applications will be a little different compared to Visual Studio or Delphi which have the code editor and visual interface builder all rolled into a single software package.

Unlike Visual Studio or Delphi, both Xcode and Interface Builder come with all new Macs for free. If they are not already installed on your Mac, you can install them from the DVD that came with your Mac or you can download them from Apple.

Objective-C Syntax

Objective-C is the programming language of choice for Mac and iPhone programming. It scares off a lot of prospective Mac programmers. If you jump right into some Objective-C without getting your bearings first, you will be lost. While it has a learning curve, getting a few basics under your belt will ease the transition.

The good news is that Objective-C is not really that complex. It just looks foreign. Objective-C is actually a super-set of the venerable C language. That means that its just plain old C, but with some added syntax to make it object-oriented. Why didn’t Apple choose C++ if they  wanted object-orientation? We’ll touch on that later when we discuss Cocoa and NeXTSTEP.

In some object-oriented languages, like C++ or PHP, objects and their methods are typically referenced as follows:

myobject -> dosomething

In languages like Ruby, Delphi, or C#, where the dot notation is available, you might see something like:

myobject.dosomething

In Objective-C, this would be:

[myobject dosomething]

Basically, you enclose calls to objects inside square brackets and separate the name of your object from its method with a space. What about methods that take parameters?

[myobject dosomething height:6 weight:160]

Get the idea? You add the name of each parameter followed by a colon and the value you want to send. Once you get used to it, it is fairly readable. Think “ask myboject to dosomething with a height of 6 and a weight of 160.”

Just to be complete, note that the newest version of Xcode on the Mac supports dot notation in Objective-C, so the following will also now work:

myobject.dosomething

I recommend that you learn the old style first (using brackets) as most of the sample code you will see is written in the older style. Some programmers dislike dot notation, but its a personal preference. Once you build your confidence with the old style, then you can try some dot notation and decide what works best for you.

Compiling Objective-C

Did Apple make a new compiler for Objective-C to work? Not at all. The open source compiler, known as “GCC” and widely available on Linux and Unix, is called by Xcode to compile your program every time. GCC can handle Objective-C, C, C++ and some other languages as well. You can mix C and C++ into your Mac programming, but its best to stick with Objective-C as often as you can.

Case Sensitivity

Objective-C is case-sensitive, so myObject is different than myobject. The Mac tradition is to name objects with a lowercase letter first, so MyObject would be frowned upon, while myObject or mySecondObject would be applauded by other Mac developers.

Strange Symbols

Another source of confusion for new Mac programmers is the @ symbol. You might see:

@interface myaccount: Object { …. }

@interface tells the compiler that you are declaring an object’s variables and methods. There are some other @ symbols, like @implementation that you’ll come across and use.

You’ll also see lines starting with a dash:

- (NSString *)firstname

The leading dash means that the function being provided is an instance method. That’s something you would not see in C.

You might also see (id) used a lot. (id) just means that you are referencing a generic Objective-C object and its basically a stand-in for any Objective-C object.

IBOutlet and IBAction

IBOutlet and IBaction are used in code like this:

IBOutlet NSTextField *mytextfield;

- (IBAction) addEmail: (id)sender; 

Basically, IBOutlet and IBAction are keywords that only Interface Builder (IB) reads to match your visual interface with your programming code. Remember I said that Xcode and Interface Builder are actually separate programs? These keywords allow them to work together. In Interface Builder, objects you create visually have “actions” and “outlets”. These keywords tie it all together for you and allow the system to connect your functions with their interface elements. For example, if you want the user to press a button and make a sound, you’ll use Interface Builder to draw the button and then you’ll use IBAction and IBOutlet keywords in your code to help connect your button to your sound functions.

H and M Files

In Objective-C, you will typically break up your programming into header files (ending in “.h”) where you declare your objects, and then your function code will be in files ending with “.m”.

NIB and XIB Files

Interface Builder creates its own files which represent the visual design you have created for your program’s interface. These files are called NIB files but for newer versions of Xcode and Interface Builder, you’ll also see XIB. They’re not exactly the same, but they’re close enough for you to get started.

Interestingly, a lot of commercial Mac programs have their NIB/XIB files available to you. Applications on the Mac are not typically just an executable file, they are actually a folder with the application’s resources inside. You can find the NIB/XIB files inside the folder and if you change a NIB file (carefully) in Interface Builder, you can actually change the visual layout of a commercial application. Of course, you can also break the application if you’re not careful.

Cocoa

Over the years, Apple has provided some different programming frameworks for developers to build Mac applications. Frameworks are libraries and bundles of pre-written routines to help you get your application built and running quickly. If you had to program every little thing from scratch it would take too long. The previous Apple programming system was called “Carbon” but the latest and greatest is called Cocoa.

Cocoa is a slew of Objective-C object-oriented frameworks that allow you to build your applications quickly. By using the Cocoa “WebKit” framework for example, you can build web browser functionality into your application with just a few lines of code. “AppKit” is another important framework that provides a lot of the base code you will use for a graphical application on the Mac.

iPhone and Cocoa

Since the iPhone interface is a little different than a normal computer, Apple created some Cocoa frameworks for the iPhone. “UIKit” is the framework on the iPhone to use in place of “AppKit”.

NS This and NS That

When you work with Cocoa, you’ll be seeing NS everywhere. For example, NSString is a Cocoa string object which gives you string functions beyond a conventional C string. “NS” is just a prefix that stands for NeXTSTEP. Items that begin with NS are just Cocoa objects.

NeXTSTEP

Learn Mac Programming

When Steve Jobs left Apple in the 1980s and started the NeXT computer company, those computers ran the high performance Unix-based NeXTSTEP operating system. When Steve Jobs came back to Apple in the 1990s, and Apple was struggling to build a new operating system, they used parts of BSD Unix and parts of NeXTSTEP.

NeXTSTEP was built with Objective-C and that’s why Objective-C is still so important to Apple. NeXTSTEP even had something similar to the current Interface Builder. That’s why Apple did not rebuild Cocoa as a C++ library. By leveraging the NeXTSTEP Objective-C frameworks and tools, and calling them Cocoa, Apple saved a lot of time and knew they had something that already worked well.  So, if you see “NS” just go with it, its just a name.

Your Next Step

So these were some basics to keep in mind as you begin your Mac or iPhone programming journey. To get started quickly, I highly recommend Cocoa Programming for Mac OS X by Aaron Hillegass.

Bookmark on del.icio.us
Bookmark on del.icio.us

18 Responses to “Basics for new Mac and iPhone Programmers”

  1. allin Says:

    thank you helps a lot

  2. Jon Hess Says:

    This is great info for people new to the platform.

  3. Ocelot Says:

    OK

  4. Quartz Watches Says:

    It’s not hard to realize decisions when you separate what your values are.

  5. Appareils photo Pentax Says:

    But now I have be stricken to put faith that the fit domain is an puzzle, a harmless conundrum that is made rueful by our own mad effort to explicate it as in spite of it had an underlying truth.

  6. JVC caméscope Says:

    But now I contain show up to feel that the all in all community is an riddle, a non-toxic poser that is made hideous not later than our own fuming effort to spell out it as though it had an underlying truth.

  7. tennis tables Says:

    A gink begins sneering his perceptiveness teeth the initially often he bites eccentric more than he can chew.

  8. paintball gun Says:

    A human beings begins scathing his perceptiveness teeth the initially often he bites out more than he can chew.

  9. kayak paddles Says:

    A gink begins scathing his perceptiveness teeth the first often he bites on holiday more than he can chew.

  10. bodyboard Says:

    A man begins scathing his perceptiveness teeth the senior time he bites out more than he can chew.

  11. snowboards Says:

    A man begins sneering his discernment teeth the senior often he bites on holiday more than he can chew.

  12. PBTllZ Says:

    QxiBuii

  13. Fossil Wrist Watches Says:

    To be a noble benign being is to procure a kind of openness to the far-out, an skill to trusteeship uncertain things beyond your own control, that can front you to be shattered in uncommonly outermost circumstances for which you were not to blame. That says something very weighty about the fettle of the principled compulsion: that it is based on a conviction in the fitful and on a willingness to be exposed; it’s based on being more like a spy than like a treasure, something kind of feeble, but whose extremely item attractiveness is inseparable from that fragility.

  14. Nike Fury Watch Says:

    To be a upright charitable being is to procure a amiable of openness to the mankind, an ability to guardianship uncertain things beyond your own manage, that can govern you to be shattered in uncommonly exceptionally circumstances pro which you were not to blame. That says something very weighty about the condition of the ethical compulsion: that it is based on a conviction in the fitful and on a willingness to be exposed; it’s based on being more like a spy than like a treasure, something somewhat fragile, but whose extremely particular handsomeness is inseparable from that fragility.

  15. Gucci Collection Watch Says:

    To be a noble charitable being is to have a make of openness to the far-out, an gift to trust unsure things beyond your own manage, that can front you to be shattered in hugely outermost circumstances pro which you were not to blame. That says something very outstanding relating to the fettle of the honest compulsion: that it is based on a conviction in the up in the air and on a willingness to be exposed; it’s based on being more like a shop than like a prize, something fairly feeble, but whose mere particular beauty is inseparable from that fragility.

  16. Marc Ecko Ladies Watch Says:

    To be a good human being is to procure a amiable of openness to the in the seventh heaven, an cleverness to guardianship uncertain things beyond your own restrain, that can take you to be shattered in unequivocally exceptionally circumstances for which you were not to blame. That says something remarkably outstanding about the condition of the honest compulsion: that it is based on a conviction in the unpredictable and on a willingness to be exposed; it’s based on being more like a plant than like a prize, something fairly tenuous, but whose very item handsomeness is inseparable from that fragility.

  17. erektile dysfunktionen Says:

    Work out ferments the humors, casts them into their adapted channels, throws eccentric redundancies, and helps nature in those hush-hush distributions, without which the body cannot subsist in its vigor, nor the man role of with cheerfulness.

  18. ejercicios impotencia Says:

    Exercise ferments the humors, casts them into their meet channels, throws off redundancies, and helps feather in those hush-hush distributions, without which the solidity cannot subsist in its vigor, nor the incarnation dissimulate with cheerfulness.

Leave a Reply

©2007-2010 Derek Underwood. All rights reserved.
Entries RSS Comments RSS Login