Journal Articles

CVu Journal Vol 16, #4 - Aug 2004 + Programming Topics
Browse in : All > Journals > CVu > 164 (12)
All > Topics > Programming (877)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: An Introduction to Objective-C

Author: Administrator

Date: 03 August 2004 13:16:06 +01:00 or Tue, 03 August 2004 13:16:06 +01:00

Summary: 

This series of articles aims to introduce the Objective-C programming language to readers of C Vu, who are users of C and C++. I will try to show how the language manages to add object-oriented facilities to the low-level features of C in a way that is radically different from that of C++.

Body: 

Stroustrup, the designer of C++, has been at pains to emphasise that C++ is not just C with an object-oriented extension but rather a new multiparadigm language that happens to support objects; indeed, recent developments in the language have been more in the direction of generic types ('templates') rather than object-orientation as traditionally conceived. The Standard Template Library, which is now part of the Standard C++ Library, also shows some influence from the functional programming style. In pursuit of this goal, the author and the later standardisers of the language have had no compunction in 'improving' on C by for instance strengthening the type system and changing the namespace rules, with the result that a standards-conforming C source is not guaranteed to perform as the programmer expected after being passed to a C++ compiler.

The designer of Objective-C Brad J. Cox, had other ideas. His goal was to make available some of the facilities of the high-level Smalltalk-80 programming environment to users of C, so as to enhance productivity without taking away the advantages of efficiency, versatility and portability that such a low-level language can offer. The object-oriented part of Objective-C is entirely orthogonal to its C-based component, so that a standard C source is always a perfectly good Objective-C source. Cox's problem, then, was how to extend C in such a way so as to enable powerful and easy object-oriented programming without impinging on the original syntax and semantics of the language. The solution he found is to my mind extraordinarily elegant, though it does involve syntactical conventions that seem quite strange to someone without a Smalltalk background; as a result, it only takes an hour or two's study and practice for a C programmer to learn enough to become proficient at Objective-C.

In his book, Object-Oriented Programming, an Evolutionary Approach, Cox addressed what he calls 'the software crisis', namely the tendency of large IT projects to be delivered late, to end up costing much more than their allocated budget, not deliver the intended functionality, and indeed, to fail entirely. Software development is contrasted unfavourably with electronic engineering: software developers keep writing the code over and over again with minor variations to suit the demands of each individual system, while their hardware counterparts are able to create new devices largely by slotting in and connecting components that they have acquired from third parties and are thus much more productive. If development could be largely reduced to putting together systems from software ICs with minimal wiring in between, this would go a long way to solving the software crisis, Cox argued. At the time, he knew of only one development system which provided such components at the IC level, and that was Smalltalk, but the use of Smalltalk was limited by the fact that it required its own 'box' or run-time environment to work, and being interpreted rather than compiled, its performance was unspectacular on the computers of the day. Much could be gained if one could program in C and yet make use of libraries that are available to the Smalltalk programmer.

Cox's company, the Stepstone Corporation, brought out an Objective-C compiler in 1983 with a Smalltalk-like library called ICPak(R). ICPak101provided foundation classes, including Object, the root object, and data-structure classes like Sets, Dictionaries, Arrays, Lists and Strings, while ICPak201 had classes to build cross-platform graphical user interfaces; the coroutine library TaskMaster was added later. Richard Stallman and others developed open-source equivalents of the compiler, run-time system and basic class library for the GNU project, starting in 1992. However, arguably the most signigicant event in the history of the language was its adoption in 1988 by NeXT Computer Corporation as the development language of choice for their UNIX-based NexTStep operating system running on their proprietary workstations. They made some extensions to the language, such as Categories and Protocols, and rather then use Stepstone's ICPak libraries, they developed their own, called Foundation and AppKit. NeXT's workstations, though they drew widespread admiration, did not sell well, and so their development environment, now called OpenStep, was ported to other architectures, as NeXT transformed itself into a software company. OpenStep became popular with financial institutions, scientists, and, so I am assured, US intelligence agencies because it could be used to produce powerful applications with a graphical front end in a remarkably short time; one of these applications was the first World-Wide Web browser, written by Sir Tim Berners-Lee at CERN in 1992. Unfortunately, support for multi-platform OpenStep came to an end after NeXT was acquired by Apple in 1996, but the concept still lives in the Cocoa development frameworks on Apple's Mac OS. The open-source GNUstep project provides libraries and tools to do OpenStep-style development on non-Mac OS platforms.

