Skip to content

Introduction

In this tutorial we cover the following topics

  • Brief history
    • Ancestor: C -- the roots
    • Ancestor: Smalltalk -- turn into novelty
    • Objective-C borns
    • Apple's main programming language
  • Into the first program
    • Frameworks
    • First program
  • Next step: use standard C as an Objectve-C code
  • Bonus material -- Xcode source control

Brief history

Ancestor: C -- the roots

I don't want to write much about C -- I think that everybody knows that this language exists and knows something about it. Since the early 70th years, when it was developed and used to re-implement the Unix operating system, it has become one of the most widely used programming languages of all time. In short, C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion. It's compact (there are no many sophisticated or hard to remember statements) -- by design, C provides constructs that map efficiently to typical machine instructions. Therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from embedded systems to supercomputers. If you want to stay as close to the hardware as it is possible but don't want or have to learn assembler, C is obvious choice.


Ancestor: Smalltalk -- turn into novelty


There are a large number of Smalltalk variants. The unqualified word Smalltalk is often used to indicate the Smalltalk-80 language, the first version to be made publicly available and created in 1980.

In short, Smalltalk is an object-oriented, dynamically typed, reflective programming language. It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s.


Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.

To get the idea let's consider a few examples from languages you may know. The following code snippets create an instance foo of class Foo, and invoke its method hello. A normal and reflection-based call sequences are shown (examples from Wikipedia: Reflection).

ECMAScript

Java

C#

PHP

Python

Objective-C

As in other object-oriented languages, the central concept in Smalltalk (being more precisely: Smalltalk-80 but not in Smalltalk-72) is that of an object.

So what is an object? Put simply, it is a "thing" which can represents many different... things. From the program’s perspective, this "thing" is a "black box" -- it hides its inner workings; the object performs any task that is asked of it (if only the object has been programmed to actually perform the task, of course). How the object performs its task is irrelevant, as far as the program itself is concerned. Object represents real "things" with all relevant properties and abilities -- actions we can ask this object to do for us and its behaviours.

An object is always an instance of a class. Classes are "blueprints" that describe the properties and behavior of some class of real things. Every time we create an existing instance of this type (of this class), we create an object which holds all properties, actions and behaviours described by its class.

Bellow there are listed pillars of object-oriented development

  • Encapsulation There is no geneal agreement what encapsulation means. Generally speaking it is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
    • A language mechanism for restricting access to some of the object's components.
    • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
  • Inheritance Inheritance is when an object or class is based on another object or class, using the same implementation or specifying implementation to maintain the same behavior. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. Inheritance was invented in 1967 for Simula programming language.
  • Polymorphism Polymorphism is the provision of a single interface to entities of different types.

A Smalltalk object can do exactly three things:

  • Hold state (references to other objects).
  • Receive a message from itself or another object.
  • In the course of processing a message, send messages to itself or another object.

Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate; if "yes" it is served, if "not" it is left without any bad consequences like "runtime error". We can say that despite the objects, messaging is the most important concept in Smalltalk.

Smalltalk is a "pure" object-oriented programming language, meaning that, unlike Java and C++, there is no difference between values which are objects and values which are primitive types. In Smalltalk, primitive values such as integers, booleans and characters are also objects, in the sense that they are instances of corresponding classes, and operations on them are invoked by sending messages. A programmer can change or extend (through subclassing) the classes that implement primitive values, so that new behavior can be defined for their instances -- for example, to implement new control structures -- or even so that their existing behavior will be changed.

Since all values are objects, classes themselves are also objects. Each class is an instance of the metaclass of that class. Metaclasses in turn are also objects, and are all instances of a class called Metaclass.

Another importand properties is that Smalltalk is a totally reflective system, implemented in Smalltalk itself. Smalltalk provides both structural and computational reflection. Smalltalk is a structurally reflective system whose structure is defined by Smalltalk objects. The classes and methods that define the system are themselves objects and fully part of the system that they help define.


Objective-C borns

In previous section we saw two different worlds. In a procedural language, the code is focused around variables, data, control flow and functions. Generaly this type of language helps us to decide how to store data and what to do with the data. On the other side, an object-orientated language focuses on creating objects, which are then used to do certain things, just like objects, or “things”, do in real life. Thus with object we change the way we think about the problem -- it is more natural.

Objective-C was created primarily by Brad Cox and Tom Love in the early 1980s. It merges advantages of C like the purity and low-level control of C with true object-oriented features that would allow reusability in software design and programming. As it is Objective-C is a strict superset of C, unlike C++ which is most of C, but with many differences. Remember about this when you will be working with it.

The greatest criticism of Objective-C is the syntactical styling. Yes, at first look it's a little bit odd -- instead of well known from C++

Objective-C uses

It is just a metter of syntax. Syntax which is strange only because of our habits. When we think about this for a while we should discover that latter is better: more descriptve and less error prone. What may seem like an arbitrary collection of brackets at first will, with time and practice, make sense as a mechanism for organizing code around object-oriented concepts that were added as a thin layer on top of the well known C world.


Apple's main programming language

When Steve Jobs founded NeXT Computer (1985), after he resigned from Apple, he was interested in development of new computer, both hardware and software (mainly operating system for it). NeXTSTEP, an operating system developped for this computer is a combination of several parts:

  • a Unix operating system based on the Mach kernel, plus source code from BSD,
  • display PostScript and a proprietary windowing engine,
  • the Objective-C language and runtime,
  • an object-oriented (OO) application layer, including several "kits",
  • development tools for the OO layers.

