Tip:
Highlight text to annotate it
X
so wearing a jacket seen and object oriented language in most of the code
that we're going to write or use will be contained inside classes we'll create
objects based on those classes
and we'll tell those objects to do in fact we will call methods of those
objects
now vote is joe you a few of the classes that we have available to us because
apple of written them already that might include something like the t_v_ audio
player class
this is an existing class that has methods like
play and pause and stop
and the kind of things you can probably guess in audio player would happen we
can create an object of this class and call those methods
but even an estranged our basic string object in objective c has plenty of
methods it has dozens and if we
have and an astronaut jet we have methods like
lower case string and uppercase strength
and has prefix and has suffix
we can do all sorts of things with that string object
when it comes time to creating your own classes we can right-wing se lekar bank
account class and we can have methods like deposit and withdrawn open and
close
soto we're describing what it is these classes can do now this of course is
common object pointed stuff to the question is well
how do we call these methods in objective c because that is a little
different than most other languages
in most languages you would colon method by using something called dot centex
you'd used the name of the object
then the name of the method
any parentheses that would say
does this method take any arguments and
you know there's is not going to work an objective c we don't use
dot syntax an objective c recall methods
we use square brackets
square brackets is one of those indicators to the compiler that this is
an objective c not
regular straight c
so in this case were sending a message here
with sending the message some method to the object my object
it's all in closed in the square brackets and there's no dont between the
object name and the method name
now some developers get really weirded out by the square brackets but it is
just basant exchange is a slight change
the concept is the same
let's take it the little step further
what happens if this method returns a value
well if your familiar with don't send tanks should expect to write something
like this
you'd have a receiving variable called
result instead of people to whatever comes back from
my object on some methods
well it's exactly the same an objective c he just replace the dot syntax with
the square brackets and call it that way
but if that method returned a valued this would work just fine
that moving on one more step what happens if you want to pass and argument
into that method
so the method
has been defined with parameters were passing argument into it
welcome dot syntax
you're typically put the argument inside the parentheses
object
method and in parentheses the arguments and the concept is the same an objective
c but instead of the parentheses
we use a column
so it's object name space method name
argument all within the square brackets
and that's how we pass an argument in so that method
no makes up his work it's politically or complex
multiple arguments
because that is the first place where is quite different
and giving the analogy again of the dot syntax way
when you pass in multiple arguments
separated by comma is
inside the parentheses
months not going to work in objective c
the big difference in objective c is that if you have a method the takes
multiple arguments the method name becomes split apart into multiple pieces
the actual name of the method changes
so let me show you an example
this is a real method that's available in objective sees the method
insert string at index more properly it's read as
insert string
cole on at index
and the wayward writer because we'd use the object name myspace my object
then the first part of the method name which is insert string call on and the
first parameter
then a space then the second part of the method name which is pat indexed call on
and the second parameter
so the actual method name is different each argument represents a different
piece of the method name
now what this leads to something that objective c is notorious for
particularly when people in need to know which is a method names that are really
really along
because what this means is any method in the case of multiple parameters
has to be defined to have multiple parts to the name of it
and that leads us to
for example a
real method called replace item in your alco lawn without a mature hopeful on
backup item and colon
options colon
resulting item your alkaline
and yes that doesn't exactly rolled off the tongue
and you'll find in objective c and in some of the classes that apple provide
as ones that are even bigger than that
and i'm not going to attend to read this one
at the big
benefit of this is even though we can give you a lot more coached to read and
you can of course split the single method call across multiple lines to
become more readable
well your coat becomes more understandable international program
because instead of eight smaller method called where you're passing multiple
parameters all squeezed into one set of parentheses
but you don't really know what those parameters are
they're all named hearing your coat it's obvious what they are and that's one of
the benefits of having a really long method names an objective c
and most of the developers i know the dispense sometime with objective c
including myself
we should begin to prefer it this way
because while he makes for felongco writing of code and figuring out what
those methods are
it makes for much more readable and maintain double code after the fact
now the final piece here
we can have nested method calls just as in
dot syntax
where you could instead of passing in an argument directly
you could just put in a call to another function or method
well he can do the same thing in objective c instead of the standard
objective seaway
where i've been using the argument after the cold on
we can had
a nother set of square brackets and nest
one called inside another
the interior want will be evaluated first in this case calling another
method of another object
and the result of that
will be fed into the exterior warning my object some method called
so while the syntax is different and can take a little bit of getting used to
the concepts are exactly the same as in other object oriented languages