This is a series of guides about OS X Programming.
Who Should Read this:
This is a continuation of a guide that teaches people how to program in OS X. You can find the previous article here.
This tutorial is written for both beginners and experienced programmers alike. Much of it can probably be skipped by skilled developers. You should at least have good experience with Mac OS X. This part does not include using OS X yet, and more focuses on the background of OS X programming and the Objective-C language.
For “already” programmers, this guide focuses on programmers that develop in C, C++, BASIC, Java, and other similar languages.
Organization:
I have split up the categories and concepts into grouped paragraphs to make it easier to skip or navigate through the article. An example of this is right below with the title of the group: “Basics of C“, in bold. I have also highlighted parts of the text like this: “example“, for beginner programmers that other developers can pass over. There is also something called “In Depth“, which explains more in detail of something.
Basics of C:
C is the most widely used language in the computer world. Almost every language whether C++, Objective-C, or Java, your using the concepts and ideas created by C.
In Depth:
In the 60s, AT&T Bell labs was developing an operating system called Unics, they created a language to write it in called B. In the late 60s to early 70s, the programmers wanted to make the OS more portable, B was to integrated with the hardware to allow this. They need to develope a new Languages, this is what started the development of C. A few years later, AT&T Bell labs finished their OS in C renaming it UNIX. C is a very portable system that can rest on almost any hardware setup making it very popular. C was also very easy to use compared to other languages.
To start off with with basics of C, you need to understand the “#include” keyword. This is a needed asset which imports and connects outside resources to your code. The most imported resources would be from the standard library for the language along with other resources to your code. In Objective-C, you’ll normally be adding something called Cocoa.h to each document in your project.
If you’ve used another language like BASIC, this concept might be new. In BASIC, the library/compiler is normally proprietary and built into the IDE(Integrated Development Environment) application. In C and languages based off of it, the compiler is noramlly not a part of the IDE, this means it needs more information on what to do and what code to use. Also, compilers like this are more open and extensible. This can help expand developers’ applications by giving them the ability to use 3rd party frameworks and systems. This is one of the big reason for the include keyword. Besides making the applications more feature rich by allowing outside sources to connect to it, it can also make your code more organized and make your programs smaller.
In an environment like Realbasic, for instance, a application is given a dylib(Dynamic Library). This dylib contains all the functions and resources built into Realbasic, not just the ones you’ve used. This can add up to large file sizes. In C and other languages, the include keyword is not just needed to import the standard set of libraries, but to import only the ones you want, making your applications small.
Another great feature about the #include, is that it allows you to split up your code into multiple files. This allows more people to work on the same project, and it makes it easier to go over a specific asset. Here’s an example line for including a source file, which normally is a header file.
#include <stdio.h>
The first part is “#include”, which tells the preprocessor(a system before the compiler) to include a source file. The next part is “<stdio.h>”. The “<” and “>” tags mean that you want to get the source file from a standard system folder, normally one containing files from the standard set of the Language. The “stdio.h” is the source file your looking for. Now this code is not for the actual compiler. This line is for something called the preprocessor. All keywords with the “#” in front of them are for the preprocessor, which gets the code polished up and ready for the compiler.
Lastly with the #include keyword, to access normal source files you’ve made, you should use quotes instead of tags like this:
#include “foo.h”
This means that it will look in the same folder as your code, for the file foo.h.
The next part to know about C are the variables. Variables are an incredibly valuable resource in programming. a variable like explained in the last part is a space that holds data. Here is an example of declaring or making a C variable, with a BASIC version of the code underneath:
int foo;
Dim foo As Integer
alternative:
int foo = 100;
dim foo As Integer = 100
alternative:
int foo, foo2;
dim foo, foo2 As integer
The next important part to learn about before getting into Objective-C, is the conditional statements. A conditional really helps make a program non-linear and more feature rich. Just about every program out in the modern world uses many of these conditional statements. Here’s an english readable version of one:
If a person presses the computer’s power button, then the computer will turn on.
Now above, a computer won’t understand what that means. We have to translate it into a more simplistic form like this:
if(person.presses == computer.powerButtonPressed) {
computer.turnOn;
}
Above, I wrote out the same sentence in C. At this point ignore the parentheses and curly brackets. The first part is “if” which is a keyword that starts the conditional statement. The next part is “person.presses”. I made the person into an object, and presses into a method. The method returns a boolean(true or false) value. Next I added a “==” which means I want to see if the right side of the statement equals the left. After that, I add “computer.powerButtonPressed”, in which computer is an object and powerButtonPressed is a method returning a boolean value. “Computer.turnOn” is the results of the conditional statement.
For beginners you might want to look over the last paragraph a few times before continuing.
Now about those brackets and parentheses. In a language like C, these are normally used as containers. These containers hold code with in them, away from the global code. A real world example would be: in a house, a refrigerator is a container that holds food. Now in the conditional statement above, the parentheses are used as a container that hold the statement. The curly brackets are used as a container that holds the results.
In C, “=” and “==” are to different operators. “=” means you want a variable or another asset on the left side to contain the value of the asset on the right side. The “==” means you want to know if the value on the left equals the value on the right:
int var1;
var1 = 5;
this makes var1 have the value 5.
if(var1 == 5){
do.this;
}
this checks to see if var1 does equal 5.
Objects and Classes:
The Language C, is not an object oriented language. It is a procedure language, which means it uses steps to reach it’s desired output or state. In Object oriented languages like Objective-C you use objects and methods to create a network or structure that forms your application. Another concept you need to learn about are classes. When you create your own custom object, your making what is known as a class. A class holds the information needed to create the objects and is more like the factory than the product(in a sense).
A class tells the compiler what the object is and what methods and other assets it has. When you build a class in some languages it looks like this:
function Class1{
this.method1 = function{alert(“hello world”);};
this.var1;
}
Above is an example of a class from Javascript. In many languages there is something called a function. A function separates code like a container from the global code, it’s useful because the container can be called from anywhere. This can save you from writing down the same code more than once since it’s already in the function. Many languages were created before or away from the idea of the object oriented concept. To adapt, many languages just expanded on the functions to create class interfaces. To make the above function class into an actually object also called an instance of the class, you do this:
var object1 = new Class1;
In Objective-C, normally a class takes up two files, the header file and what is called the implementation file. I’ll get into those later. here’s how to create an Objective-C object:
NSImage * image1 = [[NSImage alloc] init];
Understand Some Objective-C Concepts:
In Depth:
Objective-C is based off of C, but more specifically it’s an extension to Ansi C. “Ansi”, just means that the C code used is the standard set by the American National Standards Institute(ANSI). To be even more specific, Objective-C is based off of a language created at Xerox PARC known as Smalltalk.
Smalltalk, released in 1980, was the first language called object oriented. Smalltalk and Objective-C are what is considered pure object oriented. Unlike C++ or Java, in a pure OOP primitive values(integers,characters,etc) are the same as object values. To add to this, in a pure OOP, primitives like integers are objects themselves. Like for instance, in C++ you’d use a variable like int, in Objective-C you’d use NSInteger. NSInteger isn’t just a variable, it’s an object.
In C, like above, you have the include keyword, written as “#include”. This resource happens to be in Objective-C as well, since Objective-C is on top of C. Now to tell you the truth, you should never use #include in your Objective-C programming. Apple, replaced this concept with something called #import. This has the same properties as include and works almost the same way. The only difference with the import keyword compared to the include, is that it tells the preprocessor to check to see if the code it’s trying to import has already been imported. If you use “#include”, it wont care if it already imported the code, it’ll do it again making your apps bigger than they need to be by having 2 or more of the same exact thing. here’s an Example:
#include <Cocoa/Cocoa.h>
#import <Cocoa/Cocoa.h>
If you plan to use ObjC to make applications in OS X your going to have more than one source file in your program. Each one of these files connects to the Cocoa.h header file. If you use #include you will be compiling Cocoa.h in your application multiple times. This can lead to a large file size, and even a long compiling time.
Objective-C or ObjC uses a different means of building objects and accessing methods and variables than most languages including C. You can however use C functions and methods with ObjC with no problem, like this:
NSLog(@”this is an Objective-C String”);
NSLog although looks like an ObjC function of some sort, is actually C. The part in the parentheses is ObjC. The “@” tells the compiler that this happens to be an ObjC String Object also called an NSString.
Like I said in the previous part of the guide, ObjC uses what’s called messages to communicate to objects. Here’s two simple example:
[Object method:argument];
[Object method:[Class method]];
The first might be easy to understand. You first have the Object which you want to talk to, then you have the method. The last part is the arguments that the message is sending to the object’s method.
The example under the first one, might look a little harder. It’s exactly the same until you reach the argument part. What you have is “[Class message]”,
which is a simple and common way to get an action done by a method of a class with out making the class an object. I could have done this:
Class * Object2 = [[Class alloc] init]; //I’ll get into “alloc” and “init” later
[Object message:[Object2 message]];
above is the same as:
[Object message:[Class message]];
The great power of ObjC is the ability to use a class with out creating an instance of it. For example, to load an image to be used by an item in the tool bar, you would do this:
[tbOpenFile setImage:[NSImage ImageNamed:@”OpenFile.png”]];
This is a real world message used in many programs to add a graphical image to a tool bar item. The first part is “tbOpenFile”, this is the tool bar item you are accessing. The next part is the method: “setImage”, this will set the image. After that you add to the setImage argument: “[NSImage ImageNamed:@”OpenFile.png”]”, which is a sub message inside a parent message. This sub message uses a class called NSImage and it’s method, ImageNamed, to locate the file OpenFile.png in the Resources folder. In all OS X applications, there is a folder called Resources which holds all the assets for the program.
Next Part:
Finally, we’ll get into the IDE called Xcode, provided by Apple. I’ll show you how to build a basic web browser for the Cocoa application environment; it’s easier than you might think.