As we can see, NeXTSTEP was very strong object oriented ecosystem. After they chose Objective-C to keep speed benefits and system control and gain object profits, NeXT eventually wrote many libraries and methods for the base language. These all begin with NS -- abreviaton from Next Step. By 1989 the NeXTSTEP OS was considered to be vastly superior to MS Windows or Mac OS.

On December 20, 1996 Apple Computer announced an intention to acquire NeXT. The main purpose of the acquisition was to use NeXTSTEP as a foundation to replace the dated Mac OS, instead of BeOS or the in-development Copland. The deal was finalized on February 7, 1997, bringing Jobs back to Apple as a consultant, who was later appointed as interim CEO. In 2000 Jobs took the CEO position as a permanent assignment.

After the purchase of NeXT by Apple, NeXTSTEP became the source of the popular operating systems OS X, iOS, and now watchOS and tvOS. Many bundled OS X apps, such as TextEdit, Mail and Chess, are real life examples of NeXTSTEP's legacy.


Into the first program


Frameworks

In the previous section we have seen how Objective-C became the native programming language for Apple’s iOS and OS X operating systems. It is a compiled, general-purpose language capable of building everything from command line utilities throught animated GUIs to domain-specific libraries. It also provides many tools for maintaining large, scalable frameworks.

Apple's native object-oriented application programming interface (API) for the OS X operating system is named Cocoa. Cocoa continues the lineage of several software frameworks (primarily the App Kit and Foundation Kit) from the NeXTSTEP and OpenStep programming environments developed by NeXT. There is also a similar API called Cocoa Touch designed for applications for the iOS operating system, used on Apple devices such as iPhone, iPad, iPod Touch and Apple TV.

Cocoa consists of three Objective-C object libraries called frameworks. Frameworks are functionally similar to shared libraries, a compiled object that can be dynamically loaded into a program's address space at runtime, but frameworks add associated resources, header files, and documentation. The Cocoa frameworks are implemented as a type of bundle, containing the aforementioned items in standard locations.

  • Foundation Kit (or simply Foundation) is a generic object-oriented library providing string and value manipulation, containers and iteration, distributed computing, run loops, and other functions that are not directly tied to the graphical user interface. The "NS" prefix, used for all classes and constants in the framework, comes from Cocoa's OPENSTEP heritage, which was jointly developed by NeXT and Sun.
  • Application Kit (or AppKit) is directly descended from the original NeXTSTEP Application Kit. It is used to create and interact with graphical user interfaces. AppKit is built on top of Foundation, and uses the same "NS" prefix. For iOS devices we use UIKit.
  • Core Data is the object persistence framework included with Foundation and Cocoa. It provides a convenient API for managing object relationships, supporting undo/redo functionality, and interacting with persistent storage.
  • Quartz which is a part of the Core Graphics framework. It provide a vital part of the Cocoa architecture -- its comprehensive views model.

Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder, using the Objective-C or Swift language.


First program

  1. Use Finder (or any other method you know) to find Xcode.
  2. On welcome dialog select Create a new Xcode project.
  3. Select application type.
  4. Provide necessary information to set up project.
  5. Select a location for source code.
  6. Check what was autogenerated for you.
  7. Run and check the results.

You may notice that Objective-C uses .m extension for a source file. A good explanation of this you can find in Objective-C: What Does The “.m” Extension Stand For?

If you wonder were exectable files of your project are located you should check the following location


/Users/[USER_NAME]/Library/Developer/Xcode/DerivedData

and then dig into folder structure to find your poject. An executable location for one of my projects was as follow

pc18:Debug fulmanp$ pwd
/Users/fulmanp/Library/Developer/Xcode/DerivedData/app_003_blocks_ex11-bfscpjhfqmuceigvftncvvixnauc/Build/Products/Debug
pc18:Debug fulmanp$ ls -l
total 48
-rwxr-xr-x 1 fulmanp staff 20992 4 mar 22:55 app_003_blocks_ex11
pc18:Debug fulmanp$


Next step: use standard C as an Objectve-C code

As we stated few sections before, Objective-C is a strict superset of C. In consequence every C correct code is also correct Objectve-C code. That is why as next step we will study a basic foundation in C before tackling the higher-level aspects of the language. Below there are links to one of the best among other "C in Objective-C" tutorials (I assume that you know C language; if not, you have to find some materials on your own which shouldn't be a problem nowadays)


Bonus material -- Xcode source control

Below there are short screencast. More details you can find for example here

  1. Check history of your file. Select Source Control | History from main menu.
  2. There should be only one "item" calleed commit: an initial commit.
  3. Make some changes in your file (add a comment in my case).
  4. Commit (confirm) all changes.
  5. Decide what you realy want to commit.
  6. Add description of your commit.
  7. When commit is done, verify history of your project (Source Control | History). There shoud be two commits.
  8. Select last commit from history.
  9. Verify changes accepted in last commit.
  10. Make some new changes (new comment in my case).
  11. Discard all the changes made since last commit.
  12. Confirm that you know what you are doing.
  13. Verify that changes are retrived.
  14. Select View | Version Editor | Show Comparision View from main menu to compare any two version of your files.
  15. You should see something similar to the image presented below
  16. Let's change version you want to compare from last commit...
  17. ...to previous one.
  18. Result of comparision.
  19. Discard some changes.
  20. Confirm that you know what you are doing.
  21. Status of your file after revert (changes discard).
  22. Switch to standard editor (View | Standard Editor | Show Standard Editor).
  23. Verify with source control history that the first commit is untouched.
  24. Verify with source control history that also the second commit is untouched.