2

Are "local object variables" the variables that are used or initialized in a method, or are they the arguments taken in? I can't find this term in Xcode's documentation or Google.

I found this in the Objective-C book that I'm using. The full quote is

Local variables that are basic C data types have no default initial value, so you must set them to some value before using them. The three local variables in the reduce method are set to values before they are used, so that's not a problem here. Local object variables are initialized to nil by default. Unlike your instance variables (which retain their values through method calls), these local variables have no memory. Therefore, after the method returns, the values of these variables disappear. Every time a method is called, each local variable defined in that method is reinitialized to the value specified (if any) with the variable's declaration."

jscs
  • 63,694
  • 13
  • 151
  • 195
stumped
  • 3,235
  • 7
  • 43
  • 76
  • "Local object variable" isn't really typical terminology in Objective-C. Where are you seeing it used? – Andrew Madsen Apr 06 '12 at 01:46
  • It's in my book it reads, "Local variables that are basic C data types have no default initial value, so you must set them to some value before using them. – stumped Apr 06 '12 at 02:31
  • The three local variables in the `reduce` method are set to values before they are used, so that's not a problem here. Lovl object variables are initialized to _nil_ by default. Unlike your instance variables (which retain their values through method calls(, these local variables have no memory. Therefore, after the method returns, the values of these variables disappear. Every time a method is called, each local variable defined in that method is reinitialized to the value specified *if any( with the variable's declaration." – stumped Apr 06 '12 at 02:34

2 Answers2

1

Based on your comment, I understand what the book means. Local variables are variables local to a particular scope (denoted by braces '{}' in C and Objective-C). Local variables are declared in the scope where they're used, as opposed to global variables which can be seen and used globally (to a file, multiple files or the whole program depending on declaration visibility). Instance variables are part of a class instance and can be used by any of its methods (and other classes too if declared using @public, though that's generally not good practice).

Primitive local variables are local variables whose type is a C primitive like int, float, char, etc. What the book is calling "local object variables" are simply local variables whose type is (a pointer to) an Objective-C object. Examples are NSString *, NSDictionary * and id.

Local variables are stored on the stack, as opposed to the heap. Variables on the stack go away at the end of the method or function call where they were declared. This Stack Overflow question has some good answers explaining the difference between the stack and the heap: What and where are the stack and heap?

The first result of a Google search for "local variables objective-c": http://blog.ablepear.com/2010/04/objective-c-tuesdays-local-variables.html .

Community
  • 1
  • 1
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
0

Local variables are defined in the method and scope of the variables that have existed inside the method itself.

Java By Kiran
  • 95
  • 1
  • 2