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.
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.
No comments:
Post a Comment