More Objective-C Details FA 172 Intro to Mobile App Development.

Slides:



Advertisements
Similar presentations
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Advertisements

Written by: Dr. JJ Shepherd
Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Introduction to Objective-C and Xcode (Part 1) FA 175 Intro to Mobile App Development.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
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.
1 Objective-C Foundation Lecture Flow of Lecture 2  Objective-C Basics  Introduction to Objective-C  Intrinsic Variables and Image-Specific.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Objective-C and Xcode (Part 3) FA 175 Intro to Mobile App Development.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
Classes CS 21a: Introduction to Computing I First Semester,
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Managing Multiple Views and Segues FA 172 Intro to Mobile App Development.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Introduction to Java Java Translation Program Structure
Introduction to Objective-C and Xcode (Part 5) FA 175 Intro to Mobile App Development.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Sounds, Images, and Text FA 172 Intro to Mobile App Development.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
OOP with Objective-C Categories, Protocols and Declared Properties.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Lec 5 Obj-C part 2 CS 3800 Introduction to IOS programming Lecture 5 Summer 2011.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Written by: Dr. JJ Shepherd
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Introduction to Objective-C and Xcode (Part 4) FA 175 Intro to Mobile App Development.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Objective-C: Intro Michelle Alexander COMS E6998 2/4/2013.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Lecture 3 John Woodward.
Chapter 7 User-Defined Methods.
Arrays .
Arrays and Array Lists CS 21a.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Defining Classes and Methods
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

More Objective-C Details FA 172 Intro to Mobile App Development

Agenda Statements – Getters and setters – If statements Class methods Arrays

Method calls Recall method call syntax – [ ] – [ : ] Examples – [myCar loadGas:43.21]; – car travelled %6.2 miles", [myCar distanceTravelledInMiles]); – [b1 depositAmt:100];

Properties and method calls Retrieving property values is equivalent to a method call – myCar.gasLeft is equivalent to [myCar gasLeft] – When you declare a property, a getter method is automatically created – -(double) gasLeft { return _gasLeft; }

Properties and method calls Assigning a value to a property, e.g. – theProduct.stockLevel = 100; Is equivalent to – [theProduct setStockLevel:100]; A setter method is also automatically created (for readwrite properties) – -(void) setStockLevel:(int) quantity { _stockLevel = quantity; }

Statements we know so far Declarations – double salePrice; Assignments – discount = salePrice*0.10; Return statements – return computedValue; Function calls – world"); Method calls – [myCar driveDistance:94];

Conditional execution Sometimes, we want a statement executed only when some condition is met if-statement syntax: – if ( ) – if ( ) else

If-statement example In a withdraw method of a BankAccount class… if (self.balance >= amount) self.balance = self.balance - amount; Better yet, if (self.balance >= amount) self.balance = self.balance - amount; else balance");

Blocks If multiple statements need to be conditionally executed, group as a block and enclose with { } if (self.balance >= amount) { self.balance = self.balance - amount; withdrawn”,amount); } else balance"); you."); Note: The last statement is executed unconditionally

Conditions Conditions are a result of calculations that use – Relational operators: > = – Logical operators: && || ! Conditions return values of type BOOL – e.g., 10 > 5 returns a YES, 1 == 2 returns a NO The NSString function has a method for comparing strings – -(BOOL) isEqualToString:(NSString *) otherString; – e.g., [name

Class methods In some situations, a method related to a class need not be called on a specific object, such as when: – The method applies to all objects of a class – The class is viewed as a container or manager of its objects and the method is at the level of the manager Example: alloc is a “built-in” method of any class – [ alloc] returns a newly created/allocated instance/object under that class

NSString The method integerValue is an instance method of NSString that returns an int NSString *s int val = [s integerValue]; The method stringWithFormat is a class method of NSString that returns an NSString* NSString *x; x = [NSString val];

UIImage imageNamed is a class method of UIImage that returns a UIImage object given an image file name self.imageView.image = [UIImage In contrast, size is an instance method (actually, a property) that returns the size of a given image [self.imageView.image size]

Declaring and using class methods Use a + instead of a - when declaring and defining a class method: +(void) setInterestRate:(double)rate; You may not use properties in these methods – Properties represent instance-level data Example: an interestRate variable for a BankAccount class can be set by a class-level setInterestRate method – Declare interestRate in the.m file, outside of the methods

@interface (readwrite) BankAccount -(void) deposit:(double)amt { self.balance += amt; } double interestRate = 0.05; -(void) applyInterest { double amt = self.balance*interestRate; self.balance += amt; } +(void) setInterestRate:(double) rate { interestRate = rate; Declared outside the methods

Arrays Array: collection of variables of the same type Suppose you had three variables (type double) that represent the yearly incomes of three different employees: double income1; double income2; double income3; Use an array instead to declare them “in batch” double income[3];

Using arrays Assignment using the non-array version: income1 = ; income2 = ; income3 = ; Assignment using the array version income[0] = ; income[1] = ; income[2] = ; Syntax: [ ] If n is the size of the array, indices go from 0..n-1

Looping through array elements You may use loops to apply a statement for every array element Non-array version income1 = income1 + income1*0.10; income2 = income2 + income2*0.10; income3 = income3 + income3*0.10; Array version using a loop for(int i = 0; i <3; i++) income[i] = income[i] + income[i]*0.10;

Array of objects To store an array of five strings: NSString *words[5]; words[0] words[1] words[2] words[3] words[4] To print these strings: for( int i = 0; i < 5; i++ ) ", words[i]);

Array classes Arrays just discussed are “C-style” arrays Alternative: create array objects using the Objective-C classes NSArray or NSMutableArray NSArray (fixed-size) – Create and initialize using arrayWithObjects class method NSMutableArray (dynamically-sized) – Create using [[NSMutableArray alloc] init] – Add new elements using addObject method

Array classes To access individual elements – Use objectAtIndex method – Or use [ ] Use the count property to determine the current size of the array Refer to the test code in the course website for a demonstration of using NS array classes

Summary Getter and setter methods – Automatically defined If-statement: conditional execution Class method – Method that is not called on an instance (it may return an instance) – Method that manages objects of the class Array – Collection of variables of the same type – Involves indexing and loops – C-style, NSArray, NSMutableArray