iOS Development

Course No. 10-152-139

Chapter 13 — Objects

What’s Objective-C?

  • At its base, Objective-C is C. Then extensions were added to turn C into an Object-Oriented language like Java, Ruby, or C#. However, it is older than those language. A little known fact is that many parts of Java were patterned after Objective-C. And since .NET was designed to be a modern copy of Java, you could say that Objective-C has influenced a lot of modern languages and coding practices.
  • Here’s the Wikipedia link to the Objective-C page. It has a very interesting history and you might enjoy reading about it.
  • This first part of Objective-C we’ll learn about is the @ sign.
  • We will see that a lot in our code. This is called a compiler directive. It tells the compiler that a piece of Objective-C code is next.

What’s Object-Orientation?

  • Let’s start at the very beginning. The Objective-C way of Object-Orientation starts with a C struct. Here it is.
  • This struct contains pointers to other structs. It looks a little more complicated than the structs we’ve used. However, it is still just a C struct.
  • What’s a Class?
    • A Class is a way to define a collection of variables and functions that work together.
  • What’s an Object?
    • An Object is a single struct that contains data and has functions that can work with the data. It is automatically created on the Heap.
  • What’s a Method?
    • A Method is a function that is connected to an Object.

Creating your first object

  • Switching to an Objective-C application!
  • From now on when we start a new application we have to switch to Type: Foundation.


  • The first change we can see is that the main file is now named main.m. That’s the file extension for Objective-C code.


  • The starting code is different, too.
  • Let’s look at the details of a few of these lines.
    • The Foundation here is a larger set of functions than just C. It includes the stdio.h that we are used to as well as the basic Objective-C classes and functions.
    • Here’s one of the @ signs. An @autoreleasepool is part of Objective-C‘s memory management system. We’ll learn more about that later in the semester and in future semesters.
    • This next line has a function that starts with NS. It stands for NeXTStep. The company where Cocoa, Cocoa Touch, Mac OS X, and iOS got their start was called NeXT. NeXTStep was the development environment that was created at NeXT. It grew up into Xcode and the iOS development we are learning.
    • That same line also shows another piece of Objective-C syntax. This is an Objective-C string:
    • Objective-C can also use C strings:
    • The @ is used so the compiler can tell which is which.

Create an Object!

  • Here’s your first object.
    • Ta Da! Um, what?
  • More detail you say? OK, coming right up.
    • The first word in this line is the Class. You will be writing your own classes soon.
    • The next part should be clear to you. It’s a pointer to the type NSDate. (A class is a custom type, just like a struct.)
    • Then our old friend the assignment operator. It works just like we’re used to it working.
    • Then we have a set of Square Brackets. They are used to define the beginning and ending of sending a message.
    • Next, we have a receiver of the message. More about this in a bit.
    • Finally, we have the message we are sending to the receiver.
  • Now let’s spend some time looking at this in even more detail!

Methods and messages

This is one of the most confusing parts of beginning iOS Development.

A Simple Method

  • What could be confusing about methods?

In JavaScript

  • A simple function
  • How it’s used.
  • Pretty straight forward, I think most of us have seen something like this.

In Java

  • Here’s a simple method
  • And here’s how it’s used
  • Makes perfect sense to most of us, I think. (But, which one is the width? Are we sure?)
  • Does Objective-C do it significantly differently?
  • Yes

In VB.NET

  • VB.NET is supposed to be very different than java.
  • They still look very similar in structure.
  • And here’s how to call the method

In PHP

  • Now translated to PHP
  • Usage
  • This looks close enough to the above code to be cousins.
    • Sure does!

Now for Objective-C

  • In Objective-C we need public methods to be declared in two files.
  • In the header file that ends in .h.
    • Uh, what?
  • Then in the implementation file that ends in .m.
    • You’re losing me!
  • And now, send the receiver the message.
  • Where’s the name of the method?
  • Don’t you mean to call the method? What do you mean by “sending a message”?
  • And what’s a “receiver”?
  • And what’s with the goofy square brackets?
  • OK, now I’m confused!

