mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-26 18:21:04 +00:00
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@12373 72102866-910b-0410-8b05-ffd578937521
199 lines
13 KiB
Text
199 lines
13 KiB
Text
@paragraphindent 0
|
|
|
|
@node Introduction
|
|
@chapter Introduction
|
|
|
|
The aim of this manual is to introduce you to the Objective-C language and the GNUstep development environment. The manual is organised to give you a tutorial introduction to the language, by using examples whenever possible, rather than providing a lengthy description of the language.
|
|
|
|
While Objective-C is not a difficult language to learn or use, some of the terms may be unfamiliar, especially to those that have not programmed using an object-oriented programming language before. Whenever possible, concepts will be explained in simple terms rather than in more advanced programming terms. So to start I'll give you a brief description of object-oriented programming, the Objective-C language and the GNUstep development environment.
|
|
|
|
The descriptions are not authoritative, and given only as a grounding for the topics that follow.
|
|
|
|
@section What is Object-Oriented Programming?
|
|
@cindex object-oriented programming
|
|
|
|
There are several object-oriented (OO) programming languages in common use today and you have probably heard of some of them: C++ and Java for example, and of course Objective-C. OO languages all have one thing in common: they allow you to design and write programs in a different way than if you used a traditional procedural language like C or Pascal.
|
|
|
|
Procedural languages provide the programmer with basic building blocks that consist of data types, (integers, characters, float etc) and functions that act on that data. This forces the program designer to design the program using these same building blocks. Quite often this requires quite a leap in imagination between what the program must do and how it can be implemented.
|
|
|
|
Object-oriented languages allow the program designer to think in terms of building blocks that are closer to what the program will actually do. Rather than think in terms of data and functions that act on that data, OO languages provide you with objects and the ability to send messages to those objects. Objects are, in a sense, like mini programs that can function on their own when requested by the program or even another object.
|
|
|
|
For example, an object may exist that can draw a rectangle in a window; all you need to do as a programmer is send the appropriate messages to that object. The messages could tell the object the size of the rectangle and position in the window, and of course tell the object to draw itself. Program design and implementation is now reduced to sending messages to the appropriate objects rather than calling functions to manipulate data.
|
|
|
|
@subsection Some Basic OO Terminology
|
|
@cindex basic OO terminology
|
|
|
|
OO languages add to the vocabulary of more traditional programming languages, and it may help if you become familiar with some of the basic terms before jumping in to the language itself.
|
|
|
|
Objects
|
|
|
|
As stated previously, an object is one of the basic building blocks in OO programming. An object can receive messages and then act on these messages to alter the state of itself (the size and position of a rectangle object for example). In software an object consists of instance variables (data) that represent the state of the object, and methods (like C functions) that act on these variables in response to messages.
|
|
|
|
Rather than 'calling' one of its methods, an object is said to 'perform' one of its methods in response to a message. (A method is known as a 'member function' in C++.)
|
|
|
|
Classes
|
|
|
|
All objects of the same type are said to be members of the same class. To continue with the rectangle example, every rectangle could belong to a rectangle class, where the class defines the instance variables and the methods of all rectangles.
|
|
|
|
A class definition by itself does not create an object but instead acts like a template for each object in that class. When an object is created an 'instance' of that class is said to exist. An instance of a class (an object) has the same data structure (instance variables) and methods as every other object in that class.
|
|
|
|
Inheritance
|
|
|
|
When you define a new class you can base it on an existing class. The new class would then 'inherit' the data structure and methods of the class that you based it on. You are then free to add instance variables and methods, or even modify inherited methods, to change the behavior of the new class (how it reacts to messages).
|
|
|
|
The base class is known as the 'superclass' and the new class as the 'subclass' of this superclass. As an example, there could be a superclass called 'shapes' with a data structure and methods to size, position and draw itself, on which you could base the rectangle class.
|
|
|
|
Polymorphism
|
|
|
|
Unlike functions in a procedural program such as C, where every function must have a unique name, a method (or instance variable) in one class can have the same name as that in another class.
|
|
|
|
This means that two objects could respond to the same message in completely different ways, since identically named methods may do completely different things. A draw message sent to a rectangle object would not produce the same shape as a draw message sent to a circle object.
|
|
|
|
Encapsulation
|
|
|
|
An object hides its instance variables and method implementations from other parts of the program. This encapsulation allows the programmer that uses an object to concentrate on what the object does rather than how it is implemented.
|
|
|
|
Also, providing the interface to an object does not change (the methods of an object and how they respond to received messages) then the implementation of an object can be improved without affecting any programs that use it.
|
|
|
|
Dynamic Typing and Binding
|
|
|
|
Due to polymorhism, the method performed in response to a message depends on the class (type) of the receiving object. In an OO program the type, or class, of an object can be determined at run time (dynamic typing) rather than at compile time (static typing).
|
|
|
|
The method performed (what happens as a result of this message) can then be determined during program execution and could, for example, be determined by user action or some other external event. Binding a message to a particular method at run time is known as dynamic binding.
|
|
|
|
@section What is Objective-C?
|
|
@cindex what is Objective-C?
|
|
@cindex Objective-C, what is?
|
|
|
|
Objective-C is a powerful object-oriented (OO) language that extends the procedural language ANSI C with the addition of a few keywords and compiler directives, plus one syntactical addition (for sending messages to objects). This simple extension of ANSI C is made possible by an Objective-C runtime library (libobjc) that is generally transparent to the Objective-C programmer.
|
|
|
|
During compilation of Objective-C source code, OO extensions in the language compile to C function calls to the runtime library. It is the runtime library that makes dynamic typing and binding possible, and that makes Objective-C a true object-oriented language.
|
|
|
|
Since Objective-C extends ANSI C with a few additional language constructs (the compiler directives and syntactical addition), you may freely include C code in your Objective-C programs. In fact an Objective-C program may look familiar to the C programmer since it is constructed using the traditional @code{main} function.
|
|
|
|
@example
|
|
#include <stdio.h>
|
|
#include <objc/objc.h>
|
|
|
|
int main (void)
|
|
@{
|
|
|
|
/* Objective C and C code */
|
|
|
|
return(0);
|
|
@}
|
|
@end example
|
|
|
|
Objective-C source files are compiled using the standard GNU @b{gcc} compiler. The compiler recognises Objective-C source files by the .m file extension, C files by the .c extension and header files by the .h extension.
|
|
|
|
As an example, the command @b{$gcc -o testfile testfile.m -lobjc} would compile the Objective-C source file @code{testfile.m} to an executable named @code{testfile}. The @code{-lobjc} compiler option is required for linking an Objective-C program to the runtime library.
|
|
|
|
The GNUstep @b{make} utility provides an alternative (and simple) way to compile large projects, and this useful utility is discussed in the next section.
|
|
|
|
@section What is GNUstep?
|
|
@cindex what is GNUstep?
|
|
@cindex GNUstep, what is?
|
|
|
|
GNUstep is an object-oriented development environment that provides the Objective-C programmer with a range of utilities and libraries for building large, cross-platform, applications and tools.
|
|
|
|
This manual does not discuss the full functionality of GNUstep but concentrates on using the GNUstep base library to create non-graphical programs, and the GNUstep @b{make} utility to compile these programs. Further information about GNUstep can be found at @url{http://www.gnustep.org}.
|
|
|
|
@subsection GNUstep Base Library
|
|
@cindex GNUstep base library
|
|
|
|
The GNUstep base library contains a powerful set of non-graphical Objective-C classes that can readily be used in your programs. At present there are approximately 70 different classes available, including classes to handle strings and arrays, dates and times, distributed objects, URLs and file systems (to name but a few).
|
|
|
|
Classes in the base library are easily identified since they begin with the upper case characters 'NS', as in @code{NSString}. Some examples in this manual use classes from the base library, but for complete documentation on the base library visit the GNUstep web site at @url{http://www.gnustep.org}.
|
|
|
|
@subsection GNUstep Make Utility
|
|
@cindex GNUstep make utility
|
|
|
|
The GNUstep @b{make} utility is the GNU version of the UNIX make utility. So what does it do? It simplifies the process of building (compiling and linking) a large project. You simply type @b{make} at the command prompt and the make utility takes care of file dependencies, only re-compiling source files that have changed, or that depend on files that have changed, since the last 'make' (a header file for example).
|
|
|
|
Before using @b{make} you must first create a 'makefile' that lists all the files and file dependencies in your project. The easiest way to do this is to copy an existing makefile and change it to suit your own project.
|
|
|
|
The make utility will be used to build the Objective-C examples shown in this manual, and when an example can be compiled then the makefile will also be shown. Further information about makefiles and the make utility can be found in Appendix C.
|
|
|
|
@section Building Your First Objective-C Program
|
|
@cindex your first Objective-C program
|
|
|
|
The following example will show you how to create and compile an Objective-C program. The example simply displays a text message on the screen, and there are easier ways to to do this, but the example does demonstrate a number of object-oriented features of Objective-C, and also demonstrates the use of @b{make} to compile an Objective-C program.
|
|
|
|
@enumerate
|
|
|
|
@item
|
|
Create a new project directory to hold your project.@*@*
|
|
|
|
@item
|
|
Create the following Objective-C source code using your favourite text
|
|
editor and save it in the project directory with the filename @code{source.m}.
|
|
|
|
@example
|
|
#include <stdio.h>
|
|
|
|
/*
|
|
* The next #include line is generally present in all Objective-C
|
|
* source files that use GNUstep. The Foundation.h header file
|
|
* includes all the other standard header files you need.
|
|
*/
|
|
#include <Foundation/Foundation.h>
|
|
|
|
/*
|
|
* Declare the Test class that implements the class method (classStringValue).
|
|
*/
|
|
@@interface Test
|
|
+ (const char *) classStringValue;
|
|
@@end
|
|
|
|
/*
|
|
* Define the Test class and the class method (classStringValue).
|
|
*/
|
|
@@implementation Test
|
|
+ (const char *) classStringValue;
|
|
@{
|
|
return "This is the string value of the Test class";
|
|
@}
|
|
@@end
|
|
|
|
/*
|
|
* The main() function: pass a message to the Test class
|
|
* and print the returned string.
|
|
*/
|
|
int main(void)
|
|
@{
|
|
printf("%s\n", [Test classStringValue]);
|
|
return 0;
|
|
@}
|
|
@end example
|
|
|
|
The text between comment markers (/* */) is ignored by the compiler but indicates to someone reading the source file what each part of the program does. The program is an example of a (class) method responding to a message. Can you see how it works?@*@*
|
|
|
|
A message is sent to the @code{Test} class as an argument to @code{printf()}, requesting the string value of that class. The @code{Test} class performs its @code{classStringValue} method in response to this message and returns a string that is finally printed. No object is created in this program since a class method does not require an instance of a class in order to respond to a message.@*@*
|
|
|
|
You will learn more about class methods in the next chapter.@*@*
|
|
|
|
@item
|
|
Now create the makefile, again using your favourite text editor, and save it in the same project directory with the filename @code{GNUmakefile}.
|
|
|
|
@example
|
|
include $(GNUSTEP_MAKEFILES)/common.make
|
|
|
|
TOOL_NAME = LogTest
|
|
LogTest_OBJC_FILES = source.m
|
|
|
|
include $(GNUSTEP_MAKEFILES)/tool.make
|
|
@end example
|
|
|
|
If you look at the makefile above you will notice the two lines that tell the make utility to build a tool with the filename @code{LogTest} from the Objective-C source file @code{source.m}. You could copy and modify this makefile for later projects you may have: just change the tool name and list the new source files.@*@*
|
|
|
|
The two 'include' lines are just a way of keeping your makefile simple, by including two 'ready-made' makefiles that someone else created.@*@*
|
|
|
|
@item
|
|
You can now compile the project using make. At the system command prompt, change to the project directory and enter the @b{make} command.@*@*
|
|
|
|
@item
|
|
Run the program (for Linux enter @b{./LogTest} at the command prompt). The message "This is the string value of the Test class" will be displayed (assuming there were no errors).
|
|
|
|
@end enumerate
|
|
|
|
You have now compiled and run you first Objective-C program. Hungry for more? Then read on.
|