Objective C Basics. It’s C with Some Extras!  Cross Platform language  Mac  Linux/UNIX  Windows  Layer above C (Superset)  Adds Object-Oriented.

Slides:



Advertisements
Similar presentations
Lecture 2 - Introduction Objective C is used primarily for application development on Apple's Mac OS X and iPhone. On the Apple it is used together with.
Advertisements

Introducing JavaScript
C Language.
Introduction to Java.
XHTML Basics.
Compiler Design PROJECT PRESENTATION : COMPILER FOR OBJECTIVE C Harshal Waghmare Jeet Kumar Nandan Kumar Vishal Agrawal.
The Web Warrior Guide to Web Design Technologies
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 1: Introduction
Introduction to Objective-C and Xcode (Part 1) FA 175 Intro to Mobile App Development.
Introducing ActionScript 3.0 Object-oriented programming language Used to power Flash Player Similar to JavaScript Can be embedded in a Flash project.
Week 2 Recap CSE 115 – Spring Object Oriented Program System of objects that communicate with one another to solve some problem.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
ASP.NET Programming with C# and SQL Server First Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
C++ fundamentals.
Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture Set 2 Part B – Configuring Visual Studio; Configuration Options and The Help System (scan quickly for future reference)
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
The Java Programming Language
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Classes CS 21a: Introduction to Computing I First Semester,
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Computer Programs and Programming Languages What are low-level languages and high-level languages? High-level language Low-level language Machine-dependent.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
+ An Intro To Xcode By Sarah Montroy. + What is Xcode?
View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
OOP with Objective-C Categories, Protocols and Declared Properties.
1 JavaScript in Context. Server-Side Programming.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Bill Tucker Austin Community College COSC 1315
User-Written Functions
Working with Java.
Objectives Identify the built-in data types in C++
CIS16 Application Development Programming with Visual Basic
Units with – James tedder
Focus of the Course Object-Oriented Software Development
C Programming Language
Classes CS 21a: Introduction to Computing I
Review of Previous Lesson
Presentation transcript:

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 Code sample from Objective-C: Visual QuickStart Guide. Page 12. (copied 12/19/2010 from Safari Online)

Getting Started with Obj-C  Native language used for:  Mac development  iPhone/iPod touch development  iPad development  Download SDK (XCODE) for development on Mac

What is OOP?  Object Oriented Programming!  History  Structured programming  Procedural programming  Object Oriented programming

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

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

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

OOP Terminology, continued  Parameter: info that is sent when you send a message  Property: an instance variable 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

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 directives.  # indicates a preprocessor directive. Processes any # before it compiles. From Sam’s Teach Yourself iPhone Application Development in 24 hours

Common Compiler Directives interface—used to describe instance variables for your class (in header file) –simplifies interaction with interface variables. Automates the getters and setters for the instance variables. (in header file). the end the compiler which class you are using, followed by the code you are implementing. (in implementation file) part that is located in the implementation file. From Sam’s Teach Yourself iPhone Application Development in 24 hours

Common Preprocessor Directives  #import 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

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

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;

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…. }

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 options:NSCaseInsensitive];

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)

Project Folder and Opening Existing Project

ViewController.h

ViewController.m (top)

ViewController.m (lower)

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

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

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

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

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;

Categories of Data Types 1. Primitive Data Types 1. Not an object 2. Stores value directly in variable Example: int userAgeInDays; userAge=30; userAgeInDays = userAge * Object Data Types 1. Created from a class 2. Can not store value directly 3. Variable with this data type points to the location of the value, it does not directly contain the value Example: NSString *userName; From Sam’s Teach Yourself iPhone Application Development in 24 hours

Primitive Data Types From Objective-C for Absolute Beginners (pg 50)

Other File Extensions ExtensionIndicates.cC file.mObj C implementation file.hObj C header file.mmC++ implementation file.xibInterface Builder.pngImage file.plistData property list file

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

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

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: