Objective C Basics

33
Objective C Basics

description

Objective C Basics. It’s C with Some Extras!. Cross Platform language Mac Linux/UNIX Windows Layer above C (Superset) Adds Object-Oriented features to C C works in Objective-C programs. Sample C Program. - PowerPoint PPT Presentation

Transcript of Objective C Basics

Page 1: Objective C Basics

Objective C Basics

Page 2: Objective C Basics

It’s C with Some Extras! Cross Platform language

Mac Linux/UNIX Windows

Layer above C (Superset) Adds Object-Oriented features to C C works in Objective-C programs

Page 3: Objective C Basics

Sample C Program

Code sample from Objective-C: Visual QuickStart Guide. Page 12. (copied 12/19/2010 from Safari Online)

Page 4: Objective C Basics

Getting Started with Obj-C Native language used for:

Mac development iPhone/iPod touch development iPad development

Download SDK (XCODE) for development on Mac

Page 5: Objective C Basics

What is OOP? Object Oriented Programming!

History Structured programming Procedural programming Object Oriented programming

Page 6: Objective C Basics

Why Object-Oriented? Be able to break up code into objects Self-contained, with details not visible

Page 7: Objective C Basics

OOP Terminology Class—defines an object and what that object

can do. Code in your OOP program. Usually consists of an interface (header, .h) file and an implementation (.m) file.

SubClass—inherits properties and actions of it’s parent class.

Superclass—the parent class Object or Instance—an active class that you

are currently using in your code. Classes define what the properties and capabilities of an object are, whereas an object or instance of that class is what we actively use in the program.From Sam’s Teach Yourself iPhone Application

Development in 24 hours

Page 8: Objective C Basics

OOP Terminology, continued Instantiation—creating an object or instance Instance method—an action/method that is

implemented IN a class for an instance. Class method—an action/method that is

applied to the class and applicable to all objects created from that class.

Message—when you use a method. Give the name of the instance followed by the name of the method.

Instance variable—storage holder to hold information specific to a class and is available inside of an object.

Variable—storage holder, only available in the method where defined.

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 9: Objective C Basics

OOP Terminology, continued Parameter: info that is sent when you send a

message Property: an instance variable (used @property and

@synthesize) that is easy to access in your code.--------------------------------------------------------------- Directive: commands that helps Xcode, Interface

Builder and other development tools. Directive does not implement program logic…it tells Xcode (etc..) how your program is structured.

Protocols—methods grouped together under a common name. May be adopted (or implement if Java background) by a class. The class is contracted to implement the protocols methods. Some of the protocol methods may be optional.

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 10: Objective C Basics

Objective-C Characteristics and Symbols Written differently from other languages Object communicate with messages—does

not “call” a method. @ indicates a compiler directive. Objective-C

has own preprocessor that processes @ directives.

# indicates a preprocessor directive. Processes any # before it compiles.

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 11: Objective C Basics

Common Compiler Directives @ interface—used to describe instance

variables for your class (in header file) @property –simplifies interaction with

interface variables. Automates the getters and setters for the instance variables. (in header file).

@end—shows the end @implementation—shows the compiler which

class you are using, followed by the code you are implementing. (in implementation file)

@synthesize—second part of @property, that is located in the implementation file.

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 12: Objective C Basics

Common Preprocessor Directives #import <file.h> tells the program to replace

statement with the file.h contents. Used instead of # include. Will only add a header file once.

#define –preprocessor macro. Used to define constants.

#pragma mark—used to create sections in your code. Do not add any features. Will show up in the symbol menu (like bookmarking).

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 13: Objective C Basics

Parts of a Method in a Class Methods are executed on behalf on an

instance from the class. Method receives a pointer to the object instance.

Methods may directly access the object’s instance variables

Syntax for a method is different than for a function

From Learning Objective-C 2.0: A Hand’s on Guide for Mac and iOS Programmers

Page 14: Objective C Basics

Declare in Header file (.h)Each method will start with either a – or a +

symbol.- indicates an instance method (the receiver is an instance)+indicates a class method (the receiver is a class name)