Objective-C's fortunes have been very different from those of C and C++, and, unlike them, it has never become a mainstream development tool. It is beyond the scope of these articles to speculate as to why this might have been so. (I do not believe Dr Cox minds too much, as his idea of exploiting reusable software components has been abundantly vindicated, from Visual Basic custom controls to Python libraries; he has retained his enthusiasm for Smalltalk-style languages and is now a member of the Ruby community.) The closest thing to a standard is Apple's compiler and frameworks, since the vast majority of Objective-C users work either with these or with GNUstep. Subsequent articles will discuss Objective-C as implemented by NeXT/Apple, and the code examples will illustrate the use of the Foundation framework. I have not tested the code on GNUstep but have reason to expect that it will work there with little or no modification.

Availability of Objective-C Resources

Apple's development tools, including their compiler[1], can be downloaded free of charge from their site. As far as I am aware, the original Objective-C compiler and ICPak libraries produced by the Stepstone Corporation are no longer commercially available. The open-source GNU Compiler Collection (GCC) version 3.4 offers a compiler with facilities very similar to Apple's. The Portable Object Compiler (POC) is in reality an Objective-C front-end to various C compilers and is available free from its author, Mr David Stes. Metrowerks CodeWarrior development tools for Mac OS will interoperate with Apple's Interface Builder GUI construction tool and include an Objective-C compiler that will produce code that links with Apple's frameworks. IBM's Visual Age C/C++ Advanced Edition for Mac OS X also has Objective-C support.

D A Thomas

Bibliography and References

Anguish, Scott, Erik Buck and Donald Yacktman, Cocoa Programming, SAMS, 2002

Apple Computer Inc., The Objective-C Programming Language, available online at http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/

Apple Developer Connection http://developer.apple.com/

Beam, Michael and James Duncan Davidson, Cocoa in a Nutshell, O'Reilly, 2003

Budd, Timothy, Introduction to Object-Oriented Programming, 3rd ed., Addison-Wesley, 2001

Cox, Brad J. and Andrew J. Novibilski, Object-Oriented Programming, an Evolutionary Approach, 2nd ed., Addison-Wesley, 1991

Cox, Brad, Planning the Software Revolution, IEEE Software Magazine, November 1990, available online at http://www.virtualschool.edu/cox/pub/PSIR

Cox, Brad, TaskMaster available online at http://www.virtualschool.edu/cox/pub/TaskMaster/

Davidson, James Duncan and Apple Computer Inc., Learning Cocoa with Objective-C, 2nd ed., O'Reilly, 2002

Duncan, Andrew M., Objective-C Pocket Reference, O'Reilly, 2002

GNU Compiler Collection http://gcc.gnu.org

Hillegass, Aaron, Cocoa Programming for Mac OS X, 2nd ed., Addison-Wesley, 2004

Mahoney, Michael and Simson Garfinkel, Building Cocoa Applications: A Step-by-Step Guide, O'Reilly, 2003

Pinson, Lewis J. and Richard S. Wiener, Objective-C: Object-Oriented Programming Techniques , Addison-Wesley, 1991



[1] This compiler is a GCC derivative and also supports a dialect called Objective-C++, which allows mixing of C++ and Objective-C source; this is meant to facilitate the porting of legacy applications written in C++ to Cocoa.

Notes: 

More fields may be available via dynamicdata ..