1 / 38

ART40545

ART40545. Basics of Programming:IOS Kris Secor. Course Intro. This will be part iphone SDK part Objective C Initially, we will jump into Objective C As we move further along in the course, xcode and the sdk will overtake obj-c …but you can expect portions of each in every class.

loan
Download Presentation

ART40545

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. ART40545 Basics of Programming:IOS Kris Secor IOS Development

  2. Course Intro • This will be part iphone SDK part Objective C • Initially, we will jump into Objective C • As we move further along in the course, xcode and the sdk will overtake obj-c…but you can expect portions of each in every class IOS Development

  3. Grading • There are 6 homeworks….I reserve the right to alter that to accommodate you. • Any alteration to the course plan will not be done without your collective approval… • There is a quiz in week 9, which we will start in class and you will be allowed to take it home and submit it in week 10 IOS Development

  4. What about the reading? • Mostly for reference, unless you learn best that way. • Always use the companion code. • Always test, recode, extend • This course is about accessing resources, and understanding the terminology • Apple wants to help, but in doing so have cluttered the documentation with thousands of articles. IOS Development

  5. The problem with online help… • Xcode 4.5 is very new (9/12)…there are very few tutorials on it…but it has many new toys…like ARC (Automatic Reference Counting). • The bulk of tutorials will be prior version of xcode where memory references are released manually. • Always go with the apple docs before outside tutorials…always…stackoverflow is good, but make sure you check the date… IOS Development

  6. The Project • This will be one project that will utilize all that is taught in this course…. • It will cover the interface, • It will cover storage and accessibility • Let’s look at the project concept IOS Development

  7. Let’s dive in! • Objective C is a super set of C • Where C is procedural, Obj C is completely OOPed…many classes that prewritten… • Also may be able to get additional classes from the docs… IOS Development

  8. Introduction • Objective-C is implemented as set of extensions to the C language. • It's designed to give C a full capability for object-oriented programming, and to do so in a simple and straightforward way. • Its additions to C are few and are mostly based on Smalltalk, one of the first object-oriented programming languages.

  9. Why Objective C • Objective-C incorporates C, you get all the benefits of C when working within Objective-C. • You can choose when to do something in an object-oriented way (define a new class, for example) and when to stick to procedural programming techniques (define a struct and some functions instead of a class). • Objective-C is a simple language. Its syntax is small, unambiguous, and easy to learn • Objective-C is the most dynamic of the object-oriented languages based on C. Most decisions are made at run time.

  10. Object-Oriented Programming • The insight of object-oriented programming is to combine state and behavior, data and operations on data, in a high-level unit, an object, and to give it language support. • An object is a group of related functions and a data structure that serves those functions. The functions are known as the object's methods, and the fields of its data structure are its instance variables.

  11. The Objective-C Language • The Objective-C language is fully compatible with ANSI standard C • Objective-C can also be used as an extension to C++. • Although C++ itself is a Object-Oriented Language, there are difference in the dynamic binding from Objective-C

  12. Objective-C Language (cont.) • Objective-C source files have a “.m” extension • “.h” file is the interface file • For example: • main.m • List.h (Interface of List class.) • List.m (Implementation of List class.)

  13. Object Oriented Terms • Class - (description/template for an object) • Instance - (manifestation of a class) • Message - (sent to objects to make them act) • Method - (code invoked by a message) • Instance Variable (ivar) – object specific storage • Inheritance – code sharing mechanism • SuperClass/Subclass – code sharing relationships • Protocol –non-class-specific method declaration IOS Development

  14. Preexisting Classes • There are many, many, many of these • NS stands for Next-sun • Let’s check the foundation framework • This will be the predominant framework for our project IOS Development

  15. Classes • Here are some simple, common classes weʼll see a lot of: • NSString is a string of text that is immutable. • NSMutableString is a string of text that is mutable. • NSArray is an array of objects that is immutable. • NSMutableArray is an array of objects that is mutable. • NSNumber holds a numeric value. IOS Development

  16. Dynamic Language • Almost everything is done at runtime • Uses dynamic typing, linking, and binding • This allows for greater flexibility • Minimizes RAM and CPU usage

  17. To Import or Include? • C/C++’s #include will insert head.h into the code even if its been added before. • Obj-C’s #import checks if head.h has been imported beforehand. #import “head.h”

  18. Primitive data types from C • int, short, long • float,double • char

  19. Operators same as C • + • - • * • / • ++ • --

  20. Conditionals and Loops Same as C/C++ if / else if/ else for while break continue do-while for(inti=0; i< 22; i++) { printf(“Checkingi=“@d\n”, i); if(i+90 == i*i) { break;} }

  21. for in loop • Introduced in Objective-C 2.0 (“fast enumeration”) for(Item_Type *item in Collection_of_Items) { //do whatever with the item Nslog(@” Looking now at %@”, item); } Note: %@ in the NSLogconverts whatever is passed (in this case item) to a string

  22. C-style arrays int multipleValues[5]; multipleValues[0] = 50; multipleValues[1] = 60; or int multipleValues[5] = {50,60,70,80,90}; or int multipleValues[0] = {50,60,70,80,90}; IOS Development

  23. C-Style Arrays with Objects Formal: NSString *stringArray[5]; stringArray[0] = [[NSStringalloc] initwithString:@”Hello”]; stringArray[0] release; Informal (without memory); NSString *stringArray[5] = {@”first”,@”second”,@”third”,@”fourth”,@”fifth”}; NSLog (@”The first member is %@”,stringArray[0]); • //no bounds checking • //can’t mix types • //fixed size IOS Development

  24. Array Objects and Methods inti; NSMutableArray *array = [[NSMutableArrayalloc]init]; for (i=0;i<10;++i){ [array addObject:[NSStringstringWithFormat:@"%d", (i*3)]]; } inty=0; for (id obj in array){ NSLog(@"array element %i is %@",y,obj); y++; } IOS Development

  25. Functions --- pass by reference void myFunction(void) { for ( inti = 1 ; i < 5000 ; i++ ) { if (i % 5 == 0) { continue; // jump back to the top. } NSLog(@"The value of the index is %i", i); } } int main (intargc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init]; // call the function myFunction(); [pool drain]; return 0; }

  26. Passing Arguments void myFunction(intx, inty) { NSLog(@"The value of the x is %i and the value of y is %i",x,y); } int main (intargc, const char * argv[]){ // call the function myFunction(4,5); return 0; } IOS Development

  27. Pointers and Objects NSMutableString *message = @"Hello"; NSString *newmessage = [message insertString:@" Kris, how are you?" atIndext:3]; NSLog(@"The new message is @%",newmessage); IOS Development

  28. Passing the pointer Void myFunction (NSString *inbound) {NSLog(@”The message was %@ “,inbound); } NSString *message = @”Hello”; myFunction(message); IOS Development

  29. Classes • Have both definition file (interface) and implementation file : classname.h and classname.m

  30. Declaring a class in ClassName.h #import <Cocoa/Cocoa.h> @interface ClassName : Parent { //class variables int age; NSString name; } // methods declared -(void)setAge:(int)number; -(void)setName:(NSString)n; -(int)getAge; -(NSString)getName; @end #import <standardimports.h> #import “local-your-otherfiles.h” @interface ClassName: Parent { //class variables } //methods -(return_type) methodName:(type)param1, (type) param2; @end

  31. Declaring methods C++ syntax void function(int x, int y, char z); Object.function(x, y, z); Objective-C syntax -(void) method:(int)x, (int)y, (char)z; [Object function:x, y, z]; -(return type) function_name: (type) p1, (type) p2, ***; Apply function to Objectpassing parameters x,y,z

  32. Whats this + and – stuff? • When declaring or implementing functions for a class, they must begin with a + or - • + indicates a “class method” that can only be used by the class itself. (they are like static methods in Java invoked on class itself) • - indicates “instance methods” to be used by the client program (public functions) –invoked on an object / class instance . (they are like regular methods in Java invoked on object)

  33. Inheritance • Root class is NSObject • Inheritance is cumulative. A Square object has the methods and instance variables defined for Rectangle, Shape, Graphic, and NSObject, as well as those defined specifically for Square

  34. Inheritance (cont.) • Instance Variables: The new object contains not only the instance variables that were defined for its class, but also the instance variables defined for its super class, all the way back to the root class • Methods: An object has access not only to the methods that were defined for its class, but also to methods defined for its super class • Method Overriding: Implement a new method with the same name as one defined in a class farther up the hierarchy. The new method overrides the original; instances of the new class will perform it rather than the original

  35. The Interface • The declaration of a class interface begins with the compiler directive @interface and ends with the directive @end @interface ClassName:ItsSuperclass { instance variable declarations } method declarations @end

  36. Frameworks • UIKit.framework for developing standard iOS GUIs (buttons etc.) • UITextField (user-entered text that brings up the keyboard) • UIFont • UIView • UITableView • UIImageView • UIImage • UIButton (a click-able button) • UILabel (a string of text on-screen) • UIWindow (main Window on iPhone)

  37. iOS programming • Event driven framework • Interface Designer has some extra macros for the code that act like hooks to variables; • IBAction - trigger events like mouse clicks • IBOutlet - captures outputs from events • These tags are not compiled (don't affect the code) but sit as an extra before variables that the Interface Designer can see.

  38. Start a New XCode iOS Project • New Project → iPhone/iPad OS • Navigation-Based - “navigation controller” as used in Contacts app. • OpenGL ES - cut-down OpenGL • Tab Bar – app in style of iPod app. • Utility – Flipside app in style of stock quote app. • View-Based – single view that you draw into and display to screen. • Window-Based – basic app with a main window. (we will use this one for examples.)

More Related