Today I want to talk about method call in Objective-C.
One of the strangest things I came across when learning Objective-C was the method call syntax.
In C, we use something like this:
ObjectName->MethodName(parameter1, parameter2);
In Objective-C, the methods are called using messages, when we want to invoke a object method we send it a message.
Let's look at an example:
Suppose we have a NSTextField object named TextBox, to change its text we need to call its setStringValue method. In fact the method does not belong to the NSTextField, it is inherited from the NSControl (you can check the inheritances on the Mac OS X Reference Library).
So we have:
[TextBox setStringValue:@"New Text"];
As you can see we enclose the object name, method name and it's parameters in square brackets.
TextBox is the object name and setStringValue is the method name, but there is a little detail regarding the method name, setStringValue, besides identifying the method also identifies the first parameter of the method. The value of the parameter follows the parameter name after a colon. For methods with only one parameter we hardly notice this, but when a method has at least two parameters we really need to know what is going on.
Lets look at another example. Now suppose we have a calculator object and we want to call a method that receives a value and the operator. So if we want to add 25 to the current value we would call the method that we will call computeValue so to add a value we write something like this:
[Calculator computeValue:25 operator:Add];
On this example we can notice that the method name also refers to the parameter name.
Analyzing the code we can identify the object "Calculator", the method "computeValue" that also refers to the first parameter, and the second parameter "operator".
It's all for today. I hope this helped to clarify how the method call works.
Wednesday, February 16, 2011
Tuesday, February 1, 2011
New tutorial
I just add a new tutorial for those who are giving the first steps on Xcode. Look for "First App Tutorial" on the side bar.
This is a simple tutorial that will introduce the basics of using Xcode and Interface Builder. You will also learn how to connect the interface with your code as well as some basic concepts of Objective-C.
I hope it helps. Any comments or suggestions are welcome.
This is a simple tutorial that will introduce the basics of using Xcode and Interface Builder. You will also learn how to connect the interface with your code as well as some basic concepts of Objective-C.
I hope it helps. Any comments or suggestions are welcome.
Saturday, January 29, 2011
Before we start
If you are reading from my first port you should be thinking "When the hell do I start coding?", at least that was what i thought when reading some tutorials.
Before we put our fingers on the keyboard and writing some code it is important that we understand how the IDE works.
There are two tools that you need to get familiar with: Xcode and Interface Builder.
Very briefly, Xcode is where you write your code and Interface Builder allows you to design the interface of you application. We will get into more detail on these tools later.
These tools work together in order to define how the interface will be linked to your code.
Basically, Interface Builder provides two things to you application, actions, for instance, when you press a button you are invoking a action, and outlets. The outlets are objects with which your program can interact to provide feedback.
Take this example, if you were writing a program that woud execute a task when you press a button updating a progress bar to indicate the amount of the task that has been completed. In this example, the button provides an action (when you press it) and the progress bas is the outlet that gives you feedback on the completion of the task.
First things first
So, one of the first things that some one asks when learning a new programming language is perhaps, "What should I already know before I start?".
Something that you should already know before starting is C.
Other thing that it would be useful is to have some knowledge about object oriented programming.
The language that we will use is called "Objective-C", it is a language that is derived from C but if you look at programs written on Objective-C you will find very little resemblances with C.
Maybe the second question you will ask is "What tools do I need?".
If you are learning to program for Mac OSX you probably already have all the tools you need.
You will need a Mac (obviously) and and IDE (Integrated Development Environment) called Xcode.
I'm not sure if the Xcode already comes installed when you buy a Mac but it is available on the installation DVD although I recommend that you download the latest version from the Apple website. (You will have to register as a developer)
At the time I'm writing this post the latest version is 3.2.5 but there are already announcements for the upcoming version 4, from what I've read, this version brings major improvements compared with the current version.
Welcome to my blog
Hi.
My name is Bruno and first I want to explain you why I decided to create this Blog.
For many years I've heard that everyone who buys a Mac never goes back to the PC again.
I always was a bit sceptic about this and kept working on my PC, until, last year I decided to buy one. My goal was to develop applications for my iPhone.
I've already programmed on several languages, so I thought that shouldn't be hard to learn a new programming language.
I've searched for simple tutorials to get me started and was on the YouTube that I could find a tutorial for my first program. Despite being able to write a working program, I could not understand most of what I have done, so I tried to get the knowledge I needed to understand what was going on.
I found out that it was easier to understand the concepts os the language if I try to write some Mac OSX programs first. So I'm not yet able to write an iPhone program, I'm first trying to understand the language itself.
I'm still learning so it's very likely that you will find errors on my posts.
The main idea behind this blog is to exchange and clarify doubts from programmers who are giving the first steps on Mac OSX programming. Here I will post my discoveries, and my own doubts as I learn.
Feel free to post comments regarding the subjet or just to tell me that my grammar sucks (I'm not a native english speaker so be patient).
Subscribe to:
Posts (Atom)