Irving iOS Jumpstart Meetup - Objective-C Session 1b
-
Upload
irving-ios-jumpstart -
Category
Technology
-
view
257 -
download
0
description
Transcript of Irving iOS Jumpstart Meetup - Objective-C Session 1b
Objective-C ProgrammingIrving iOS Jumpstart
Objective-C Programming
January 28, 2014 Part Two
Overview of Today
• Preprocessor
• Pointers
• Objective-C OO
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.
Preprocessor Directives
• Sample directives
#import / #include
#define
#ifdef
Preprocessor Directives
• Sample directives
#import
• substitutes content of imported file
#import ViewController.h!
! ! #import <Foundation/Foundation.h>
Preprocessor Directives
• Sample directives
#define
• Define an object or macro for inclusion in objective-c code
#define TaxRate = .0825
Preprocessor Directives
• Sample directives
#ifdef
• Enables conditional inclusion of blocks of objective-c code
#ifdef DEBUG
NSLog (@"We're in debug mode.");
#endif
Preprocessor Directives
• Demonstrate Preprocessor Directive in sample weather app
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
Pointers
• An integer pointer declaration
int shoesize = 11;
int *shoesizeptr = &shoesize;
Pointers
• An integer pointer declaration
int shoesize = 11;
int *shoesizeptr = &shoesize;
• shoesizeptr is a memory address
Pointers• An integer pointer declaration
int shoesize = 11; (integer variable)
int *shoesizeptr = &shoesize;
• * is the indirection operator
Pointers• An integer pointer declaration
int shoesize = 11; (integer variable)
int *shoesizeptr = &shoesize;
• * is the indirection operator
• shoesizeptr now contains a memory address
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
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
Pointers• An integer pointer declaration
int shoesize = 11; (integer variable)
int *shoesizeptr = &shoesize;
• & is the address-of operator
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"
Pointers• An integer pointer declaration
int shoesize = 11; (integer variable)
int *shoesizeptr = &shoesize;
• deference the pointer
int newshoesize = *shoesizeptr;
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
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"
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
Object Oriented Programming
C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming
languages.
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.
Objective-C OO• Objective-C Classes
• Defining classes
• Initializers
• Instance variables and scope
• Declared properties
• Methods, messaging polymorphism
• Inheritance
Object Oriented Programming Concepts
• some OOP terminology
• class
Object Oriented Programming Concepts
• some OOP terminology
• class
• encapsulation
Object Oriented Programming Concepts• some OOP terminology
• class
• encapsulation
• inheritance
Object Oriented Programming Concepts• some OOP terminology
• class
• encapsulation
• inheritance
• properties
Object Oriented Programming Concepts• some OOP terminology
• class
• encapsulation
• inheritance
• properties
• methods
Object Oriented Programming Concepts
• code reuse is emphasized and enabled via OOP
• objects are self-sufficient reusable units of programming logic
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
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)
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.
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"
Defining Classes in Objective-C
• Created via .h file and .m file
• All classes inherit from NSObject or another class
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.
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.
Object Oriented Programming Concepts
NSObject is the base object in Objective-C providing properties and methods necessary
for any objective-C object.
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.
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.
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.
Object Oriented Programming Concepts
Objective-C methods take the following form:
+ or - (return_type)methodName:(data_type)parameter1
Object Oriented Programming Concepts
Objective-C methods have two forms; class methods and instance methods.
Object Oriented Programming Concepts
• Objective-C class method definitions begin with a plus (+)
+ (id)alloc;
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;
Defining Classes in Objective-C
• Exercise
• Create RentalCar class
• Add methods to Car class
Credits
• Wikipedia
• Programming in Objective-C by Stephen Kochan
• Programming in C by Stephen Kochan
(any errors or omissions are probably mine)