Irving iOS Jumpstart Meetup - Objective-C Session 1b

48
Objective-C Programming Irving iOS Jumpstart

description

 

Transcript of Irving iOS Jumpstart Meetup - Objective-C Session 1b

Page 1: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Objective-C ProgrammingIrving iOS Jumpstart

Page 2: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Objective-C Programming

January 28, 2014 Part Two

Page 3: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Overview of Today

• Preprocessor

• Pointers

• Objective-C OO

Page 4: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Objective-C Preprocessor

• Preprocessor directives tell the compiler to perform specific actions prior to or during compilation of the objective-c code. A directive does not perform any action in the language itself, but rather only a change in the behavior of the compiler.

Page 5: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Preprocessor Directives

• Sample directives

#import / #include

#define

#ifdef

Page 6: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Preprocessor Directives

• Sample directives

#import

• substitutes content of imported file

#import ViewController.h!

! ! #import <Foundation/Foundation.h>

Page 7: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Preprocessor Directives

• Sample directives

#define

• Define an object or macro for inclusion in objective-c code

#define TaxRate = .0825

Page 8: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Preprocessor Directives

• Sample directives

#ifdef

• Enables conditional inclusion of blocks of objective-c code

#ifdef DEBUG

NSLog (@"We're in debug mode.");

#endif

Page 9: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Preprocessor Directives

• Demonstrate Preprocessor Directive in sample weather app

Page 10: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers

• A pointer references a location in memory

• Obtaining the value stored at that location is known as dereferencing the pointer.

• Pointers have many useful applications

Page 11: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers

• An integer pointer declaration

int shoesize = 11;

int *shoesizeptr = &shoesize;

Page 12: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers

• An integer pointer declaration

int shoesize = 11;

int *shoesizeptr = &shoesize;

• shoesizeptr is a memory address

Page 13: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• * is the indirection operator

Page 14: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• * is the indirection operator

• shoesizeptr now contains a memory address

Page 15: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• * is the indirection operator

• shoesizeptr now contains a memory address

• the address of the value of shoesize

Page 16: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• * is the indirection operator

• shoesizeptr now contains a memory address

• the address of the value of shoesize

• the integer value 11 is stored at the location

Page 17: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• & is the address-of operator

Page 18: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• & is the address-of operator

• "set the pointer to an integer, shoesizeptr, equal to the address (location in memory) of the integer variable shoesize"

Page 19: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• deference the pointer

int newshoesize = *shoesizeptr;

Page 20: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• deference the pointer

int newshoesize = *shoesizeptr;

• now the variable newshoesize is equal to 11

Page 21: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Pointers• An integer pointer declaration

int shoesize = 11; (integer variable)

int *shoesizeptr = &shoesize;

• deference the pointer

int newshoesize = *shoesizeptr;

• now the variable newshoesize is equal to 11

• "assign the contents at location shoesizeptr to the integer variable newshoesize"

Page 22: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Arrays

• An array is a set of ordered data items

• an array is used when the order matters

• arrays are zero indexed

• array index begins at zero, so an array with 3 elements are indexed 0, 1, and 2

Page 23: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming

C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming

languages.

Page 24: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Object-oriented programming (OOP) is a programming paradigm that represents

concepts as "objects" that have data fields (attributes that describe the object) and

associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design

applications and computer programs.

Page 25: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Objective-C OO• Objective-C Classes

• Defining classes

• Initializers

• Instance variables and scope

• Declared properties

• Methods, messaging polymorphism

• Inheritance

Page 26: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

• some OOP terminology

• class

Page 27: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

• some OOP terminology

• class

• encapsulation

Page 28: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts• some OOP terminology

• class

• encapsulation

• inheritance

Page 29: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts• some OOP terminology

• class

• encapsulation

• inheritance

• properties

Page 30: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts• some OOP terminology

• class

• encapsulation

• inheritance

• properties

• methods

Page 31: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

• code reuse is emphasized and enabled via OOP

• objects are self-sufficient reusable units of programming logic

Page 32: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts• code reuse is emphasized and enabled via

OOP

• objects are self-sufficient reusable units of programming logic

• a class is a template for object creation

Page 33: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts• code reuse is emphasized and enabled via

OOP

• objects are self-sufficient reusable units of programming logic

• a class is a template for object creation

• consist of nouns (properties) and verbs (methods)

Page 34: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself

– referred to as class instances, class objects, instance objects or simply objects.

A class defines constituent members that enable its instances to have state and

behavior.

Page 35: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

A class usually represents a noun, such as a person, place or thing, or something

nominalized. For example, a "Car" class would represent the properties and functionality of

cars in general. A single, particular car would be an instance of the "Car" class, an object of

the type "Car"

Page 36: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Defining Classes in Objective-C

• Created via .h file and .m file

• All classes inherit from NSObject or another class

Page 37: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C supports the concept of information hiding and encapsulation.

Encapsulation is the principle of controlling access to class members (instance variables

and instance methods).

The @interface in an objective-c .h file, and the @implementation in an objective-c .m file

serve to separate the interface of a class from its implementation.

Page 38: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C supports the concept of inheritance - allowing sub-classes to inherit

interfaces from the classes that they are derived from.

Inheritance enables a subclass to 'inherit' the properties, methods (hence functionality) of

the object inherited from.

Page 39: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

NSObject is the base object in Objective-C providing properties and methods necessary

for any objective-C object.

Page 40: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C class properties are data field descriptions (or fields, data members, or

attributes). These are usually field types and names that will be associated with state

variables at program run time.

The structure defined by the class determines the layout of the memory used by its

instances.

Page 41: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Properties are accessed (read and set) via setters and getters. This enables controlled access to class instance data, a key tenet of

OOP encapsulation

In objective-c, properties (and their getters and setters) are defined via @property and

@synthesize directives.

Page 42: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C class methods define the behavior of a class or its instances. Methods

are subroutines with the ability to alter the state of an object or simply provide ways of

accessing it.

Page 43: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C methods take the following form:

+ or - (return_type)methodName:(data_type)parameter1

Page 44: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

Objective-C methods have two forms; class methods and instance methods.

Page 45: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

• Objective-C class method definitions begin with a plus (+)

+ (id)alloc;

Page 46: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Object Oriented Programming Concepts

• Objective-C class method definitions begin with a plus (+)

+ (id)alloc;

• Objective-C instance method definitions begin with a minus (-)

- (void)printTemperature;

Page 47: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Defining Classes in Objective-C

• Exercise

• Create RentalCar class

• Add methods to Car class

Page 48: Irving iOS Jumpstart Meetup - Objective-C Session 1b

Credits

• Wikipedia

• Programming in Objective-C by Stephen Kochan

• Programming in C by Stephen Kochan

(any errors or omissions are probably mine)