Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt...

22
Objective-C: An Introduction (pt 1) Tennessee Valley Apple Developers Saturday CodeJam July 24, 2010 – August 7, 2010 Sunday, August 29, 2010

Transcript of Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt...

Page 1: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

Objective-C: An Introduction (pt 1)

Tennessee Valley Apple DevelopersSaturday CodeJam

July 24, 2010 – August 7, 2010

Sunday, August 29, 2010

Page 2: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 3: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 4: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 5: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 6: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

Concepts of OOPObjects

Classes

Inheritance

Abstraction

Encapsulation

Polymorphism

Sunday, August 29, 2010

Page 7: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 8: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 9: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 10: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 11: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 12: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 13: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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)

Sunday, August 29, 2010

Page 14: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 15: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 16: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 17: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 18: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 19: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 20: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

Anatomy of an Objective-C interface (header) file

# precompiler directives...

@interface nameOfClass : nameOfParentClass {

attribute information

...

}

list of messages class will respond to

....

@end

Sunday, August 29, 2010

Page 21: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

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

Sunday, August 29, 2010

Page 22: Objective-C: An Introduction (pt 1) - Meetupfiles.meetup.com/1643403/Objective-C_Introduction (pt 1).pdf · C/C++, and the Objective-C compiler will compile C, C++ and Objective-C

Anatomy of an Objective-C implementation file

#interface file imports...

@implementation nameOfClass

method / message definition {

...

}

...

@end

Sunday, August 29, 2010