ITP 342 Mobile App Dev Fundamentals
Object-oriented Programming Object-oriented programming (OOP) is a programming paradigm based on the concept of objects.
Classes A class can have attributes & actions (or members & s) Attributes (Members) Variables holding data Can be varying s Actions (Methods) Behaviors I love alliterations! Think of a as defining an action to be taken 3
Classes and Instances Think of a class as a cookie cutter Instances are the cookies Instance Class Instance 4
Example 5
Classes and Instances In Objective-C, classes and instances are both objects 6
Objective-C Classes Encapsulate data with the s that operate on that data An object is a runtime instance of a class Contains its own in-memory copy of the instance variables declared by that class & pointers to the s of the class Specification requires 2 distinct pieces: interface (.h) = public header implementation (.m) = private implementation 7
The Interface When you define a new class, you have to tell the compiler 3 things: Where the class came from Name its parent class Specify what of data is to be stored Describe the data that members of the class will contain These members are called the instance variables Define the of operations, or s, that can be used 8
Methods A class in Obj-C can declare 2 s of s Instance s A whose execution is scoped to a particular instance of the class Before you call an instance, you must first create an instance of the class Declaration is preceded by a minus (-) sign Class s Do not require you to create an instance Typically factory s for creating new instances of the class or accessing some piece of shared info Declaration is preceded by a plus (+) sign 9
No arguments Declaring Methods - (void) printinfo; return name takes no argument 10
Declaring Methods Return a primitive - (int) getage; return name takes no arguments 11
Declaring Methods Return an object - (NSString *) getname; return name takes no arguments 12
Declaring Methods One argument that is a primitive - (void) setage: (int) num; argument return name takes argument argument name 13
Declaring Methods One argument that is an object - (void) setname: (NSString *) name; argument return name takes argument argument name 14
Two arguments Declaring Methods - (void) setname: (NSString *) name age: (int) num; argument return name takes argument argument name 15
Method Example Method declaration syntax 16
Object Messaging To get an object to do something, you send it a message telling it to apply a Message expressions are enclosed in brackets The receiver is an object and the message tells it what to do Message is the name of a and any arguments The name serves to select a implementation Methods names in messages are often referred to as selectors [receiver message]; [person1 printinfo]; 17
Messaging Methods can also take parameters or arguments A message with a single argument affixes a colon (:) to the selector name & puts the argument right after the colon [person1 setage:21]; For multiple parameters, names should interleave the name with the arguments The s name naturally describes the arguments expected by the The sub-parts of the name - of the selector - are not optional, nor can their order be varied [person1 setname:@"trina" age:21]; 18
Messaging Example Example of calling an instance Call the insertobject by messaging the corresponding object [myarray insertobject:anobj atindex:0]; Example of calling a class arraywithcapacity: is a class on the NSMutableArray class that allocates and initializes a new instance of the class and returns it to your code NSMutableArray* myarray = nil; // Create a new array & assign it to the myarray variable myarray = [NSMutableArray arraywithcapacity:0]; 19
Methods Methods can return values BOOL vote; vote = [person1 canvote]; One message expression can be nested inside another [person1 setage:[person2 age]]; 20
Variables Variables allow us to store information The C programming language is a statically d language We have to tell upfront what kind of data a variable is going to hold Format to declare a variable: name; Examples: int num1; float score; Declare once, use many 21
Variables Specify the s of data to be stored in your class along with the names of those data s Data s can be: C basic s like int, float, char Objective-C defined s like BOOL Other classes that you have created or ones that are already created such as NSString Names Begin with a letter or underscore (_) Followed by any combo of letters (upper- or lowercase), underscores, or the digits 0-9 Cannot be a reserved word like int 22
Local Variables Variables Within a, declare using and name // In an implementation (.m) file, within a - (void) mymethod { // local variables BOOL canlegallyvote; int numofyears; NSString *name; } name = @"Tommy Trojan"; 23
Use CamelCase usecamelcase 24