cocoa
play

Cocoa Demo: GUI-based Stk app. Xcode Interface Builder StkX - PDF document

Last Week... Music 3SI: Introduction to IDE (briefly) Audio/Multimedia App. VST Plug-in Programming Assignment 1 hints Week #5 - 5/5/2006 CCRMA, Department of Music Stanford University 5/5/06, Music 3SI, CCRMA, Stanford 1 2


  1. Last Week... Music 3SI: Introduction to • IDE (briefly) Audio/Multimedia App. • VST Plug-in Programming • Assignment 1 hints Week #5 - 5/5/2006 CCRMA, Department of Music Stanford University 5/5/06, Music 3SI, CCRMA, Stanford 1 2 Today... • Cocoa • GUI programming Cocoa • Demo: GUI-based Stk app. � Xcode � Interface Builder � StkX 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 3 4 Wikipedia - Cocoa Why Cocoa? • Cocoa is well thought out “Cocoa is the dried and partially fermented with highly consistent APIs fatty seed of the cacao tree from which chocolate is made” • Provides a very rich starting point “This project is particularly difficult to find “Cocoa was a development environment “Slavery has commonly been used in its “Uses of cocoa are numerous. for exploring application design created by Apple Computer in the mid- to information on, because the same production...” It may be used in cakes, creams, drinks, company (Apple) has since released late-1990s that allowed children to • Shows “real-world” implementations develop applications and web sites.” toppings.” another, much more important Cocoa.” of OO design patterns The project was cut by Steve Jobs in 1998. http://en.wikipedia.org/wiki/Cocoa_(API) 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 5 6

  2. Cocoa Is Many Things Cocoa Applications • It’s a runtime environment � Dynamic dispatch is fundamental • It’s a user interface framework Mail Safari iChat � Events, views, buttons, sliders and so on • It’s a development framework Photo Booth Automator iPhoto � A collection of reusable and extendable objects Keynote Aperture IB 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 7 8 Using Cocoa Mac OS X Architecture • GUI (Graphical User Interface) applications • Command-line tools Frameworks Mach • Plug-ins Core Foundation BSD 4.4 Cocoa Application Services Quartz (2D) Carbon Core Carbon IOKit • Even device drivers! OpenGL (3D) CFNetwork File Systems Classic Core Services QuickTime Web Services Java Open Source Kernel - Darwin 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 9 10 Cocoa Architecture Event-Driven Applications • AppKit manages the flow of events Frameworks • Your code is invoked automatically as the Application Kit user interacts with the application Aqua Elements Application Runtime • You write small chunks of code that handle UI Widgets specific events Foundation Kit • Simple, easy-to-use model Utility Classes Collection Classes Object Wrappers for OS Services 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 11 12

  3. Basic Tools Xcode • Xcode • “Wizard” helps you create new projects � coding � no Harry Potter this � app-level specifications • Best to stick with Xcode-defaults � building in new projects for now � debugging • Don’t let the complexity overwhelm you • Interface Builder � user-interface design � basic connections between objects 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 13 14 Xcode - A Development Interface Builder • Edit your code • Lays out and connects user-interface elements • Specify how your code is compiled � Target/action and linked � Outlets • Build and run your code � Bindings • Debug your code • Edits “nib” files � A nib file a collection of archived objects (your user interface) stored on disk 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 15 16 Objects: Evolution of C • In C, the building blocks are structures and functions • OOP provides an abstraction over these OOP • A way of organizing structures and functions into self-contained units • Lets you group functions with the data they operate on 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 17 18

  4. OOP Vocabulary Encapsulation • Class: • Keeps implementation details private � defines the grouping of data and code (“type”) • Forces a clearly defined interface to access data or functionality • Instance: � a specific allocation of a class • Interface is the public “contract” or API • Method: • Implementation can be changed without affecting callers � a “function” that an object knows how to perform • Instance Variable: � a specific piece of data belonging to an object 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 19 20 Polymorphism Inheritance • Different objects can respond to the same • A class is always derived from a “base” methods in specific ways class • Because data is bound to functionality, • Subclasses can: methods know what to operate on � Add new variables or methods � Replace method implementations • Simplifies interfaces by using consistent terminology � Refine or extend inherited methods • Code that is common among objects can be factored to a superclass for reuse 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 21 22 Inheritance More OOP Info? • Tons of books and articles on OOP • Most Java or C++ book have OOP Superclass Memory introductions NSObject management • ADC document Generic NSControl � http://developer.apple.com/documentation/ behaviors Cocoa/Conceptual/ObjectiveC Specific NSButton NSTextField behaviors Subclass 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 23 24

  5. Objective-C • A very simple language, but some new syntax • Strict superset of C Objective-C • Single inheritance � classes inherit from one and only one superclass • Dynamic runtime 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 25 26 Why ObjC? Class Interfaces • Exposure to other languages is always good typedef struct @interface Person:NSObject { { • A language focused on simplicity char *name; Instance and the elegance of OO design int age; Variables float weight; } Person; } • A data point to compare with designs of C, C++ and Java void printName(Person *person); - (void)printName; @end Methods 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 27 28 Class Implementations ObjC Files Person.h Person.m @implementation Person - (void)printName void printName(Person *person) @interface Person:NSObject @implementation Person { { - (void)printName printf (“Name: %s\n”, person->name); name); char *name; { } int age; printf (stderr, float weight; "Name: %s\n", name); } } @end - (void)printName; @end @end 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 29 30

  6. Messaging Syntax Messaging Syntax • Calling a method called “doSomething” • Calling a method “divide” with arguments C Function: divide(arg1, arg2); C Function: doSomething(anObject); C++ or Java: obj.divide(arg1, arg2); C++ or Java: anObject.doSomething(); ObjC: divide: by: arg2 arg1 [obj ]; - (float)divide:(float)arg1 by:(float)arg2; ObjC: anObject doSomething [ ]; Selector: divide:by: 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 31 32 Types of Methods Using Classes • Instance methods operate on a specific object #include “Person.h” • Class methods are global and have no specific data associated with them main () { Person *person; • “-” denotes instance method person = [[Person alloc] init]; person = [Person alloc]; � - (void)printName; [person init]; [person printName]; • “+” denotes class method } � + (void)alloc; 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 33 34 “self” and “super” String Constants • Methods have an implicit local variable • In C constant strings are named “self” (like “this” in C++) � “simple” � - (void)doSomething { • In ObjC, constant strings are [self doSomethingElseFirst]; ... � @“just as simple” } • Constant strings are NSString instances • Also have access to “super” methods � - (void)doSomething { [super doSomething]; ... } 5/5/06, Music 3SI, CCRMA, Stanford 5/5/06, Music 3SI, CCRMA, Stanford 35 36

Recommend


More recommend