WWDC2003 Session 400

Transcript

Kind: captions
Language: en
thank you thank you it's been a long day
how you guys doing everybody okay
excellent so today's gonna be a really
great session for those of you who are
either new to the platform or have been
here a long time and want to see what's
happening the cocoa side we've got two
really great people we have Matthew
Formica who's a developer technical
support engineer and we have t no one
who is an application framework
engineering they're both going to talk
to you about why you should be using
cocoa and how cool you can do things
with it so let's start with the math you
help me thank you Jason we are going to
get right in to the material here today
what we're going to talk about today is
what cocoa is some of you may be here
adjacent said for the first time you're
not really familiar with cocoa although
you may have already heard about it
we're also going to talk about what some
of the benefits of cocoa are for you as
a developer and how you can use it in a
variety of application settings and
we'll have some demos along the way so
let's dive right in what is cocoa well
what we like to say is that cocoa is a
powerful full-featured easy-to-use
object-oriented framework for Mac os10
development and that's kind of a
mouthful but basically it means more
rapid productivity for you as a
developer thanks to its object oriented
underpinnings and we we believe at Apple
that it is in fact the fastest way to
full-featured applications on Mac OS 10
this is an architecture diagram
illustrating some of the layers that
make up Mac OS 10 at the bottom is
Darwin the UNIX layer on top of that
stood some graphics technologies courts
OpenGL and QuickTime and then the layer
of framework these are the layers that
your applications will typically right
to such things as Java carbon and cocoa
and what we're going to focus in on
today is cocoa we talk about cocoa as
this one monolithic thing
but when we drill down a bit we see that
there are really two core frameworks
that make it up Foundation and app get
the way this works is like so foundation
is where the non graphical user
interface components of cocoa reside
such things as strings and networking
scripting and file system calls reside
here if you're writing an application
that doesn't use the user interface you
could just linked against foundation and
you'd be set however if you do want to
use a graphical user interface you'll
want to add in app get app kit contains
classes for control and windows and all
those things that do use the user
interface it's part of the application
we can actually go beyond this besides
these two core object oriented
frameworks foundation and app kit there
are a variety of other pieces of the
system that work together to make the
cocoa experience what it is these
include things like our development
tools project builder and our newly
introduced Xcode along with interface
builder and their performance tools on
the system all have features carefully
designed for cocoa development on top of
this we have powerful and dynamic
languages that provide the support to
make cocoa possible Objective C and Java
and all of these work together to help
you write code faster the framework
api's armature they've been around for a
long time and they've been designed from
the ground up with an object-oriented
approach in mind besides this they
leverage objective-c and the dynamic
runtime that it provides in a few
different ways objective-c and cocoa
code are weakly typed and so they allow
you to reuse more code because the code
you write will tend to be more generic
right out of the box in addition there
aren't there isn't a need for such
complexities as templates often a source
of confusion in C++ objective
see in a dynamic runtime also enable you
to create more effective shared
frameworks you don't have application
binary interface issues like you might
with other languages and you can extend
base classes without breaking
applications that depend on them and
thirdly we have this concept of
categories this is a language feature
which is very powerful you can add
methods to existing classes without
having the source code to them so for
example if you write say a program
that's a million lines of code and you
use our Unicode compliance string class
NS string throughout that code and then
discovered is one more method you wish
all strings in your program could take
advantage of you don't have to go and
replace your use of NS string with a
subclass simply add a category to NF
string and add that method very powerful
and all of this adds up to help you be
faster to market with your products you
can spend your time developing your
applications unique features instead of
reinventing the wheel and enables you to
write less code Coco keeps simple things
simple and makes complex things possible
it LO it raises the baseline for the
code that you write so you're writing
your applications unique portion in
addition your application can
automatically get new features over time
without recompiling or reshipping thanks
to the dynamic nature of objective-c and
the cocoa runtime your application can
take advantage of features as soon as
they appear in the operating system they
just automatically show up
and so we've been talking a lot about
the languages that are used up to this
point so I want to focus in on that just
a bit there are really two languages
that are supported for croaker
development objective c and java i'm
sure you you all know what java is so
i'll focus on objective-c here it's
actually a language that was invented in
the eighth in the 80s and is today a
standard part of the free software
foundation's GCC distribution and it's a
very dynamic language and along with
Java provides the support that cocoa
needs in addition if you're writing in
Cocoa you can plug in your carbon C or
C++ code right into these languages or
if you have UNIX code they integrate in
as well so that still leaves the
question open which language should you
use and really the answer is use the
language that fits your needs you may
find that you have existing Java code or
libraries that you want to take
advantage of or you may have code out of
the community that you want to take
advantage of if so you may find that
writing your application in Java using
the cocoa api's is the best road for you
on the other hand most developers find
that the faster runtime performance and
lower memory overhead of objective-c
make it the language of choice will
focus in on objective-c today with that
I'd like to turn things over to my
associate Tina to talk a little bit
about some of the details of how you
actually go about programming and coca
thank you very much Matt once again my
name is Tina flung and I am one of the
hooker frameworks engineers so basically
what I'll be talking to you about in
this section is three things first we're
going to cover some of the fundamentals
of objective-c syntax then we're going
to go over some of the various cocoa
design patterns and finally we're going
to talk about some of the various
developer tools we have for you on the
system so in order to illustrate some of
these concepts we're going to take a
look at this simple
to do this example and in particular
we're going to focus on adding a
preference of pain to this to-do list
and for those of you interested later on
to the talk mass going to come back on
stage and actually show you how you can
integrate this preferences pane into a
carbon application so let's just jump
right in and take a look at Objective C
syntax here's a copy of valid
objective-c code here is a function that
converts from Celsius to Fahrenheit and
it probably looks very familiar to a lot
of you well that is because Objective C
is a strict superset of ansi c so all
your old auntie c code from your old
apps is still valid objective-c code so
you don't always have to look at
Objective C 3 answers however a deck to
see does provide a few additions on top
of an TC the first is we want to
objective-c to be an object or have an
object oriented there on top of 60 so
this included having new syntax to
define new types of classes and to
actually deal with those different
objects and most importantly one of the
ability to send messages to these
objects in Objective C one of the very
unique things about it is we pass
messages to objects rather than invoke
methods and this means that the actual
code that's executed is determined at
runtime so in order to do this we needed
new syntax to send messages and we
needed run types runtime support for
these messages and finally we needed
various data types for these messages so
let's just jump right in and look at
some messaging syntax so for those of
you familiar with any other object or
any language like Java or C++ you might
be used to a method call looking
something like this so here you're
sending a do something method to my
object well an objective c each message
begins an end
with a set of square brackets to the
right of the opening bracket you have
the receiving object and to the right of
that you'll see the do something message
so here is a complete objective-c
message now you can also have a message
that takes any number of arguments so in
a traditional optical anguish you would
have something like this which is an
ordered list of arguments now an
objective-c we have something new that
we refer to as name parameters so it
looks something like this you begin once
again with your receiving objects and
then to the right of that we're going to
have the do with this colon and this
colon message the cones are part of the
message name and to the right of that
you will see the various parameters that
you pass in and you have one parameter
to the right of each colon so this is
how you pass messages and objective-c
now so leave me some of you may wonder
why would we want to do this name
parameters won't store this generally
just improve the readability of your
code in this case it's pretty obvious
the two objects that you're trying to
send but you can imagine a case where
you have in this initializer that you're
taking in 10 different arguments now
granted the java method of just having
10 arguments in the list might seem
shorter however you can imagine that you
might start confusing which argument
goes where and having the name
parameters can actually be very useful
so let's take a look once again at these
messages but let's focus particularly on
the parts in orange so the part of the
message independent of the parameters is
referred to as the selector of the
message and you can actually
programmatically access this part of the
message using what we call the add
selector syntax named a wonder why might
I want this well this allows you to
access a message independent of the
receiving objects
tight and what makes us really powerful
is the fact that at runtime and compile
time the type of the receiving object
does not necessarily have to be known
and this is because objective-c is a
dynamically typed language the type of
the object is actually determined at
runtime and so we have the scenario i D
object that could represent any object
type and you're actually allowed to
query the object at runtime and ask it
what class it is and what methods are
available to it and so you can actually
use this message the response to select
a message in order to find out first if
an object does actually accept this
message and then if it does send it the
message so now that we've looked at some
of the specifics of objective-c syntax
let's step back a second and take a look
at some generic cocoa design patterns
and the first one that we want to talk
about right now is memory management so
Coco uses something called reference
counting basically when you retain an
object this increments your reference
count when you release the object you
decrement the count and we have
something called auto releasing that
decremented at some later point and an
object is freed whenever the reference
count drops to zero and so there are a
few rules to the memory management
contract the first being only creation
methods and this is in it copy and you
will actually return retained objects
and in all other cases the receiver is
responsible for retaining the object
when the first gets it and releasing it
when it's done and once again an object
is deallocated once the retain count
drops to then the remaining references
and we can't retain count drops to zero
so now we're going to look at another
Coco specific design pattern and this is
actually going to deal with
communicating
with the interface so you can imagine
that in your interface you have some
element in the UI and you want it to
basically effect change on the
application so first we're talking about
that we use something called target
action and so you can imagine you have a
button in your application and the user
clicks this button so what happens well
essentially this sends a some sort of
button clicks action message to whatever
you're controlling object in your
application is and we refer to this
controlling object as the outlet of the
action now in converse what if you have
your application and you wanted to
actually drive the UI and update changes
in your interface well to do this we use
something referred to as outlets so here
we have the same controlling object as
before in your application but you also
have some sort of text field and you
interface and essentially you want your
controlling object to pass some sort of
message to this text field and say steps
the text field value to be whatever the
string is and in order to do this you
basically need a handle from your
controlling objects that ties it to the
text field and so in order to do this
you set the text field as a controlling
objects outlet so for many of you this
whole communication with the interface
may seem very reminiscent of something a
general software design pattern called
Model View controller so for those of
you who don't have ever heard of this
before multiple you controller
essentially divided the application into
three layers at the very bottom you have
the model which is what stores and
manipulates all your user data at the
very top of it you would have the view
which is everything the user see
interacts with and somewhere in the
middle is actually this controller which
is the glue that ties the model and the
view together and so to tie the fact
with some of the earlier the early
design patterns of target action and
outlets I take a look once again at our
button so the button is part of the view
and when the button is clicked it sends
the action message but that object that
it would send the message to is referred
to as the controlling object so that is
your controller and this controller in
turn would send a message to the model
and tell to update so in this case it
would tell the model while creating new
folder and this is really important
because as now you have the user
interface isolated from the view and so
you can make changes to your interface
and not have to modify user interface
and not follow your modify your model in
return and vice versa and most of the
application specific code is now
isolated in the controller object and
you can imagine the reverse of this is
if something changes in the model and
that would cause a message to be sent to
your controlling object and then that
would cause the you I'd update through
the use of an outlet and in order to do
this we actually have is really neat
developer tool called in a base builder
interface builder essentially allows you
to have this easy quick drag and drop
graphical manipulation of your UI and so
it's very easy to develop your user
interface to your application rather
than unlike most other interface tools
that essentially just write you out some
source code to represent your view what
interface builders does is actually very
unique it archives live objects and also
archives their connections and these
connections are the target action
connections and the outlet connections
that we just referred to
and finally it's completely integrated
with cocoa from the day that Coco was
built essentially we had interface
builder and the two of them are built up
together so it's not like we had this
old developer tool we have this new like
builder that just kind of does the same
thing that writes more code it's really
there to help you and make coding easier
for you so in order to show you some of
we want this now show you some of the
coolness of interface builder so
interface builder creates and archives
your files in this nib format and so
here's my project with my to-do list and
I'm going to take a look at that file so
here is basically a view of what is in
your nib file and so you have your main
window and various other properties in
it that will go into in a little bit up
here you'll see that there is your a
palette of controls that you have
available to you and finally we have the
inspector window which allows you to see
the different properties of your
controls and so if you're trying to
build something like this preferences
pane you can start with like over here
we have a text view and you can just
drag it over here and notice that these
blue grid lines drop up well these are
basically guidelines to help you meet
the aqua specifications so even if you
have no idea how does how to design a
few that meets apples criteria all you
can do is follow these lines and you can
make an app that looks like all other
macs off apps and so now we can do the
same by dragging out a check box and
over here in the inspector window you
can see there's a title and we can
you can set the title to be from that
Ike show completed items and you can
drag it out to be whatever size you want
you can just drag out some radio buttons
and you can see here that you can change
a number of rows and adjust it once
again and notice how you do the Aqua
guidelines they get to address is facing
and you click on each of the items you
can set the values and so we'll make
that one red and we can make that one
blue and we'll make this box and finally
we'll add a button to the bottom and
we'll tell this one to clear the to-do
list and once again notice how easy it
is to adjust your UI and so since
interface builder actually deals with
live objects you can do something that
we've referred to from the file menu you
can choose test interface and this
actually lets you play with the
interface as it would really work so you
can see how the checkbox is checked and
the radio buttons click and click on
various buttons so now that we have our
UI we need a controller object and
interface builder actually allows you to
do that so an undead File Viewer here we
have a tab that shows clap the tab
labeled classes and this essentially has
all the objects that that nib file knows
about so in this case is all of that kit
objects and any objects that you
specifically tell the nib about so we're
going to subclass nsobject because
that's the most basic of the objects and
we hit enter it gives you a subclass and
we can name it
controller and here's a fresh controller
object and I that shouldn't already be
created but essentially here you have
for your call my object here since I've
apparently mess on my demo here and have
trust control our victory instantiated
so let's say you have an object called
my object and in the inspector window
you can see that you can create outlets
and actions right here in interface
builder so let's add some outlets so we
want an outlet to represent the color
buttons and we can add an outlet for the
show completed button and finally we can
add an outlet for the title field and
you can do the same for actions so we'll
have an action do clear the list and
we'll have an action to set the color
and finally an action to set whether or
not you want to show completed items so
once you have that then you can create
the files for have interface builder
create the outlines for outline template
for these files so we look at my object
and in the classes menu you can tell it
to create files for my object and here
I'll tell you you can choose to create
both the header and the implementation
file and tell it put it in my project
now if you go back to project builder
and look at it you'll see that it's
created these two files for you so you
take a look at them notice that no
behavioral source code is generated for
you and the advantage of this is really
that once you create your view and your
model you can actually just change the
positionings of your view
and change the way it behaves and not
have to change your code necessarily and
the same happens for your head header
files and so let's go back to interface
builder and so each nib file has to be
loaded by some object of a particular
class and by default its NF application
but you can actually set this an
interface builder through the files
owner so every nib file has a file's
owner and you can actually inventor in
the inspector you can tell it sure made
the custom class and as you can see this
is NS application well in this case
we're going to set to be my object and
here it gives you a warning saying all
yet that you're changing the files owner
for men s application that's okay so now
you have a way to essentially hook up
your controller to your view so the way
that that's done and once again remember
the slide with the target in action you
basically want to connect your control
your controller to the items the
controls in your UI and so for instance
let's take a look at this clear to do
list button and you essentially want
that button to send an action to the
controller object so you would hit ctrl
and drag from that button to the
controller object and you can say clear
that that's action I want to happen and
you stay connect and you can do the same
for the various buttons here thank you
tell the color button well I want this
to set my color and the same with this
checkbox control drag from that to the
target which is your controller object
and tell it to set the show completed
now again the converse of that is what
happens when you want your controller
object to have access to your UI
elements well you want your control
object to be able to send messages to
controls in your UI so you a control
drag in that direction so you control
drags from the controlling object to in
almond it controlling the UI and you
could say that is my set completed
button to set that outlet and you can do
the same for the color buttons and will
want to set the title field and that's
basically it's simple how easy it is to
hook up from your UI to your controller
object so for sake of time we're
actually going to jump to a version that
is already we've polished up the UI for
you a bit and we have actual controller
code implemented because obviously when
that action message gets sent something
has to happen and we're going to build
and run that for you and high this and
so here you can see if I go to file the
demo and open up the preferences let's
first create a few items like feed the
dogs and call mom something
and go get my lid on the Himalayas so
you have these items and let's say that
I remember to feed my dog and notice
here that I can tell it whether or not I
want it to show the completed button to
complete a task and change the color of
that and I can also clear the list I
want so there's one more thing we want
to do here and that is we want to be
able to type in the to do list title and
actually have it update in the to do
list well using that target action
pattern we can have some sort of apply
button that will tell the title list to
be set but there's a better way to do
that because we want to have an actual
live update from the preferences pane to
the actual to-do list and let's actually
go back to slide here in order to do
this we use link virgenes delegation and
this essentially allows an object to
appoint another object to be its
delegate so what a delegation well let's
think of it in a non-coding fashion so
yeah my boss and he's about to go on
vacation so he essentially appoints Matt
to be he says Matt I want you to be my
delicate so all messages that would go
to my boss will now go to Matt and so
now I'm like I want to go on vacation
and normally I would say hey can I
deffenbaugh can I go on vacation but
obviously he's not there so that message
now gets forwarded to his delegate who
in this case of matt and matt says
Shirin tina's go on vacation because
he's a good buddy of mine so this is
delegation so in activity and Coco we
actually have two basic behaviors that
delicate can perform the first is the
delicate can actually modify the default
behavior of the object and the second is
it can just be a receiver of
notifications so whenever an
object perform certain event the
delicate can just be noted by that this
event has occurred and so there are
several classes in act hit that have a
delegate outlets and when you set this
delegate outlet its entity the dojo will
be asked Hayes okay if I do this
particular action or the delicate could
just be notified that something has
happened and so for instance if you have
some sort of text field in this case
zelig you may be asked questions like is
this about about a ballad value for my
text field and for instance if you want
the text field to only accept numeric
values you can adjust the behavior
accordingly and another instance you can
just want to know well the text is about
to change and so the Dalek needs be
notified that the tax is about to be
changed and so essentially what this
allows is you can customize a object
without having subclasses so let's take
a look at how this works
so we're going to go back to a nib file
close with one and if you look at the
text the text fuel text view sorry
notice that it has an outlet called a
delegate outlet and so you can control
drag from that text view to the files
owner which is the press controller and
say make this my delegates and Udall get
set and so let's take a look at the code
that was necessary that's necessary to
make this change just take a look at the
press controller and what you want to
see here is the text did change delegate
method and essentially this is called
whenever the text changes and so here
we're going to tell the title field set
its font and set a string value and then
update the window and so now if we build
and run this
that's too many windows here so now if
you open the press pain when you change
types of text so I guess ibises you know
like my shopping list or something
notice that it live updates on your
to-do list and so a delegation is a very
powerful basic design pattern that Coco
allows you to do so there's one more
thing we want to be able to set the spot
of this to-do list and in order in order
to do this we need something that's
slightly different from this general
target action design pattern that we've
shown you up to this point and the main
reason for this is well a font panel
that you've probably seen for on OS 10
it can affect any number of views and so
its targets essentially determined on
the fly and so let's go back to slides
here and so in order to do this we use
something called the responder chain and
this allows us to determine what our
target is at any given moment and so the
respondus chain is essentially an
ordered list of objects that response to
user events or event to general and this
means that the target can be determined
at runtime in particular the first
object in this responder chain is
referred to as the first responder and
that's the actual object that were
received the event and so the respondent
chain is used in various places for
example cut copy and paste you click on
one text view and you say copy this text
and then you click on some other text
views from where else and you say paste
and so first responder loud but to check
what your main window is and determine
that new view is where I want the tax be
pasted also for menu item validation you
can validate many items depending
on where you're clicking and once again
you can use this for things such as font
panel and color panel and so let's show
you how that works so essentially right
now we're going to look at this other
nib file which I haven't shown you up to
this point but this essentially is the
main window of your to-do list
application clean out some of these
windows so this is the main view of your
application and in here though you'll
notice that there's this main menu and
this is essentially the menu bar that is
associated with the application and so
in your controls palette you have a
bunch of menus they can add to it and in
this case we're an ad as font menu and
though it's good to have a whole like a
ray of thing to get show the font panel
bold underlining things like that so
that's Avis and give it a run so notice
here that we now have a font menu in our
to do this application and you can just
say show font and font panel comes up
and so let's take a look at our
preferences and so here's like I
wouldn't say i want to emphasize that's
my list so i can make the font really
big probably a little bit too big Thank
it so you smaller and I have no idea of
what's happening this font sizes but
let's make it all 36.5 but sent to HD
here is that whatever view I've
currently selected you can now have the
font panel effect that particular view
and that's because this text view here
is the first responder
can go back to slide please and so
that's essentially cocoa in a nutshell
you know we don't expect you to be
full-fledged cocoa programmers by any
means at this point but hopefully you
have a general understanding of you know
the various powers that cocoa provides
and you will understand enough about
cocos impacts to hopefully participate
in some of the other talks that we have
available for you later this week in the
cocoa track and learn more about cocoa
so I'm now going to pass this back to
Matt to talk to you about how to
integrate this preferences pane in a
carbon context thanks Tina so this next
part is actually one of my favorite
parts about cocoa it's something I think
is really cool and that's how you can
integrate cocoa with other technologies
now you can certainly use cocoa to build
full-featured applications with mac OS
10 but a relatively newer development is
the ability to integrate cocoa with
carbon and even unix code you can mix
cocoa code right in with your existing c
and c++ code and we're going to show you
how to do that today and why you would
want to do it and then walk you through
a demo so why mix carbon cocoa well
there are three basic reasons first of
all you may have existing code that you
want to leverage and this is typically
the most common reason for integrating
carbon and cocoa there's an app that
exists in carbon and you want to add
some functionality using cocoa the
second reason why you might want to do
this is if you're writing a common
service that perhaps applications
written with carbon or cocoa could take
advantage of for example the address
book framework on Mac OS 10 actually
bends api's in both see and objective-c
for both of these frameworks
and finally there may be various pieces
of alternative functionality that you
find easiest to access through one
framework or the other and so you want
to integrate those in one application so
what does this involve well it basically
involves just adding the code whether
it's carbon or cocoa to the same source
file go ahead and include carbon odd H
or cocoa h we're both the other thing
you'll want to do if you're including
objective-c code in your existing C or
C++ source files is change the file
extension to dot M to indicate that
objective-c code will be included if
you're also including C++ in the same
code you'll want to change the extension
to dot mm to indicate that our other
compiler the objective c++ compiler
should be used this this technology
allows all languages to coexist in the
same source file and be compiled and if
you have source files that you expect to
be compiled on other platforms where
perhaps objective-c doesn't exist and
yet you want the code to just kind of go
through smoothly you can use the pound
if def abdi syntax and that basically
says if this is defined then we're
compiling using a compiler that
understands objective-c and you can also
force fit with the right compiler to be
used on files without having to change
the file extension if you choose so
besides compiler support and source code
support the operating system has support
from mixing carbon cocoa as well vos
itself provides what we call toll-free
bridging of common data types between
foundation in cocoa and the core
foundation framework such data types as
CF string and NF string can be simply
cast to one one to the other in fact
they're the same object and there are
actually two main levels of integration
between carbon and cocoa that we support
priors and macalistair version 10.2
we supported non GUI integration so you
could have for example carbon or causal
code that dealt with the file system and
then have the interface be designed
using the other set of api's we change
this in machynlleth 10 version 10.2 we
now support mixing the graphical user
interface on a per window basis which
means you can have a carbon window in a
Coco applications or a Coco window in a
carbon application and the one thing to
keep in mind is that you'll want to call
this one function NS application load to
initialize Coco if you're using cocoa
within a carbon application so we've got
a kind of nifty little demo here to show
you guys you may be familiar with the
glyph of three code base it's been
around since nineteen eighty eight
fifteen years was written by John
Calhoun of glider Fame for the Macintosh
and today it's fully carbonized and
lives on sourceforge net where the
source can be downloaded so let's show
you this game we can switch to the demo
machine Tina's going to create the start
a new game it's basically a joust clone
and the source code is available you
basically sail around on your bird
trying to knock sphinxes out of the air
and i guess some sort of an Egyptian
pyramid and she's going to try and get
on the high score list here this code
with Pascal black and white back in the
day and then it was retrofitted to see
in color and barest technologies were
added over the years he was finally
carbonized I remember playing this on my
performance 633 megahertz back in the
day so she's dive-bombing and into the
lava it was actually a a mummy hand on
the left hand side that comes up and
grabs you if you're not effective so so
here's a high score dialogue and
actually given the history of this code
base and it's pascal string and the
string usage all we can enter english at
this point
we're going to retrofit this to be able
to support more languages we could do
this in carbon we're going to do it in
cocoa we could imagine we want to add
other technologies to this using cocoa
down the road so she enters her name we
get the high score list she's number one
and now let's take a look at some of the
architectural changes we're going to
make to this so we could go back to
slides please there are two things we're
going to change we're going to add a
preference panel you using cocoa similar
to what Tina Illustrated earlier and
then we're also going to change the high
school name entry dialogue to be Coco so
before the integration what does this
code look like it's basically c6 source
files it's straight see it includes a
kind of basic sprite engine and sound
engine it was something that John
Calhoun actually used to learn how to
program through the years as he was
working all and glider for the Mac it
includes high score list and other
preference code it saves all these out
manually using its own data structure
which is end stores out we're going to
change that a little bit we're going to
go more towards the
model-view-controller approach the Tina
illustrated and access some of this
through a preferences model instead of
directly to distract and we've taken the
liberty of making some carbon
modifications to the code before
stepping in with cocoa basically what
we've done is replaced the pascal
strings with CF strings to finally
support Unicode in other languages this
enables easy data transfer to NF strings
as I mentioned earlier we can simply
cast the CF string to an NS string or
vice versa
so let's go look at the other Coco
changes we've made to discard the first
thing to notice looking at the files
here is that we've changed the extension
on main from name see two main dot M
this indicates that will be including
objective-c source in this file and if
we open it up we can look at the code
that actually handles preferences when
we load preferences we pound ifdef aab
see this code just in case we would run
a compile it on another platform I'm not
quite sure why we want to do that here
but it illustrates the point and what we
can see is that we're calling NS
application load to initialize cocoa and
finally we create a new preps model
object and initialize it with the
Preferences data structure that glyph of
three natively uses so that loads in
preferences when the game starts up then
when the Preferences menu item is chosen
in the game we need to actually go and
create this new Koko preferences panel
once again we carefully conditional eyes
our code and then what we call is NS
bundles load nib named method and this
actually takes a nib name as its
parameter and basically loads up our
preference panel which then displays on
the screen let's take a look at that
preferences panel now here it is this
should look very similar to what Tina
showed earlier and we've made just a
couple of changes you'll notice a show
fonts button here and what that's for is
basically we can't add a font menu to a
carbon resource manager based menu bar
so we've added a font button here to
actually show that font now other than
that it's basically the same from a
carbon perspective we don't have to know
anything about how checkboxes and radio
buttons are implemented all we need to
do is write the code that gets run when
these controls are triggered
speaking of which let's go look at the
set sound method now this code is fairly
straightforward it basically gets the
state of a check box and if it's on it
turns on the sound if it's off it turns
off the sound and then it synchronizes
the model pretty simple now let's take a
look at the other nib in this game that
we've added which is our new high score
entry dialogue and it looks very similar
to the carbon one we showed before as it
should so let's take a look at the code
that loads this nib
once again we allocate a new controller
object and then call load nib named to
actually load this high score entry
dialogue so those are the changes we've
made let's run the app now and see how
this new retro fitted version of what 43
works so we actually have our
preferences panel now which we can pull
up so this is a Coco window running in a
carbon application that's been around
for 15 years we can go and change the
name of the high score list we can pick
a different font while we're at it let's
pick something a little more Egyptian
like papyrus or something make a little
bigger maybe and sound is on so okay
good let's close those windows and
actually Tina will go and play a new
game again download this sourceforge
play it almost like I was pretty good at
once she's made it to the high score
list annoyed take care
[Music]
she has died at grew things happen and
now she can enter her name but but we're
not limited to English anymore we can
actually have Tina go enter her name in
Chinese thanks to the power of
end-to-end unicode support through
nsstring and see up string her name is
displayed now in Chinese let's go back
to slides so what have we learned today
well we've learned that thanks to the
object-oriented dynamic frameworks in
cocoa your development can be faster
we've also learned that the cocoa
fundamentals really aren't that hard to
learn an objective-c is is really not
that bad and finally we've learned that
you can integrate cocoa into your
existing applications or build whole new
applications using cocoa this point I'll
turn things back over to Jason for wrap
up thank you
pretty cool stuff huh exogenous I think
we have some really really cool stuff
out there for you guys take a look at
here's a road map of some of the
sections of it coming up I've
highlighted all of the cocoa sessions
take a look at the cocoa updates see
some of the really cool stuff we've
added you may be new to cocoa but we've
done some great stuff with it we've had
some really cool user interface stuff
that we've added also some nice 2d
drawing techniques for those of you who
are into the graphics out of stuff we
actually don't have a cocoa performance
techniques anymore we replaced that with
an in-depth session for the cocoa user
interface some nice tips and tricks to
show you how we've taken care of some of
the iTunes ichat kind of stuff you see
the really nice things that Apple's done
with its applications and other people
have done with their applications and of
course the feedback forum if you like
cocoa and I think you will after this
week be sure to come by and say how much
you like it and if you don't you should
say that also so we can make it better
and if you want to send us some feedback
John girl NZ's the manager of software
evangelism he is the person who's
responsible for making sure the stuff
that you guys need in cocoa gets into
cocoa so make sure you send an email
galamsey at apple com if you've got
feedback on the api's you start using
them that feedback should go to cocoa
feedback at grupo apple com goes
directly to the engineering manager
responsible for building cocoa and if
you've got technical questions you
should send it to my group develop a
technical support when we will help you
as best as we can to answer those
questions without a lot of great
resources on the web in fact you can go
up there for some more information we've
got documentation on cocoa the entire
API set is up there tutorial sample code
you can download lots of really great
stuff some really good books are out
there also our partner partnership with
O'Reilly which has benefited all of us
by giving you a free book if you haven't
already gone and gotten it I'd go down
and get it it's the cocoa nut shells the
fantastic introduction and intermediate
level books for cocoa developers
absolutely go pick that up it's in your
bag
get that and there's a couple other
things also so in wrap-up thank you all
for being here I appreciate your time
I'd love to have you stick around for
questions but but we don't have an
official QA but we'll be happy to answer
any questions for you now or in the DTS
lounge on the first