Objective c introduction (pt 1)


Published on

Introduction to Objective-C (pt 1)

Covers Overview, OOP in general, Objective-C components, anatomy of Objective-C source files

Published in: Technology

Objective c introduction (pt 1)

  1. 1. Objective-C: An Introduction (pt 1) Tennessee Valley Apple Developers Saturday CodeJam July 24, 2010 – August 7, 2010
  2. 2. What is Objective-C? Objective-C is an object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Objective-C is the primary language used by Apple’s Cocoa and Cocoa Touch APIs
  3. 3. What is Objective-C? Objective-C is a superset of ANSI C Objective-C syntax is a superset of GNU C/C++, and the Objective-C compiler will compile C, C++ and Objective-C source code
  4. 4. What is “object-oriented” programming? Object-oriented programming is a programming style that uses “objects” -- data structures consisting of data fields and methods and their interactions -- to design computer applications
  5. 5. Why use “object-oriented” programming (OOP)? Emphasis is on data rather than procedures Complex programs can be broken down into smaller, less complex components that are easier to manage and maintain It improves application consistency and (potentially) eliminates redundant code It promotes code reusability
  6. 6. Concepts of OOP Objects Classes Inheritance Abstraction Encapsulation Polymorphism
  7. 7. What is an “object”? An object is a bundled set of attributes (data fields), data, and behaviors (functions and procedures, or methods), often related conceptually to something “real-world” In object-oriented programming, a bundled set of “objects” that interact with each other make up a computer application or program
  8. 8. What is an “object”? In OOP, Objects are created from structural definitions called classes. The process of creating a new object is called instantiating the class. The procedures that can use or affect the object’s data are called methods. The object’s data are called its instance variables
  9. 9. What is a “class”? A Class is a prototype definition for a particular kind of object; it’s the “blueprint” you use when you instantiate an object The class defines the instance variables that become part of every member of the class (its objects), and the methods that all objects in that class can use
  10. 10. What is a “class”? Every class defined in an object-oriented program is either a base class (what Objective-C calls a “super class” or a “root class”), or a subclass (or derived class) of an existing class In Objective-C, the base class of (almost)all other Objective-C classes is NSObject; with a few exceptions, all other Objective-C classes ultimately inherit from NSObject
  11. 11. What is “inheritance”? Inheritance is the process by which a newly defined class takes on the behaviors (instance variables and methods) of another class In Objective-C, (almost) all classes have common behaviors originating with NSObject
  12. 12. What is “abstraction”? In general, abstraction is the process of taking away or removing characteristics from something to reduce it to a set of essential characteristics In OOP, Abstraction is the process of picking out common features of objects and procedures
  13. 13. What is “encapsulation”? In general, encapsulation is the inclusion of one thing within another so that the included thing is not readily apparent In OOP, encapsulation is the inclusion within an object all of the resources needed by that object to function (data and methods)
  14. 14. What is “polymorphism”? Generally, polymorphism is the ability to appear in many forms In OOP, Polymorphism is the ability to process objects differently depending on their data type or class, using a common interface
  15. 15. One more Objective-C thing: Protocols Objective-C uses a singular inheritance process, meaning that a subclass can only have one parent class. The mechanism that Objective-C introduces that allows for a type of multiple inheritance is through the use of Protocols
  16. 16. What is a “protocol”? In Objective-C, a protocol is a list of methods that can be implemented by any Objective-C class Protocols usually consist of one or more required method declarations, and may have additional method declarations that are designated as optional
  17. 17. What is a “protocol”? If a class implements a particular protocol, that class must implement all the required methods listed within the protocol; the mechanics of the methods may differ from class to class, but the required methods must exist in all classes implementing that protocol The use of protocols is not required by an Objective- C class; however protocols are used to provide consistency and conformity among Objective-C classes
  18. 18. Objective-C Program Components Objective-C source code utilizes two main types of files: Interface files and implementation files. Interface files are also referred to as “header” files, and have an .h extension Implementation files contain the actual source code, and have either an .m or .mm extension
  19. 19. What are “header” files? Header files contain the class, type, function, and constant declarations for your source code Header files are the “public” portion of a component’s code that can be seen by other program components
  20. 20. Anatomy of an Objective-C interface (header) file # precompiler directives... @interface nameOfClass : nameOfParentClass { attribute information ... } list of messages class will respond to .... @end
  21. 21. What are “implementation” files? Implementation files contain the actual code instructions (the “source code”) of a program component -- the “nuts and bolts” of the code Implementation files are the “private” portion of a component’s code
  22. 22. Anatomy of an Objective-C implementation file #interface file imports... @implementation nameOfClass method / message definition { ... } ... @end