Wednesday, April 28, 2010

Quick Objective-C example

In this example we will see how to send multiple parameters to a method and how to create a conscructor method inside a class.


Create a new project in xcode and name it example2.
Now, create a new class named TestClass.

TestClass.h


Two variables are created: testVariable1 and testVariable2.

Two methods are also created.


TestClass.m


main.m


The first method is used to receive two parameters and assign them to their respective variables. The name of the method is extended for it to accommodate various parameters. The full name of this method is " setTestVariable1:and2: ". The part of the name after the first colon could be omitted in the declaration but it is recommended to use it since it gives us some insight about what kind of parameter the function needs to receive.

The " print " method is used to print the results of the two variables.

Let's test this to check that we did everything right.
Compile your project by pressing "Command + B" on Xcode. Press "Command + Shift + R" to open the Console and "Command + R" to Run your application.


Now we'll add a constructor method to this example. 
A constructor method lets you initialize the variables in an object at the same time that the object is initialized.


TestClass.h




TestClass.m



The name of the constructor method is initWithTestVariable1:and2:.This method initializes itselft, and then assigns values to its variables using the same method we used at the beginning of this example(setTestVariable1:and2:).


main.m


Run this again to test it.




The constructor method is used instead of init on the first statement. By doing this, the values of testVariable1 and testVariable2 are assigned in the same statement where the object is initialized.

Starting out with javascript

Well, let's start with Javascript programming!!

In this example we will learn how to declare variables and how to use some simple built-in functions.

For programming in Javascript I'm using xcode too, since it supports syntax highlighting and code completion. You can use any text editor you want and get the same results.


Let's start by creating a new file. Select and empty file and click next.


Now, choose a name for your new file. This file has to have a .html extension.
I name my file example.html.


After this, a new empty window will appear. This is where you will write your html and javascript code.

Write this code on that window:


To declare a variable in javascript we use the word var followed by the variable name. So in " var testVariable1; " we are declaring a variable named "testVariable1".  This, however, is not completely necessary. You could declare a variable just by doing something like " testVariable1 = "Hello World" ". The use of var is recommended, though, because it helps to ensure that your code will be readable.

As you can see, no type is specified for this variable. This is because javascript is a loosely typed language. This means that there is no data type assigned until the variable gets a value.

The " alert(testVariable1); " line is used to display a message box that will display the value that we assigned to testVariable1.

One more thing you should probably know is that the use of a semicolon after every line is not necessary. I recommend that you use it since it gives some style to your code ;)

Now to test this you just need to save your file and double-click it's icon.

A web browser window will open and a message box will be displayed.

Good job! This means your application worked correctly.

Now we will add some different values to testVariable1 and see how the data type changes.
Go back to the code and write this:



The first thing you should notice is that instead of using the alert function that we used in the beginning of this example we are now using the document.write function. The document.write function writes directly to the page instead of opening a message box.

Now, let's take a look at the full document.write statement.

document.write(testVariable1 + " " + typeof(testVariable1)+" <br/>");

The first part after the document.write function is used to display the value of testVariable1.

After that we have ' + " " + '. This is used to write a blank space after the value of testVariable1.

Next, we call the typeof function. This function is used to return the data type of a variable.

To end the statement we have ' +" <br/> " '. This is an html tag and it is used to insert a new line.


All the " testVariable1 = " statements are used to change the value and data type of the testVariable1 variable.

Save your file and open it up in a browser window. The result should look something like this:

As you can see, since we hadn't assigned any value to testVariable1 the first time we wrote it to the page,  it's value appears as undefined. In javascript the data type for an undefined value is also undefined, so the typeof function also printed undefined.

The value "Hello World" was assigned the second time we printed the variable, so the data type changed to string.

The third time the numerical value 32 was assigned, so the data type changed to number.

The fourth time we assigned true to the variable, this time the data type changed to boolean.

The last time we assigned null and the data type changed to object.

It's important to notice that if we use quotation marks when assigning a value to a variable, the data type will always be string.


Monday, April 26, 2010

Creating our first Objective-C program

Ok, first of all, to compile Objective-C code you will need a Mac with Xcode installed. There are also ways of compiling Objective-C without a Mac, by using GNUStep but I'll explain how to do everything on OS X and Xcode.

Well, let's start by creating a new project. Click on the Xcode icon and select "New Project" from the "File" menu.



Since we are going to be using Objective-C, select "Cocoa Application" and click on choose.



Now write the name of your application, I named mine "example".



After doing this, a window with your recently created project will appear. Xcode creates some default files for you, including main.m which is the file that contains the method from where your application will start running.

Since we chose "Cocoa Application" it will create some files that are normally used for managing visual interfaces. These files are called "exampleAppDelegate.m" and "exampleAppDelegate.h". You can delete them if you want.

Now... for this application I will explain how to use variables from other classes. So now we have to create a new file. Right-click or Ctrl-click on the Classes folder and select "Add" and then "New File".




From the menu that opens up go to "Cocoa Class" and then "Objective-C class" and click "Next".



Choose a name for your new file, I named mine "testclass.m". Also, check that the checkbox that says "Also create testclass.h" is checked.



Ok now, let's start programming!!!!
First, do you see those curly brackets on the testclass.h file?


This is where you create variables for your class. So let's start declaring 2 int variables.



As you can see to declare a variable in Objective-C (And also on C) you first specify the type and then the variable name.

The default access level for variables is "protected", but you can specify public or private if you want to.
  • Public means the variable is accessible by every class.
  • Protected means the variable is accessible only by other classes inheriting the class where the variable is declared.
  • Private means the variable is accessible only by the class that declared the variable.


Or at least it should be this way...

Let's try doing something that's normally wrong :)

We will try to access a protected variable directly from the main.m file and see what happens.

First we have to import the required files.



The first line imports the file we created so we can use it inside main.m. The second line imports a standard library from C that contains a method for printing that we will use.

Now, write the following:




The first line after the brackets initializes an object of the testclass class and gives it the name of "test". After doing this, the second line assigns the value "5" to testVariable1 in the test object. The third line prints the value of the test variable.

The "%i" is replaced by the variable after the coma. An "%i" is used because it specifies that the value after the coma will be an integer. Also the \n is the character for a new line.

Let's try running this. Press Command + B to compile.
The program will compile and show some warnings since we are accessing a protected variable directly. But we will run this anyway :)

Now press Command + Shift + R to show the Console and Command + R to run the program.


It assigned a 5 to the protected variable and it also displayed the protected variable....
In most languages you get an error when compiling and you just can NOT run this code. For some reason this works on Objective-C but you probably shouldn't do this.

*Note: the warning says that this will be an error in the future so it will probably not work in future versions of Objective-C.

Now I'll show you the correct way to assign variables directly, and the correct way to assign a value to protected variables.

First, lets go back to the testclass.h file and make some changes.




The @public and @protected are used to set the variables access levels.
The rest of the lines that we added are empty functions. These functions need to be implemented in the .m file. The " : (int) t1: " added to setTestVariable1 means this function will receive a parameter when it is called.

Lets make some changes in the testclass.m file.




In this file we have to implement all the functions that we declared on testclass.h. As you can probably see the print function is used to print the current values of both variables, the "setTestVariable" functions receive a parameter and assign it to one of the variables, and the "testVariable" functions return the value of each variable.

Now lets go to the main.m file and use all of this.



So what does this do??

In the first line after the brackets the object is initialized.

After that on:

[test setTestVariable1: 1];
[test setTestVariable2: 2];

 we assign a "1" to testVariable1 and a "2" to testVariable2. The compiler won't complain like before, since testVariable2 isn't assigned directly and it is assigned trough a function instead.

[test print];

 Is used to call the function in testclass.m that prints the values of the variables.

After this, we have the same code that we had in the first part of the example. This time, the compiler won't complain since testVariable1 is public and a public variable can be called directly from any place.

If we run our program again we should get this output:




As we can see changing the variable trough:

test->testVariable1 = 5;

changes the output from the printf command and from the [test print]. Since the same variable is used to store the value no matter which method you use for assigning the value.

Wednesday, April 21, 2010

So... how do you develop for the iPhone platform?

So far I've found that there are 2 methods to develop applications for the iPhone:

  • Develop native applications using Objective-C and Xcode

  • Develop web applications using the various standards supported by safari and webkit (such as javascript, html5 and css).

Objective-C applications can take full advantage of the iPhone hardware. Web applications are somewhat limited, since the performance of a javascript application isn't as good as that of a native application and also because the application is operating through Wi-Fi and 3g networks.

Even though there are some limitations when developing web applications, there are some cases where it might be easier and/or more useful to use this approach instead of developing an Objective-C application. For example, for a web developer, it might be easier to start developing apps for this platform right away instead of having to learn a whole new and different language. Also, by using a web application, the developers can update the applications for all the users in an easy way.

This is just the introduction to my blog. So far I don't know any Objective-c nor any javascript, but I'll post everything I learn in here. And, when I feel comfortable enough programming native and web applications for the iPhone, I'll try to find out which is the easiest way to learn this and make a full guide about it.

Hello World!

Welcome.
My name is Renato Fontes, and I'm currently learning how to program for the iPhone.
On this blog I'll document my experience and try to provide helpful information and tutorials for other people who also wish to learn how to program for this platform.