Example of a instance method-(IBAction)buttonPressed:(id)sender;Or with one argument-(void)setFillColor:(NSColor*) newFillColor;

Page 15: Objective C Basics

Parts of a Method in a Class Implement the method in the .m file Example:-(IBAction)buttonPressed:(id)sender{ do code here….} Example 2:-(void) setOutlineColor:(NSColor*) outlineColor{

do code here….}

Page 16: Objective C Basics

Messaging To Send a Message:

Format is [receiver message] instead of doing a call. Benefit is that the receiver and method are not bound together

at compile time…you only send the method name. When executes, the receiver objects executes the version of the

method that is part of it’s class. C++ determines method calls at compile time, Objective-C

sends the message at runtime (execution time) Format for a message with a parameter is [receiver

methodName: parameterValue] Format for a message with multiple parameters is

[reciever methodName: parameterValue nextParameterName: parameterValue]

Example of multiple parameter message:[userName compare:@”John” options:NSCaseInsensitive];

Page 17: Objective C Basics

Simple Objective-C Program for iOS Open Xcode Start a new Project by selecting Template

Name project. Add header code (viewController)

Save Add implementation code (viewController)

Save Double-click .xib file in resources for ViewController

Add objects in Interface Builder Link outlets and actions Save and Exit Interface Builder

Build and Run (check for error messages)

Page 18: Objective C Basics

Project Folder and Opening Existing Project

Page 19: Objective C Basics

ViewController.h

Page 20: Objective C Basics
Page 21: Objective C Basics

ViewController.m (top)

Page 22: Objective C Basics

ViewController.m (lower)

Page 23: Objective C Basics

Formatting All statements end in a ; Statements can go over multiple lines White space and indenting are ignored

Page 24: Objective C Basics

Comments Use // for a single line Use /* to begin a comment, and */ to end a

multiline comment

Page 25: Objective C Basics

Naming Conventions Name of variable or function is case sensitive May not include special characters May not have spaces Must begin with either an _ or a letter. No

numbers

Page 26: Objective C Basics

More about Files and Naming _ in front of a variable name generally

indicates a private (or internal) variable Objective-C often uses camelCase naming.

This has the first word as lowercase and runs the second word into the first and capitalizes the second word

If mix C++ and Objective-C use a .mm file extension

Page 27: Objective C Basics

Variables and Instance Variables Variables holds values Variables must be declared and assigned a

data type (so the compiler knows how much memory the value takes up and can assign the appropriate amount)

Example of instance variable:NSString *myString;

Page 28: Objective C Basics

Categories of Data Types

1. Primitive Data Types1. Not an object2. Stores value directly in variableExample:int userAgeInDays;userAge=30;userAgeInDays = userAge * 365

2. Object Data Types1. Created from a class2. Can not store value directly3. Variable with this data type points to the location of the

value, it does not directly contain the valueExample:NSString *userName;

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 29: Objective C Basics

Primitive Data Types

From Objective-C for Absolute Beginners (pg 50)

Page 30: Objective C Basics

Other File ExtensionsExtension Indicates

.c C file

.m Obj C implementation file

.h Obj C header file

.mm C++ implementation file

.xib Interface Builder

.png Image file

.plist Data property list file

Page 31: Objective C Basics

Pointers Objective-C uses pointers to indicate where

objects are stored in memory. So when we create a variable that is going to

hold Class Data Type, we use * to indicate that we are pointing to the value.

* is used when the variable is declared, but does not have to be added when you use the variable after the declaration.

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 32: Objective C Basics

Pointers, Allocating and Initializing After a variable has been declared as a

pointer to an object, it does not yet exist. You just told Xcode that you intend the variable to point to that object’s location

You must allocate memory for the object. After allocating you must initialize the

contents of the object.Example:UILabel *myLabel;myLabel = [[UILabel alloc] init];

From Sam’s Teach Yourself iPhone Application Development in 24 hours

Page 33: Objective C Basics

Convenience Methods After initialize, need to make useful Special initialization method that setup an

object with a set of properties (so you do not have to do each one yourself)

Example: initWithString: