Objective-C .. I just don't get it

Joined
May 15, 2009
Messages
1
Reaction score
0
Points
1
I am having a really hard time moving to objective-c /cocoa in terms of the .h and .m files.

I understand what they are, but I don't get what goes where.

My biggest issues right now is

how can I internalize variables for use inside a specific function
PHP:
-(void)afunctionImade{
var1 = 1;

var2 = 5;


var3 = var1 + var2;
return var3;
}

if i try to build and go on this i get the error

"var1 undeclared (first use in this function)"
and so on for each variable.

I ONLY want to use those vars inside of that exact function, I don't want them to be accessed anywhere else. Why do I have to declare it somewhere? Or how do i declare it so that is it only used inside that function?

Thanks in advance,
Chris
 
Joined
Mar 9, 2004
Messages
9,065
Reaction score
331
Points
83
Location
Munich
Your Mac's Specs
Aluminium Macbook 2.4 Ghz 4GB RAM, SSD 24" Samsung Display, iPhone 4, iPad 2
It doesn't look as though you've defined an object type for your variables...
You'd need to write:
Code:
int var1 = 1;
(Unlike PHP, you can't use variables without types like that in Objective-C - or many other languages).

Once declared, you can access the variable directly:

Code:
var1 = 5*12;
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
To add to what Aptmunich said, your problem here seems to have been more of a lack of understanding of the C language rather than with Objective-C. You may want to take a step back and find some C language tutorials or books to dive into and gain an understanding of before you start adding Objective-C's extensions to the mix.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top