1 / 33

Objective C Basics

Objective C Basics. It’s C with Some Extras!. Cross Platform language Mac Linux/UNIX Windows Layer above C (Superset) Adds Object-Oriented features to C C works in Objective-C programs. Sample C Program.

hasana
Télécharger la présentation

Objective C Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objective C Basics

  2. It’s C with Some Extras! • Cross Platform language • Mac • Linux/UNIX • Windows • Layer above C (Superset) • Adds Object-Oriented features to C • C works in Objective-C programs

  3. Sample C Program Code sample from Objective-C: Visual QuickStart Guide. Page 12. (copied 12/19/2010 from Safari Online)

  4. Getting Started with Obj-C • Native language used for: • Mac development • iPhone/iPod touch development • iPad development • Download SDK (XCODE) for development on Mac

  5. What is OOP? • Object Oriented Programming! • History • Structured programming • Procedural programming • Object Oriented programming

  6. Why Object-Oriented? • Be able to break up code into objects • Self-contained, with details not visible

  7. OOP Terminology • Class—defines an object and what that object can do. Code in your OOP program. Usually consists of an interface (header, .h) file and an implementation (.m) file. • SubClass—inherits properties and actions of it’s parent class. • Superclass—the parent class • Object or Instance—an active class that you are currently using in your code. Classes define what the properties and capabilities of an object are, whereas an object or instance of that class is what we actively use in the program. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  8. OOP Terminology, continued • Instantiation—creating an object or instance • Instance method—an action/method that is implemented IN a class for an instance. • Class method—an action/method that is applied to the class and applicable to all objects created from that class. • Message—when you use a method. Give the name of the instance followed by the name of the method. • Instance variable—storage holder to hold information specific to a class and is available inside of an object. • Variable—storage holder, only available in the method where defined. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  9. OOP Terminology, continued • Parameter: info that is sent when you send a message • Property: an instance variable (used @property and @synthesize) that is easy to access in your code. --------------------------------------------------------------- • Directive: commands that helps Xcode, Interface Builder and other development tools. Directive does not implement program logic…it tells Xcode (etc..) how your program is structured. • Protocols—methods grouped together under a common name. May be adopted (or implement if Java background) by a class. The class is contracted to implement the protocols methods. Some of the protocol methods may be optional. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  10. Objective-C Characteristics and Symbols • Written differently from other languages • Object communicate with messages—does not “call” a method. • @ indicates a compiler directive. Objective-C has own preprocessor that processes @ directives. • # indicates a preprocessor directive. Processes any # before it compiles. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  11. Common Compiler Directives • @ interface—used to describe instance variables for your class (in header file) • @property –simplifies interaction with interface variables. Automates the getters and setters for the instance variables. (in header file). • @end—shows the end • @implementation—shows the compiler which class you are using, followed by the code you are implementing. (in implementation file) • @synthesize—second part of @property, that is located in the implementation file. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  12. Common Preprocessor Directives • #import <file.h> tells the program to replace statement with the file.h contents. Used instead of # include. Will only add a header file once. • #define –preprocessor macro. Used to define constants. • #pragma mark—used to create sections in your code. Do not add any features. Will show up in the symbol menu (like bookmarking). From Sam’s Teach Yourself iPhone Application Development in 24 hours

  13. Parts of a Method in a Class • Methods are executed on behalf on an instance from the class. Method receives a pointer to the object instance. • Methods may directly access the object’s instance variables • Syntax for a method is different than for a function From Learning Objective-C 2.0: A Hand’s on Guide for Mac and iOS Programmers

  14. Declare in Header file (.h) Each method will start with either a – or a + symbol. - indicates an instance method (the receiver is an instance) +indicates a class method (the receiver is a class name) Example of a instance method -(IBAction)buttonPressed:(id)sender; Or with one argument -(void)setFillColor:(NSColor*) newFillColor;

  15. Parts of a Method in a Class • Implement the method in the .m file • Example: -(IBAction)buttonPressed:(id)sender{ do code here…. } • Example 2: -(void) setOutlineColor:(NSColor*) outlineColor{ do code here…. }

  16. Messaging • To Send a Message: • Format is [receiver message] instead of doing a call. • Benefit is that the receiver and method are not bound together at compile time…you only send the method name. • When executes, the receiver objects executes the version of the method that is part of it’s class. • C++ determines method calls at compile time, Objective-C sends the message at runtime (execution time) • Format for a message with a parameter is [receiver methodName: parameterValue] • Format for a message with multiple parameters is [recievermethodName: parameterValuenextParameterName: parameterValue] • Example of multiple parameter message: [userName compare:@”John” options:NSCaseInsensitive];

  17. Simple Objective-C Program for iOS • Open Xcode • Start a new Project by selecting Template • Name project. • Add header code (viewController) • Save • Add implementation code (viewController) • Save • Double-click .xib file in resources for ViewController • Add objects in Interface Builder • Link outlets and actions • Save and Exit Interface Builder • Build and Run (check for error messages)

  18. Project Folder and Opening Existing Project

  19. ViewController.h

  20. ViewController.m (top)

  21. ViewController.m (lower)

  22. Formatting • All statements end in a ; • Statements can go over multiple lines • White space and indenting are ignored

  23. Comments • Use // for a single line • Use /* to begin a comment, and */ to end a multiline comment

  24. Naming Conventions • Name of variable or function is case sensitive • May not include special characters • May not have spaces • Must begin with either an _ or a letter. No numbers

  25. More about Files and Naming • _ in front of a variable name generally indicates a private (or internal) variable • Objective-C often uses camelCase naming. This has the first word as lowercase and runs the second word into the first and capitalizes the second word • If mix C++ and Objective-C use a .mm file extension

  26. Variables and Instance Variables • Variables holds values • Variables must be declared and assigned a data type (so the compiler knows how much memory the value takes up and can assign the appropriate amount) • Example of instance variable: NSString *myString;

  27. Categories of Data Types • Primitive Data Types • Not an object • Stores value directly in variable Example: intuserAgeInDays; userAge=30; userAgeInDays = userAge * 365 • Object Data Types • Created from a class • Can not store value directly • Variable with this data type points to the location of the value, it does not directly contain the value Example: NSString *userName; From Sam’s Teach Yourself iPhone Application Development in 24 hours

  28. Primitive Data Types From Objective-C for Absolute Beginners (pg50)

  29. Other File Extensions

  30. Pointers • Objective-C uses pointers to indicate where objects are stored in memory. • So when we create a variable that is going to hold Class Data Type, we use * to indicate that we are pointing to the value. • * is used when the variable is declared, but does not have to be added when you use the variable after the declaration. From Sam’s Teach Yourself iPhone Application Development in 24 hours

  31. Pointers, Allocating and Initializing • After a variable has been declared as a pointer to an object, it does not yet exist. You just told Xcode that you intend the variable to point to that object’s location • You must allocate memory for the object. • After allocating you must initialize the contents of the object. Example: UILabel *myLabel; myLabel = [[UILabelalloc] init]; From Sam’s Teach Yourself iPhone Application Development in 24 hours

  32. Convenience Methods • After initialize, need to make useful • Special initialization method that setup an object with a set of properties (so you do not have to do each one yourself) Example: initWithString:

More Related