Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-17-2011, 01:45 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default NSMutableArray objects are "out of scope"

Hello,

I've been having this issue for the entirety of the past week and I cannot seem to fix the issue. I am trying to initialise the member variable "drinks" with a series of objects but when I do that, each object in the array shows up as "out of scope". I have tried creating a fresh new array which fills up fine, but then if I try to copy it over to my "drinks" array the objects just show up as "out of scope" again. Alternatively I have tried to fill up the "drinks" array with just strings and that doesn't work either.

Here is my .h file:


Code:
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
            
        UITableView *PrefsTable;
        NSMutableArray *favourites;
        NSMutableArray *drinks;
        int checkCounter;
}

@property (nonatomic, retain) IBOutlet UITableView *PrefsTable;
@property (nonatomic, retain) NSMutableArray *drinks;
@property (nonatomic, retain) NSMutableArray *favourites;

- (void) addDrink: (NSString *) name;

@end
Here is the ViewDidLoad method in the .m file:
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    checkCounter = 0;
    Drink *drink1 = [[[Drink alloc] initWithImage:nil name:@"Test1" caffeine:11] autorelease];
    Drink *drink2 = [[[Drink alloc] initWithImage:nil name:@"Test2" caffeine:22] autorelease];
    Drink *drink3 = [[[Drink alloc] initWithImage:nil name:@"Test3" caffeine:33] autorelease];
    Drink *drink4 = [[[Drink alloc] initWithImage:nil name:@"Test4" caffeine:44] autorelease];
    Drink *drink5 = [[[Drink alloc] initWithImage:nil name:@"Test5" caffeine:55] autorelease];
    self.drinks = [[NSMutableArray alloc] initWithObjects: drink1, drink2, drink3, drink4, drink5, nil];
}
Here is an image of the error I was getting:
Error image

Each of the objects initialise properly, but they don't get put in the array properly. I really do not know why this is, can anybody please help me?

Thanks in advance,
JheeBz
JheeBz is offline   Reply With Quote
Old 09-17-2011, 01:52 AM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Do you have an actual problem inside your code or is the debugger just not showing your objects correctly? If you have an actual problem in your code, how did you come to this conclusion?
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 09-17-2011, 01:53 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Do you have an actual problem inside your code or is the debugger just not showing your objects correctly? If you have an actual problem in your code, how did you come to this conclusion?
I do have a problem, the problem is that each object contained within the array is empty or "out of scope", whatever that means. I need those objects in that array.
JheeBz is offline   Reply With Quote
Old 09-17-2011, 01:54 AM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Yes, are you just going by the debugger or have you actually tried doing something with those objects and gotten an error or crash?
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 09-17-2011, 01:56 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Yes, are you just going by the debugger or have you actually tried doing something with those objects and gotten an error or crash?
Yes, I try to retrieve these in another method and they don't show up.
JheeBz is offline   Reply With Quote
Old 09-17-2011, 01:59 AM   #6 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Post the code and btw, you are going to leak your drinks array but we'll fix that later when we've solved your problem.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 09-17-2011, 02:07 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Post the code and btw, you are going to leak your drinks array but we'll fix that later when we've solved your problem.
This is what ViewDidDisappear looks like at the moment:

Code:
- (void)viewDidDisappear:(BOOL)animated
{
        for (int i = 0; i < [drinks count]; i++)
        {
                Drink *drink  = [drinks objectAtIndex:i];
                NSLog(@"%@", [drink name]);
        }
        NSLog(@"FAVOURITES COUNT: %d", [drinks count]);
        NSLog(@"VIEW DISAPPEARED");
}
The output of this is:

Code:
2011-09-17 17:04:04.710 Project[1335:207] FAVOURITES COUNT: 5
2011-09-17 17:04:04.711 Project[1335:207] VIEW DISAPPEARED
JheeBz is offline   Reply With Quote
Old 09-17-2011, 02:10 AM   #8 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Edit: I'm sorry, I learned this from an old text. You don't need to declare the variable outside.

Edit2: You do need to declare it outside if you have c89. So just to be safe, declare your i variable outside and see what happens.

Code:
int i = 0;
for (i = 0; i < [drinks count]; i++)
        {
                Drink *drink  = [drinks objectAtIndex:i];
                NSLog(@"%@", [drink name]);
        }
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.

Last edited by Domele; 09-17-2011 at 02:14 AM.
Domele is offline   Reply With Quote
Old 09-17-2011, 02:16 AM   #9 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Edit: I'm sorry, I learned this from an old text. You don't need to declare the variable outside.

Edit2: You do need to declare it outside if you have c89. So just to be safe, declare your i variable outside and see what happens.

Code:
int i = 0;
for (i = 0; i < [drinks count]; i++)
        {
                Drink *drink  = [drinks objectAtIndex:i];
                NSLog(@"%@", [drink name]);
        }
Wow that works! I had no idea you had to do this, I thought that was only necessary for while loops.

Thank you so very much, you just saved me another week of staring at the same problem.
JheeBz is offline   Reply With Quote
Old 09-17-2011, 02:23 AM   #10 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Okay now about your drinks array leaking, you have a couple options.

1. You could just not use the property to alloc and init your array.
Code:
array = [[NSArray alloc] initWithObjects:@"blah, @"blah"];
2. You could add an auto release to the end of your call.
Code:
self.array = [[[NSArray alloc] initWithObjects:@"blah, @"blah"] autorelease];
3. You use the 3 line format, create a temporary array with your objects, use the temp array to assign your property, and then release the temp array.
Code:
NSArray *tempArray = [NSArray alloc] initWithObjects:@"blah, @"blah"];
self.array = tempArray;
[tempArray release];
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 09-17-2011, 02:35 AM   #11 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Okay now about your drinks array leaking, you have a couple options.

1. You could just not use the property to alloc and init your array.
Code:
array = [[NSArray alloc] initWithObjects:@"blah, @"blah"];
2. You could add an auto release to the end of your call.
Code:
self.array = [[[NSArray alloc] initWithObjects:@"blah, @"blah"] autorelease];
3. You use the 3 line format, create a temporary array with your objects, use the temp array to assign your property, and then release the temp array.
Code:
NSArray *tempArray = [NSArray alloc] initWithObjects:@"blah, @"blah"];
self.array = tempArray;
[tempArray release];
Thanks for that. I added an autorelease to the end of the assignment. I originally had that but I've been fiddling around so much that I must've forgotten.
JheeBz is offline   Reply With Quote
Reply

Bookmarks

Tags
array, ios, iphone, nsmutablearray, scope

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 408
16 members and 392 guests
7twenty7, Eclectic, eski, EvilElf, fiftysixty, HemiMG, iOS.Lover, JackReidy, jarv, pbart, Pudding, sacha1996, teebee74, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0