Irving iOS Jumpstart Meetup - Objective-C Session 1b

498 views
334 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Irving iOS Jumpstart Meetup - Objective-C Session 1b

  1. 1. Objective-C Programming Irving iOS Jumpstart
  2. 2. Objective-C Programming January 28, 2014 Part Two
  3. 3. Overview of Today • Preprocessor • Pointers • Objective-C OO
  4. 4. Objective-C Preprocessor • Preprocessor directives tell the compiler to perform specific actions prior to or during compilation of the objective-c code. A directive does not perform any action in the language itself, but rather only a change in the behavior of the compiler.
  5. 5. Preprocessor Directives • Sample directives #import / #include #define #ifdef
  6. 6. Preprocessor Directives • Sample directives #import • substitutes content of imported file #import ViewController.h! ! ! #import <Foundation/Foundation.h>
  7. 7. Preprocessor Directives • Sample directives #define • Define an object or macro for inclusion in objective-c code #define TaxRate = .0825
  8. 8. Preprocessor Directives • Sample directives #ifdef • Enables conditional inclusion of blocks of objective-c code #ifdef DEBUG NSLog (@"We're in debug mode."); #endif
  9. 9. Preprocessor Directives • Demonstrate Preprocessor Directive in sample weather app
  10. 10. Pointers • A pointer references a location in memory • Obtaining the value stored at that location is known as dereferencing the pointer. • Pointers have many useful applications
  11. 11. Pointers • An integer pointer declaration int shoesize = 11; int *shoesizeptr = &shoesize;
  12. 12. Pointers • An integer pointer declaration int shoesize = 11; int *shoesizeptr = &shoesize; • shoesizeptr is a memory address
  13. 13. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • * is the indirection operator
  14. 14. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • * is the indirection operator • shoesizeptr now contains a memory address
  15. 15. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • * is the indirection operator • shoesizeptr now contains a memory address • the address of the value of shoesize
  16. 16. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • * is the indirection operator • shoesizeptr now contains a memory address • the address of the value of shoesize • the integer value 11 is stored at the location
  17. 17. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • & is the address-of operator
  18. 18. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • & is the address-of operator • "set the pointer to an integer, shoesizeptr, equal to the address (location in memory) of the integer variable shoesize"
  19. 19. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • deference the pointer int newshoesize = *shoesizeptr;
  20. 20. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • deference the pointer int newshoesize = *shoesizeptr; • now the variable newshoesize is equal to 11
  21. 21. Pointers • An integer pointer declaration int shoesize = 11; (integer variable) int *shoesizeptr = &shoesize; • deference the pointer int newshoesize = *shoesizeptr; • now the variable newshoesize is equal to 11 • "assign the contents at location shoesizeptr to the integer variable newshoesize"
  22. 22. Arrays • An array is a set of ordered data items • an array is used when the order matters • arrays are zero indexed • array index begins at zero, so an array with 3 elements are indexed 0, 1, and 2
  23. 23. Object Oriented Programming C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming languages.
  24. 24. Object Oriented Programming Concepts Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.
  25. 25. Objective-C OO • Objective-C Classes • Defining classes • Initializers • Instance variables and scope • Declared properties • Methods, messaging polymorphism • Inheritance
  26. 26. Object Oriented Programming Concepts • some OOP terminology • class
  27. 27. Object Oriented Programming Concepts • some OOP terminology • class • encapsulation
  28. 28. Object Oriented Programming Concepts • some OOP terminology • class • encapsulation • inheritance
  29. 29. Object Oriented Programming Concepts • some OOP terminology • class • encapsulation • inheritance • properties
  30. 30. Object Oriented Programming Concepts • some OOP terminology • class • encapsulation • inheritance • properties • methods
  31. 31. Object Oriented Programming Concepts • code reuse is emphasized and enabled via OOP • objects are self-sufficient reusable units of programming logic
  32. 32. Object Oriented Programming Concepts • code reuse is emphasized and enabled via OOP • objects are self-sufficient reusable units of programming logic • a class is a template for object creation
  33. 33. Object Oriented Programming Concepts • code reuse is emphasized and enabled via OOP • objects are self-sufficient reusable units of programming logic • a class is a template for object creation • consist of nouns (properties) and verbs (methods)
  34. 34. Object Oriented Programming Concepts In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members that enable its instances to have state and behavior.
  35. 35. Object Oriented Programming Concepts A class usually represents a noun, such as a person, place or thing, or something nominalized. For example, a "Car" class would represent the properties and functionality of cars in general. A single, particular car would be an instance of the "Car" class, an object of the type "Car"
  36. 36. Defining Classes in Objective-C • Created via .h file and .m file • All classes inherit from NSObject or another class
  37. 37. Object Oriented Programming Concepts Objective-C supports the concept of information hiding and encapsulation. Encapsulation is the principle of controlling access to class members (instance variables and instance methods). The @interface in an objective-c .h file, and the @implementation in an objective-c .m file serve to separate the interface of a class from its implementation.
  38. 38. Object Oriented Programming Concepts Objective-C supports the concept of inheritance - allowing sub-classes to inherit interfaces from the classes that they are derived from. Inheritance enables a subclass to 'inherit' the properties, methods (hence functionality) of the object inherited from.
  39. 39. Object Oriented Programming Concepts NSObject is the base object in Objective-C providing properties and methods necessary for any objective-C object.
  40. 40. Object Oriented Programming Concepts Objective-C class properties are data field descriptions (or fields, data members, or attributes). These are usually field types and names that will be associated with state variables at program run time. The structure defined by the class determines the layout of the memory used by its instances.
  41. 41. Object Oriented Programming Concepts Properties are accessed (read and set) via setters and getters. This enables controlled access to class instance data, a key tenet of OOP encapsulation In objective-c, properties (and their getters and setters) are defined via @property and @synthesize directives.
  42. 42. Object Oriented Programming Concepts Objective-C class methods define the behavior of a class or its instances. Methods are subroutines with the ability to alter the state of an object or simply provide ways of accessing it.
  43. 43. Object Oriented Programming Concepts Objective-C methods take the following form: + or - (return_type)methodName: (data_type)parameter1
  44. 44. Object Oriented Programming Concepts Objective-C methods have two forms; class methods and instance methods.
  45. 45. Object Oriented Programming Concepts • Objective-C class method definitions begin with a plus (+) + (id)alloc;
  46. 46. Object Oriented Programming Concepts • Objective-C class method definitions begin with a plus (+) + (id)alloc; • Objective-C instance method definitions begin with a minus (-) - (void)printTemperature;
  47. 47. Defining Classes in Objective-C • Exercise • Create RentalCar class • Add methods to Car class
  48. 48. Credits • Wikipedia • Programming in Objective-C by Stephen Kochan • Programming in C by Stephen Kochan (any errors or omissions are probably mine)

×