No Parameter Methods

  • Let’s start explaining this with a method with no parameters and a void return.
  • First, you have to put code in two files to define a method, the header file and the implementation file.
  • In the header file
  • Details:
    • The minus sign (-) means that this method is an instance method, not a class method. This is like not adding the word static to a method in java. If a plus sign (+) is there then it would be a class method, like adding static to a method in java.
    • The word void means what you think it means, that this method won’t return anything. It has to be in parentheses.
    • You can have a space after the (void), but, most iOS coders won’t do it that way.
    • The word doSomething is the name of the method.
  • In the implementation file.
  • Details
    • We have to have code in both files? Not always, but frequently.
    • Line 3: You have to import the header file into the implementation file. An #import is like the “include” in C that resolves multiple included files automatically.
    • Line 7: This has to be exactly the same as in the header file without the semicolon. Then add the curly braces.
    • Line 8: We’ll be doing a lot of coding inside curly braces. Most of us are used to that, at least.
  • Now the fun part, getting this method to run. But, we don’t say “calling” or even “invoking”.
  • Because of its Smalltalk roots, Objective-C uses the concept of messaging.
  • We don’t call a method on an object, we send the object a message.
  • Since the object is receiving the message, it is called the receiver or the target.

Sending the message

  • To send an object this message:


  • Why the brackets?
  • Because of parameters, we’re getting there.

A Method With a Return

  • Here’s a simple method that returns a pointer to a string.
  • In the header file:
  • In the implementation file
  • Details
    • There’re those two files again. I’m not used to them yet. You’ll get there.
    • Line 04: The - (minus sign) means that this is an instance method.
    • Line 04: Declare the return type to be a pointer to an NSString: (NSString *)
    • Line 07: Create a local string variable and assign it a new NSString. The code @"Hi!" makes a new NSString object.
    • Line 10: Return the pointer. At least we still get to use the word ‘return’!

Getting the Return Value From a Method

  • Here’s how we use this method.

A Method With One Parameter

  • The declaration of a method with one string parameter.


  • The implementation of a method with one string parameter.
  • Sending a message with one parameter.

A Method With Two Parameters

  • Are we ready for this? Maybe…
  • Here’s goes.
  • The Implementation File
  • Let’s stop right here and try to understand what this means. So much of iOS development is based on understanding how this method works and, just as importantly, how it reads.
  • First, here’s the name of the method:
    • setWidth:height:
  • That’s right, the real, full name of Objective-C methods combine all parts before colons, and the colons themselves, into one name. Huh? How did this come to be?
  • Would it make more sense if it looked like this?
  • A parameter is defined with this:
    • :(int)newWidth
    • A colon, followed by a type in parentheses, and then a name
    • What comes before a colon is called a label. Labels are not required by the compiler. However, It is a very strong convention that all parameters MUST have labels.
    • aLabel:(NSString *)aParameterName
  • The Objective-C compiler combines all labels and the colons into the full method name.
  • The two parts of our “set width and height” method:
    • setWidth:(int)newWidth
    • height:(int)newHeight
  • Now pretend to be the compiler and make the full name:
    • setWidth:height:
  • OK, I’m getting it. But, why is it like this?

Why?

  • The reason is seen when we send this message to an object:
  • This reads out loud as:
    • rectangle, set width to 25 and height to 30
  • That is very descriptive! It does exactly what is says. We are telling the rectangle object to set its width and height. The code is very clear and there is no uncertainty as to which parameter is which.
  • Compare sending that message to calling this java method:
  • We have to assume that width comes first. This java method is named well, but there’s nothing in the java language that really enforces this. How about this one?
  • Have you ever seen a method like that? We have to go to the API and look up the method to see what each parameter is supposed to be.
  • And then there are constructors!
  • In Objective-C, we would have the “setDimensions” method call as a message, something like this:
  • The Rubyists are smiling right now! They’re used to code that is so descriptive and expressive. Ruby is a much more modern language than Objective-C that was developed in the 90s as an interpreted scripting language.
  • Objective-C was developed in the mid 80s and is C. It has all the programming power and speed of C while giving us expressive code that almost matches Ruby.
  • Almost? Yes, Objective-C method parameters can’t have their order changed, while Ruby code can. We can’t do this:
  • Objective-C is a descriptive, expressive, elegant, Object-Oriented language that also allows well written apps to run pretty much as fast as the hardware will allow.
  • As many have said, “It’s what C++ should have been.
  • Now let’s look at the full example from the book.
  • More detail!
    • The NSLog function is the Objective-C equivalent to printf.
    • The %@ is an Objective-C format code for NSLog that outputs an object.
    • Here’s a message that returns a value.
    • Now, and message that has a parameter and returns data.

Class methods vs. instance methods

  • When the receiver of a message is the name of a class we call the message a “class method.”
  • This returns a pointer to a new object of the NSDate class. This new object is called an instance.
  • When the receiver is an instance of a class the message is called an “instance method.”