Introduction

In this tutorial we cover the following topics

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


[fulmanp type=”info”]
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.
[/fulmanp]

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.

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, contro 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++

peter->give("flowers", "Hanna")

Objective-C uses

[peter give:@"flowers" to:@"Hanna"]

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.
    001_xcode_in_finder
  2. On welcome dialog select Create a new Xcode project.
    002_xcode_welcome
  3. Select application type.
    003_xcode_select_application_type
  4. Provide necessary information to set up project.
    004_xcode_set_up_project
  5. Select a location for source code.
    005_xcode_selct_source_code_location
  6. Check what was autogenerated for you.
    006_xcode_autogenerated_code
  7. Run and check the results.
    007_xcode_after_first_run

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
[fulmanp type=”terminal”]
/Users/Username/Library/Developer/Xcode/DerivedData
[/fulmanp]
and then dig into folder structure to find your poject. An executable location for one of my projects was as follow
[fulmanp type=”terminal”]
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$
[/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.
    001_source_control_check_history
  2. There should be only one “item” calleed commit: an initial commit.
    002_source_control_history_after_init_commit
  3. Make some changes in your file (add a comment in my case).
    003_source_control_add_some_comment
  4. Commit (confirm) all changes.
    004_source_control_commit_changes
  5. Decide what you realy want to commit.
    005_source_control_commit_window_compare_versions
  6. Add description of your commit.
    006_source_control_commit_window_add_comment
  7. When commit is done, verify history of your project (Source Control | History). There shoud be two commits.
    007_source_control_check_project_history
  8. Select last commit from history.
    008_source_control_select_last_commit_from_history
  9. Verify changes accepted in last commit.
    009_source_control_compare_last_commit
  10. Make some new changes (new comment in my case).
    010_source_control_add_new_comment
  11. Discard all the changes made since last commit.
    011_source_control_discard_all_changes_in_local_version
  12. Confirm that you know what you are doing.
    012_source_control_discard_confirmation
  13. Verify that changes are retrived.
    013_source_control_verify_that_changes_are_reverted
  14. Select View | Version Editor | Show Comparision View from main menu to compare any two version of your files.
    014_source_control_version_editor
  15. You should see something similar to the image presented below
    015_source_control_inside_version_editor
  16. Let’s change version you want to compare from last commit…
    016_source_control_change_file_to_diff_01
  17. …to previous one.
    016_source_control_change_file_to_diff_02
  18. Result of comparision.
    017_source_control_diff_view
  19. Discard some changes.
    018_source_control_discard_some_changes
  20. Confirm that you know what you are doing.
    019_source_control_discard_some_changes_confirmation
  21. Status of your file after revert (changes discard).
    020__source_control_changes_are_discarded
  22. Switch to standard editor (View | Standard Editor | Showw Standard Editor).
    021_source_control_switch_to_standard_editor
  23. Verify with source control history that the first commit is untouched.
    022_source_control_verify_in_source_control_that_1_commit_is_unchanged
  24. Verify with source control history that also the second commit is untouched.
    023_source_control_verify_in_source_control_that_2_commit_is_unchanged

Leave a Reply

Your email address will not be published. Required fields are marked *