0

Could some one tell me why my array is out of scope? Here's my class:

// Paper.h
@interface Paper : NSObject {
  NSMutableArray* items;
} 

@property (retain) NSMutableArray* items;

// Paper.m
#import "Paper.h"
@implementation Paper {
@synthesize items;
}

// ParserUtil.m
@implementation ParserUtil {
+(Paper*) parsePaper:(NSString*)file {
...
Paper* paper = [[[Paper alloc] init] autorelease];
// does the following line is the best practice?
paper.items = [[[MutableArray alloc] init] autorelease];

Item* item = ...; // create item instance
[paper.items addObject:item];

return paper;
}

// call the parser method
...
Paper* paper = [[ParserUtil parsePaper:@"SomeFile"] retain];
// when run to this line, the paper.items is out of scope
// seems all the items in the array are dispear
NSMutableArray* items = paper.items;
...

Could someone point out what is wrong here? Many thanks!

7
  • Your syntax is all over the place in this example. @implementation ends with @end, not with curly braces. Note also that rather than [[[NSMutableArray alloc] init] autorelease] you can just as easily use [NSMutableArray array]. Commented Nov 14, 2010 at 13:46
  • @end are not pasted here. I do not want to paste all code lines here. Could you point out what the problems might be? Thanks anyway. Commented Nov 14, 2010 at 13:54
  • icespace: No, because you didn't show the code where the problem lies. Most likely, the problem is some syntactic imbalance between the code you showed and the code you didn't. Commented Nov 14, 2010 at 14:24
  • Furthermore, the object (or class) that creates a Paper object should not need to create its items array for it; the Paper object should create its own items array. Moreover, getting its items array should get a copy, so [paper.items addObject:…] should not actually add anything to the paper's items. Have Paper respond to addItemsObject: by sending itself insertObjectInItemsAtIndex:, which you should have it respond to by sending its items array (which it should have created in init) an insertObjectAtIndex: message. Commented Nov 14, 2010 at 14:27
  • @Peter: Thanks for the info. The <code>items</code> should be created in <code>init</code> method. What do you mean "getting its items array should get a copy"? The array "items" is a synthesized property of "Paper", it should be accessible by other object, right? Commented Nov 14, 2010 at 14:36

1 Answer 1

5

It isn't.

An object cannot be out of scope, because objects do not have scope. What they can be is unreachable, which is what happens when you don't have any variables holding the object's pointer.

Variables can be out of scope. You can only use a variable within the same scope in which you declared it; you can't begin a compound statement, declare a variable, finish the compound statement, and use the variable, and you can't declare a variable in one function or method and then use it in a different one.

You said in your other question that it's the debugger telling you the variable is out of scope. This means one of two three things:

  1. The variable really is out of scope. Move the variable or move the code that uses it, or just interrupt the debugger earlier (with a breakpoint, if necessary).
  2. The debugger is just being stupid. This happens a lot. Try the po command or sprinkle your code with NSLog statements instead.
  3. You're trying to examine a property-access expression. A property-access expression, by definition, must send an accessor message, which may have side effects; for that reason, the debugger won't do that just for you hovering over the expression, because that's too easy to do by accident. You must use the po command in the Debugger Console to send the accessor message and print the description of the result.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I will check the code according to your comments. The strange thing is the Paper instance is valid pointer but the paper.items is out of scope. I guess the variable pointer is released or somehow became invalid. As you said, the code I pasted here are not beautiful, but since you guys did not point what's wrong in the code, at least I know the errors are in other part of our code. I will update the thread when I found sth. Thank you guys again.
@icespace: paper.items is out of scope because it's a property-access expression. Retrieving it would cause an accessor message, which, because accessors can have side effects, the debugger will not do without your explicit command. Hovering over the expression is not explicit enough; you must inspect it using the po command.
(And yes, that's a very loose application of “out of scope”. The debugger really should say something different for property access expressions, as long as the variable at the root of the expression is in scope.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.