SlideShare a Scribd company logo
More On Variable,
Categories and Protocols
Chhorn Chhaly
Leang Bunrong
Cheng Udam
Kan Vichhai
Srou Chanthorn
Em Vy
Seth Sambo
Chea Socheat
Content
1. More on Variables and Data Types
 Initialize Objects
 Scoped Revisited
 More on properties, Synthesized Accessors, and Instance Variables
 Global Variable
 Static Variable
 Enumerated Data Types
 The typedef statement
 Data Type Convers
• Categories and Protocols
 Categories
 Class Extensions
 Protocols and Delegation
 Composite Object
Initializing Objects
• We initializing object by calling “init” method
• For example Fraction *f=[[Fraction alloc]init];
• The example above we just get an Object that has nothing or field value
• If we want to initialize object with many fields value, we have to create our custom
method to initialize object
Objective-C Java
Initializing Objects
• The method initWithNumerator:(int)n withDenominator:(int)d is the custom method to
initializing object
• All initializing methods must start with word “init…..”
• Self=[super init] means the super class calls “init” method to initialize for current object
• But it just an object without any fields value
• After that line we see that we initialize _numerator and _denomitor
• Then we return “self” that fulfill of object with field values
Variable Scope
In Objective-C, a variable has been declared it may or may not be accessible to other
sections of the program code. This accessibility depends on where and how the variable
was declared and where the code is that needs to access it. This is know as variable
scope.
Local Variables
Variables that are declared inside a function or block are called local variables. They can
be used only by statement that are inside that function or block of code. Local variable
are not known to functions outside their own.
Example:
Objective-C:
-(void) sum:(int) x secondP:(int) y {
int total;
total = x+y;
NSLog(@”Total: %i”, total);
}
NSLog(@”Total: %i”, total);
// illegal Total is now out of scope.
Java:
public void sum(int x, int y){
int total;
total = x+y;
System.out.println(“Total: ” + total);
}
System.out.println(“Total: ”+ total);
// illegal Total is now out of scope.
Global Variables
Global variables are defined outside of a function, usually on top of the program. A global variable can be accessed by any
function. That is, a global variable is
Objective-C
#import <Foundation/Foundation.h>
Int myVar = 321;
Int main (int argc, conts char * argv[]){
@autoreleasepool{
NSLog(@”myVar = %i”, myVar);
}
return 0;
}
The second file, named display.m contain the code for
the displayit() function.
#import <Foundation/Foundation.h>
extern int myVar;
void displayit(){
NSLog(@”MyVar from different source file is i%”, myVar);
}
Java
Public class Global{
Int myVar = 321;
public static void main (String args[]){
System.out.println(“myVar = ”+ myVar);
}
void displayit(){
System.out.println(“myVar = ”+myVar);
}
}
Multiple Classes In .h file
 In the Models.h file we declare two classes
Multiple Classes in .h file(cont.)
 Import Models.h we get two classes
Accessing static and non static field to category
 Models class(.h.m)
Accessing static and non static field to category
 Calling static field(check) and non static field(modelsfield) to MyCatagory.
Enumerated Data Types
• Enumerated Type : Is an user-defined type that is used in Objective-C to represent the
data that can be stored in one of several predefined values (all of them are int).
• What is it’s purpose : The purpose of enumerated types is to create specifically named
constants.
• Syntax : enum name_of_enumerator { element1, element2,…};
• enum : tell compiler to create enumeration
• name_of_enum : is the name to be assigned the enumeration
• element: field are the name that can be used to set variable
Enumerated Data Types
NOTE :
• enumerators use numbers corresponding to the element
• By default the first value corresponds to 0, the second to 1 and so on
• It is also possible to specify the values used by each value name
enum temperatureTypes{
cold = 10, // (redefined up = 10)
warm, // down will 11 (increment one from previous)
hot = 95, // (redefined left = 95)
veryhot // right will 96 (increment one from previous)
};
Example
The Typedef Statement
• The Typedef Statement : allows the programmer to define one Objective-C type as
another.
• To define a new typedef :
• Write the statement as if a variable of the original type was being declared
• Where you would put the name of the variable (after the type), place the new type name.
• Prefix everything with typedef
The Typedef Statement
• Syntax:
• Example:
in this example above, define the name Counter to be equivalent to the Objective-C
data type int.
The second line we declare variable to be type of Counter.
typedef DataType variable;
typedef int Counter;
Counter i, j;
Data Type Conversion
• Type casting is a way to convert a variable from one data type to another data type.
• In Objective-C when we evaluation of the two operands, type of the result is convert to
the big size between the two operands automatically but except char, short int or of an
enumarated data type they are converted to int.
• Example: Output
• Example:
Output
Data Type Conversion (cont.)
• In Objective-C is almost the same with Java but still have a little bit different below:
• In Java long and short are data type but in Objective-C is not so it make a conversion
different.
In Java:In Objective-C:
Categories
 Category is the best way to add more methods to class in objective – C language.
 We can override method in base class but it is poor programming practice( duplicate functionalities of
method when running time).
 Name of category start with base class plus category name( BaseClassName+CategoryName) in import
section.
class
Category
Categories
 Syntax:
@interface ClassName (CategoryName)
//// methods declaration part
@end
Categories
Cateories Example:
 MyModel class has only one method (ownMethod).
Categories
Cateories Example:
 CategoryOfMyModel has one method (newMethod).
Categories
Cateories Example:
MyModel
-(void)ownMethod
-(void)newMethod
Compile time
MyModel
-(void)ownMethod
MyModel+Categor
yModel
-(void)newMethod
Class Extensions
• Class extension is a special case allows creating a category without a name
- when you define an unnamed category like this, you can extend the class by adding
additional instance variables and properties.
- This is not allowed for named categories.
Class Extensions Cont......
MyClass.h
MyClass.m
Class Extensions Count......
Main method
Protocol
• protocol fully complete abstraction every method in protocol is abstract
• it is list the method name without body, it wait other class implement it and implement
body to it.
• we use it to create the standard method for all classes use the same method when they
implement protocol, it is look like interface in java.
• and it use for delegate also
syntax in objective c syntax in java
Protocol
• in objective divided two part of protocol @required and @optional
• @required it mean that we need to implement method when the class implements it.
• @optional it mean that we are not need to implement method when the class
implements it, it is up to you.
syntax:
Protocol
• we can implement more than one protocol
• how to implement single and multiple implement protocol
• first we need to import protocol
single implementation in objective c Single implementation in java
Protocol
multiple implementation in objective c multiple implementation in java
Delegate
• Delegate allow one object send message to other object when an event happen
• We use delegate almost for event handler in IOS
• We can use delegate when we want it to do something when something happen in our
program and raise it up.
• We almost use id for create a delegate
• We will raise it detail in later chapter that detail about the delegate
Composite Object
• What is composite object?
 A technique that a class consists of one ore more objects from other classes is call
“Composite Object”.
 Diagram above show you that ClassA has ClassB as its composite.
 If ClassA is deleted then ClassB is also deleted as a result. But if ClassB is deleted ClassA will
not be delete.
ClassA ClassB
Composite Object
• Example of composite object:
Composite Object
• Let Create other Square class that have Rectangle as its composite
Composite Object
• In our main.m file
Composition Object
• Let compare our code with java code:
 Rectangle.java
Composite Object
• Square.java
Composite Object
• Main.java
Composite Object
• The result of these two will output the same
Thank you

More Related Content

What's hot

Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
Abhilash Nair
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
JSpiders Basavanagudi
 
Wrapper classes
Wrapper classes Wrapper classes
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
Andrew Ferlitsch
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
dezyneecole
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
simarsimmygrewal
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
Eduardo Bergavera
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
Akshay soni
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Java Generics
Java GenericsJava Generics
Java Generics
DeeptiJava
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
Rajesh Roky
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
Garik Kalashyan
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
Mahmoud Ali
 
Objective c
Objective cObjective c
Objective c
ricky_chatur2005
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
Diksha Bhargava
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Ios development
Ios developmentIos development
Ios development
elnaqah
 

What's hot (20)

Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Objective c
Objective cObjective c
Objective c
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Ios development
Ios developmentIos development
Ios development
 

Viewers also liked

004
004004
I os 11
I os 11I os 11
I os 11
信嘉 陳
 
Utilising View Controllers
Utilising View ControllersUtilising View Controllers
Utilising View Controllers
danielctull
 
아이폰강의(4) pdf
아이폰강의(4) pdf아이폰강의(4) pdf
아이폰강의(4) pdf
sunwooindia
 
Parallactic Collection Views
Parallactic Collection ViewsParallactic Collection Views
Parallactic Collection Views
René Cacheaux
 
아이폰강의(6) pdf
아이폰강의(6) pdf아이폰강의(6) pdf
아이폰강의(6) pdf
sunwooindia
 
iOSハンズオントレーニング observer編 (delegate,notification,KVO)
iOSハンズオントレーニング observer編 (delegate,notification,KVO)iOSハンズオントレーニング observer編 (delegate,notification,KVO)
iOSハンズオントレーニング observer編 (delegate,notification,KVO)
聡 大久保
 
Push notifications
Push notificationsPush notifications
Push notifications
Sam Verschueren
 
UICollectionView — Александр Зимин
UICollectionView — Александр ЗиминUICollectionView — Александр Зимин
UICollectionView — Александр Зимин
CocoaHeads
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewControllerTom Fan
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView
Tom Fan
 
UITableView Training Presentation Slides
UITableView Training Presentation SlidesUITableView Training Presentation Slides
UITableView Training Presentation Slides
grateDvyde
 
Push notifications
Push notificationsPush notifications
Push notifications
Dale Lane
 
iOS: Table Views
iOS: Table ViewsiOS: Table Views
iOS: Table Views
Jussi Pohjolainen
 
iOS Programming 101
iOS Programming 101iOS Programming 101
iOS Programming 101
rwenderlich
 
07 Navigation Tab Bar Controllers
07 Navigation Tab Bar Controllers07 Navigation Tab Bar Controllers
07 Navigation Tab Bar Controllers
Mahmoud
 
Push notifications
Push notificationsPush notifications
Push notifications
Ishaq Ticklye
 
Delivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in Minutes
Sasha Goldshtein
 
Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8
Xamarin
 
Collection view layout
Collection view layoutCollection view layout
Collection view layout
Ciklum Ukraine
 

Viewers also liked (20)

004
004004
004
 
I os 11
I os 11I os 11
I os 11
 
Utilising View Controllers
Utilising View ControllersUtilising View Controllers
Utilising View Controllers
 
아이폰강의(4) pdf
아이폰강의(4) pdf아이폰강의(4) pdf
아이폰강의(4) pdf
 
Parallactic Collection Views
Parallactic Collection ViewsParallactic Collection Views
Parallactic Collection Views
 
아이폰강의(6) pdf
아이폰강의(6) pdf아이폰강의(6) pdf
아이폰강의(6) pdf
 
iOSハンズオントレーニング observer編 (delegate,notification,KVO)
iOSハンズオントレーニング observer編 (delegate,notification,KVO)iOSハンズオントレーニング observer編 (delegate,notification,KVO)
iOSハンズオントレーニング observer編 (delegate,notification,KVO)
 
Push notifications
Push notificationsPush notifications
Push notifications
 
UICollectionView — Александр Зимин
UICollectionView — Александр ЗиминUICollectionView — Александр Зимин
UICollectionView — Александр Зимин
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView
 
UITableView Training Presentation Slides
UITableView Training Presentation SlidesUITableView Training Presentation Slides
UITableView Training Presentation Slides
 
Push notifications
Push notificationsPush notifications
Push notifications
 
iOS: Table Views
iOS: Table ViewsiOS: Table Views
iOS: Table Views
 
iOS Programming 101
iOS Programming 101iOS Programming 101
iOS Programming 101
 
07 Navigation Tab Bar Controllers
07 Navigation Tab Bar Controllers07 Navigation Tab Bar Controllers
07 Navigation Tab Bar Controllers
 
Push notifications
Push notificationsPush notifications
Push notifications
 
Delivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in Minutes
 
Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8
 
Collection view layout
Collection view layoutCollection view layout
Collection view layout
 

Similar to Presentation 4th

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
Sunny Shaikh
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
agorolabs
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Connex
 
Lecture02
Lecture02Lecture02
Lecture02
elearning_portal
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
MH Abid
 
C#2
C#2C#2
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
Gayathri Ganesh
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
 
Pooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearPooja Sharma , BCA Third Year
Pooja Sharma , BCA Third Year
Dezyneecole
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
Woxa Technologies
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
Woxa Technologies
 

Similar to Presentation 4th (20)

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
Lecture02
Lecture02Lecture02
Lecture02
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
C#2
C#2C#2
C#2
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Pooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearPooja Sharma , BCA Third Year
Pooja Sharma , BCA Third Year
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 

Presentation 4th

  • 1. More On Variable, Categories and Protocols Chhorn Chhaly Leang Bunrong Cheng Udam Kan Vichhai Srou Chanthorn Em Vy Seth Sambo Chea Socheat
  • 2. Content 1. More on Variables and Data Types  Initialize Objects  Scoped Revisited  More on properties, Synthesized Accessors, and Instance Variables  Global Variable  Static Variable  Enumerated Data Types  The typedef statement  Data Type Convers
  • 3. • Categories and Protocols  Categories  Class Extensions  Protocols and Delegation  Composite Object
  • 4. Initializing Objects • We initializing object by calling “init” method • For example Fraction *f=[[Fraction alloc]init]; • The example above we just get an Object that has nothing or field value • If we want to initialize object with many fields value, we have to create our custom method to initialize object
  • 6. Initializing Objects • The method initWithNumerator:(int)n withDenominator:(int)d is the custom method to initializing object • All initializing methods must start with word “init…..” • Self=[super init] means the super class calls “init” method to initialize for current object • But it just an object without any fields value • After that line we see that we initialize _numerator and _denomitor • Then we return “self” that fulfill of object with field values
  • 7. Variable Scope In Objective-C, a variable has been declared it may or may not be accessible to other sections of the program code. This accessibility depends on where and how the variable was declared and where the code is that needs to access it. This is know as variable scope.
  • 8. Local Variables Variables that are declared inside a function or block are called local variables. They can be used only by statement that are inside that function or block of code. Local variable are not known to functions outside their own. Example: Objective-C: -(void) sum:(int) x secondP:(int) y { int total; total = x+y; NSLog(@”Total: %i”, total); } NSLog(@”Total: %i”, total); // illegal Total is now out of scope. Java: public void sum(int x, int y){ int total; total = x+y; System.out.println(“Total: ” + total); } System.out.println(“Total: ”+ total); // illegal Total is now out of scope.
  • 9. Global Variables Global variables are defined outside of a function, usually on top of the program. A global variable can be accessed by any function. That is, a global variable is Objective-C #import <Foundation/Foundation.h> Int myVar = 321; Int main (int argc, conts char * argv[]){ @autoreleasepool{ NSLog(@”myVar = %i”, myVar); } return 0; } The second file, named display.m contain the code for the displayit() function. #import <Foundation/Foundation.h> extern int myVar; void displayit(){ NSLog(@”MyVar from different source file is i%”, myVar); } Java Public class Global{ Int myVar = 321; public static void main (String args[]){ System.out.println(“myVar = ”+ myVar); } void displayit(){ System.out.println(“myVar = ”+myVar); } }
  • 10. Multiple Classes In .h file  In the Models.h file we declare two classes
  • 11. Multiple Classes in .h file(cont.)  Import Models.h we get two classes
  • 12. Accessing static and non static field to category  Models class(.h.m)
  • 13. Accessing static and non static field to category  Calling static field(check) and non static field(modelsfield) to MyCatagory.
  • 14. Enumerated Data Types • Enumerated Type : Is an user-defined type that is used in Objective-C to represent the data that can be stored in one of several predefined values (all of them are int). • What is it’s purpose : The purpose of enumerated types is to create specifically named constants. • Syntax : enum name_of_enumerator { element1, element2,…}; • enum : tell compiler to create enumeration • name_of_enum : is the name to be assigned the enumeration • element: field are the name that can be used to set variable
  • 15. Enumerated Data Types NOTE : • enumerators use numbers corresponding to the element • By default the first value corresponds to 0, the second to 1 and so on • It is also possible to specify the values used by each value name enum temperatureTypes{ cold = 10, // (redefined up = 10) warm, // down will 11 (increment one from previous) hot = 95, // (redefined left = 95) veryhot // right will 96 (increment one from previous) }; Example
  • 16. The Typedef Statement • The Typedef Statement : allows the programmer to define one Objective-C type as another. • To define a new typedef : • Write the statement as if a variable of the original type was being declared • Where you would put the name of the variable (after the type), place the new type name. • Prefix everything with typedef
  • 17. The Typedef Statement • Syntax: • Example: in this example above, define the name Counter to be equivalent to the Objective-C data type int. The second line we declare variable to be type of Counter. typedef DataType variable; typedef int Counter; Counter i, j;
  • 18. Data Type Conversion • Type casting is a way to convert a variable from one data type to another data type. • In Objective-C when we evaluation of the two operands, type of the result is convert to the big size between the two operands automatically but except char, short int or of an enumarated data type they are converted to int. • Example: Output
  • 20. Data Type Conversion (cont.) • In Objective-C is almost the same with Java but still have a little bit different below: • In Java long and short are data type but in Objective-C is not so it make a conversion different. In Java:In Objective-C:
  • 21. Categories  Category is the best way to add more methods to class in objective – C language.  We can override method in base class but it is poor programming practice( duplicate functionalities of method when running time).  Name of category start with base class plus category name( BaseClassName+CategoryName) in import section. class Category
  • 22. Categories  Syntax: @interface ClassName (CategoryName) //// methods declaration part @end
  • 23. Categories Cateories Example:  MyModel class has only one method (ownMethod).
  • 26. Class Extensions • Class extension is a special case allows creating a category without a name - when you define an unnamed category like this, you can extend the class by adding additional instance variables and properties. - This is not allowed for named categories.
  • 29. Protocol • protocol fully complete abstraction every method in protocol is abstract • it is list the method name without body, it wait other class implement it and implement body to it. • we use it to create the standard method for all classes use the same method when they implement protocol, it is look like interface in java. • and it use for delegate also syntax in objective c syntax in java
  • 30. Protocol • in objective divided two part of protocol @required and @optional • @required it mean that we need to implement method when the class implements it. • @optional it mean that we are not need to implement method when the class implements it, it is up to you. syntax:
  • 31. Protocol • we can implement more than one protocol • how to implement single and multiple implement protocol • first we need to import protocol single implementation in objective c Single implementation in java
  • 32. Protocol multiple implementation in objective c multiple implementation in java
  • 33. Delegate • Delegate allow one object send message to other object when an event happen • We use delegate almost for event handler in IOS • We can use delegate when we want it to do something when something happen in our program and raise it up. • We almost use id for create a delegate • We will raise it detail in later chapter that detail about the delegate
  • 34. Composite Object • What is composite object?  A technique that a class consists of one ore more objects from other classes is call “Composite Object”.  Diagram above show you that ClassA has ClassB as its composite.  If ClassA is deleted then ClassB is also deleted as a result. But if ClassB is deleted ClassA will not be delete. ClassA ClassB
  • 35. Composite Object • Example of composite object:
  • 36. Composite Object • Let Create other Square class that have Rectangle as its composite
  • 37. Composite Object • In our main.m file
  • 38. Composition Object • Let compare our code with java code:  Rectangle.java
  • 41. Composite Object • The result of these two will output the same

Editor's Notes

  1. Fraction *f=[[Fraction alloc]init]; Because of init method is not our own init method.. So that mean this init method is the method of NSObject.. So we initialize Objects beacause we want initialize many custom field in our own method. To do this we have to override init method by In Java it is call Constructor.
  2. This example is conveniece initializer [super init]  we initialize instance of NSObject which is the root of its Class. Init will return id type that why we have to initialize with its arquments
  3. In this slide just wan to explain What Scope variable mean? It just refer for said… we don’t know … depend oh where and how the variable was declare . This this slide we have to introduce about some of block of code.
  4. It is similar to concept of java,,,if we declare in slide the method …it is method variable or block code… Concept Java
  5. Key word extern is used for external variable declaration and without definition..because just declare is not allocate space in memory. Extern int gMoveNumber = 0 //wrong It was created to used and initialize value in method or block of code. Declare without extern but when invoke use extern keyword It is not effect to external declaration
  6. Hide from other class For declare in external variable , static is very useful,, it inherit class can’t get it as well Use extern keyword to use it every times Explain him about 6 type of static work
  7. By default, the first element in a list is set equal to 0, and each subsequent element is assigned a value one greater. In the example above, the value north is equivalent (in all contexts) to the integral constant 0, south is equal to 1, and so on. You can also specify an integer value to be equal to an enumerated value:
  8. 1. That mean each element have value 1,2,..
  9. Advantages : readability,
  10. The result will show 581.1212 but in this case…type int result will cut the last value
  11. We use categories to add more method to a class Divide single class definition in to twopart. To arrange a big source code which have many method It work as normal class too. Cos it compose with interface and implementation When runtime the class that use that categories will have have all method in category It is a flat organization structure
  12. + MyModel+CategoryOfMyModel is a category but it will provide its method to its class
  13. Category if we need one or more..we have to append it with difference file But extension calss that mean the same class can exten more such in .m file
  14. This small program want user to play count clicking In MyClass.h have only two method and MyClass.m have Class extension to declare as intger - When call MyClass.h​, we can use only method In its..but in .m method was now allow to use…so this case use method in Origin Class
  15. A protocol is a list of method declarations that is not bound to any one class. To declare methods for others to implement To declare the public interface to a class while hiding the nature of the class itself To mark similarities between classes that are not part of the same inheritance tree
  16. “A delegate is an object that acts on behalf(ផលប្រយោជន) of, or in coordination(សម្របសម្រួល) with, another object when that object encounters(ការជួបប្រទះ) an event in a program