Intro to Objective-C Syntax: What’s most confusing about Objective-C? Most class names start with NS: NSString, NSObject Parameter lists are not comma.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.
CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
IPhone Development Crash Course By Dylan Harris
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Programming Languages and Paradigms Object-Oriented Programming.
More on Classes Inheritance Copyright © 2012 by Yong-Gu Lee
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Internet Software Development Classes and Inheritance Paul J Krause.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Messages, Instances, and Initialization (Methods) CMPS 2143.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Introduction to Generics
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
More Objective-C Details FA 172 Intro to Mobile App Development.
Lec 5 Obj-C part 2 CS 3800 Introduction to IOS programming Lecture 5 Summer 2011.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Introduction to Object Oriented Programming Chapter 10.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Introduction to Objective-C and Xcode (Part 4) FA 175 Intro to Mobile App Development.
OOP Basics Classes & Methods (c) IDMS/SQL News
MORE on classes in …... OOP terminology in Objective-C Class: defines the grouping of data and code type of entity Object or Instance: a specific instantiation.
Objective-C: Intro Michelle Alexander COMS E6998 2/4/2013.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Reference Types CSCI-1302 Lakshmish Ramaswamy. Reference Variables Java supports 8 primitive types All other types are reference types Reference variable.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointer to an Object Can define a pointer to an object:
Java Memory Management
Java Memory Management
Interface.
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Java Review: Reference Types
Implementing Classes Chapter 3.
References Revisted (Ch 5)
Corresponds with Chapter 5
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Intro to Objective-C Syntax: What’s most confusing about Objective-C? Most class names start with NS: NSString, NSObject Parameter lists are not comma delimited and method names are interrupted by parameter names and types. There are too many brackets and colons. :[ Memory Management. All symbols confuse me. Both C and Objective-C methods are allowed? Weird.

Intro to Objective-C Syntax: Method Signatures In Java or C: void doNothing() { // nothing } int addThree(int x) { return x + 3; } int multiplyThreeParameters(int x, int y, int z) { return x * y * z; } // note methods with multiple parameters are given in a parameter list // that is delimited by commas. Key return type method name parameter type parameter name

Intro to Objective-C Syntax: Method Signatures In Objective-C: - (void) doNothing { // nothing } - (int) addThree:(int) x { return x + 3; } - (int) multiplyThis:(int) x ByThis:(int) y AndThis:(int) z { return x * y * z; } // note methods of Objective-C classes with multiple parameters have a space to // delimit the end of the parameter name and the continuation of the method // name. The actually method name is multiplyThis:ByThis:AndThis: Key return type method name parameter type parameter name

Intro to Objective-C Syntax: Accessing methods of objects In Java: object.method(param1, param2); In C++: object->method(param1, param2); In C: (no objects) method(param1, param2);

Intro to Objective-C Syntax: Accessing methods of objects In Objective-C: [object method:param1 method:param2]; Example: If you have a string: NSString *msg YOUR BASES BELONG TO US"; And you want to split the sentence into an array of words: NSArray *words = [msg "]; // is required for all string literals, and encodes the string using UTF8

Intro to Objective-C Syntax: Instantiation / Memory Allocation In Java: Object o = new Object(); // Java takes care of garbage collection. In this statement, memory // is automatically allocated for the new object. Memory is also // automatically released when the object is no longer in use. In C: Object *o = (Object *) malloc(sizeof(Object)); free (o); In C++: Object *o = new Object; delete (o);

Intro to Objective-C Syntax: Instantiation / Memory Allocation In Objective-C: Object *obj = [[Object alloc] init]; OR Object *obj = [Object new]; [obj release]; OR[obj autorelease]; Golden Rule of Objective-C memory management: When ever you own an object, you must relinquish ownership. I.E. Whenever you call “retain”, “alloc”, “new”, “copy”, or “mutableCopy”, it must be paired with a call to “release” or “autorelease”.

Intro to Objective-C Syntax: Classes In Java, students can define and implement a class in a single.java file. In C++, students define a class and methods in a.h header file and implement the methods in a.c file. In Objective-C, students define a class and its methods in a.h header file and implement the methods in a.m file. Circle.h Circle.m include

Intro to Objective-C Syntax: Circle : NSObject { // instance variables double radius = 1.0; } // Class methods +(double) getPi; // Instance methods -(double) getArea; -(void) setRadius:(double) Circle.h Key class name superclass return type method name parameter type parameter name Optional parameter name

#import Circle +(double) getPi { return ; } -(double) getArea { double pi = [Circle getPi]; return pi * radius * radius; } -(void) setRadius:(double) r { radius = r; Circle.m

#import "Circle.h" // Non Objective-C function; program origin int main() { Circle *mycirc = [[Circle alloc] init]; [mycirc setRadius:3.0]; double area = [mycirc getArea]; double pi = [Circle getPi]; return 0; } Main.m