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 02-05-2012, 02:21 PM   #1 (permalink)
iPhone Developer
 
djbrooks111's Avatar
 
Join Date: Sep 2010
Posts: 25
djbrooks111 is on a distinguished road
Default NSMutableArray has no objects in different method

So I am using the Facebook SDK with my app, and when I try to access one of my NSMutableArrays outside of the method that they were created and filled with data in, they are empty.

Here is the method that the NSMutableArrays are being created:

Code:
-(void)request:(FBRequest *)request didLoad:(id)result {
    NSLog(@"reached request method");
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        userInfoArray = [result objectForKey:@"data"];
        userIdArray = [[NSMutableArray alloc] initWithCapacity:10];
        userNameArray = [[NSMutableArray alloc] initWithCapacity:10];
        userPictureArray = [[NSMutableArray alloc] initWithCapacity:10];
        userGenderArray = [[NSMutableArray alloc] initWithCapacity:10];
        
    
            for (int indexVal = 0; indexVal < 200; indexVal++) {
                individualUserInfo = [userInfoArray objectAtIndex:indexVal];
                
                if ([individualUserInfo objectForKey:@"id"] && [individualUserInfo objectForKey:@"name"] && [individualUserInfo objectForKey:@"picture"] && [individualUserInfo objectForKey:@"gender"]) {
            
                    [userIdArray addObject:[individualUserInfo objectForKey:@"id"]];
                    [userNameArray addObject:[individualUserInfo objectForKey:@"name"]];
                    [userPictureArray addObject:[individualUserInfo objectForKey:@"picture"]];
                    [userGenderArray addObject:[individualUserInfo objectForKey:@"gender"]];
                } else {
                    NSLog(@"Request failed for some reason");
                }
            }
        NSLog(@"Friend Name Array : %@ ",userNameArray);
        NSLog(@"ID Array : %@ ",userIdArray);
        NSLog(@"Gender Array : %@ ",userGenderArray);
        NSLog(@"Picture Array : %@ ",userPictureArray);
        NSLog(@"Success in getting info");
        NSLog(@"Friend name, first index: %@",[userNameArray objectAtIndex:0]);
    }
}
At the end of that method, NSLog(@"Friend name, first index: %@",[userNameArray objectAtIndex:0]); shows David at index 0 of userNameArray. However, when I access that same index from a different method, the console says that userNameArray of index 0 is (null).

Here is that method where I access the NSMutableArray:

Code:
-(IBAction)nextPerson:(id)sender {
    if (sender == button1) {
        //DO STUFF
        UIImage *proPic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.iphonedevsdk.com/forum/sdk/misc/iphonedev.png"]]];
        NSLog(@"Name at first index: %@",[userNameArray objectAtIndex:indexValue]);
        nameLabel.text = [userNameArray objectAtIndex:indexValue];
        proPicView.image = proPic;
        indexValue++;
    } else if (sender == button2) {
        //DO MORE STUFF
    } else if (sender == button3) {
        //DO EVEN MORE STUFF
    }
}
In that method, NSLog(@"Name at first index: %@",[userNameArray objectAtIndex:indexValue]); shows (null) as the object at index 0, which is what indexValue starts at.

Thanks for the help!
__________________
Check out my current apps!

Drinking Games!
Never Have I Ever
Never Have I Ever HD (iPad)
Love/Hate
djbrooks111 is offline   Reply With Quote
Old 02-05-2012, 02:28 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Use properties.

These methods are in the same class?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-05-2012, 02:42 PM   #3 (permalink)
iPhone Developer
 
djbrooks111's Avatar
 
Join Date: Sep 2010
Posts: 25
djbrooks111 is on a distinguished road
Default

Yes they are in the same class, which is called Game.

Here is my Game.h:

Code:
@interface Game : UIViewController <FBRequestDelegate, MBProgressHUDDelegate> {
    Facebook *facebook;
    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    IBOutlet UIButton *button3;
    IBOutlet UIButton *startButton;
    
    IBOutlet UILabel *nameLabel;
    IBOutlet UIImageView *proPicView;
    
    MBProgressHUD *HUD;
    
    NSMutableArray  *userInfoArray;
    NSMutableArray  *userIdArray;
    NSMutableArray  *userNameArray;
    NSMutableArray  *userPictureArray;
    NSMutableArray  *userGenderArray;
    NSDictionary    *individualUserInfo;
    
    int indexValue;
}

-(void)myTask;
-(IBAction)showWithGradient:(id)sender;
-(void)getFacebookStuff;
-(IBAction)start:(id)sender;
-(IBAction)nextPerson:(id)sender;

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) NSMutableArray *userInfoArray;
@property (nonatomic, retain) NSMutableArray *userIdArray;
@property (nonatomic, retain) NSMutableArray *userNameArray;
@property (nonatomic, retain) NSMutableArray *userPictureArray;
@property (nonatomic, retain) NSMutableArray *userGenderArray;

@end
And here is my Game.m:

Code:
@implementation Game

@synthesize facebook, userGenderArray, userPictureArray, userNameArray, userIdArray, userInfoArray;

-(IBAction)nextPerson:(id)sender {
    if (sender == bangButton) {
        //DO STUFF
        UIImage *proPic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.iphonedevsdk.com/forum/sdk/misc/iphonedev.png"]]];
        NSLog(@"Name at first index: %@",[userNameArray objectAtIndex:indexValue]);
        nameLabel.text = [userNameArray objectAtIndex:indexValue];
        proPicView.image = proPic;
        indexValue++;
    } else if (sender == marryButton) {
        //DO MORE STUFF
    } else if (sender == killButton) {
        //DO EVEN MORE STUFF
    }
}

-(void)request:(FBRequest *)request didLoad:(id)result {
    NSLog(@"reached request method");
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        userInfoArray = [result objectForKey:@"data"];
        userIdArray = [[NSMutableArray alloc] initWithCapacity:10];
        userNameArray = [[NSMutableArray alloc] initWithCapacity:10];
        userPictureArray = [[NSMutableArray alloc] initWithCapacity:10];
        userGenderArray = [[NSMutableArray alloc] initWithCapacity:10];
        
    
            for (int indexVal = 0; indexVal < 200; indexVal++) {
                individualUserInfo = [userInfoArray objectAtIndex:indexVal];
                
                if ([individualUserInfo objectForKey:@"id"] && [individualUserInfo objectForKey:@"name"] && [individualUserInfo objectForKey:@"picture"] && [individualUserInfo objectForKey:@"gender"]) {
            
                    [userIdArray addObject:[individualUserInfo objectForKey:@"id"]];
                    [userNameArray addObject:[individualUserInfo objectForKey:@"name"]];
                    [userPictureArray addObject:[individualUserInfo objectForKey:@"picture"]];
                    [userGenderArray addObject:[individualUserInfo objectForKey:@"gender"]];
                } else {
                    NSLog(@"Request failed for some reason");
                }
            }
        NSLog(@"Friend Name Array : %@ ",userNameArray);
        NSLog(@"ID Array : %@ ",userIdArray);
        NSLog(@"Gender Array : %@ ",userGenderArray);
        NSLog(@"Picture Array : %@ ",userPictureArray);
        NSLog(@"Success in getting info");
        NSLog(@"Friend name, first index: %@",[userNameArray objectAtIndex:0]);
    }
}
I have taken parts of my code out as they are not involved in this error
__________________
Check out my current apps!

Drinking Games!
Never Have I Ever
Never Have I Ever HD (iPad)
Love/Hate
djbrooks111 is offline   Reply With Quote
Old 02-05-2012, 02:52 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Right, you aren't using your properties.

Are you sure the didLoad method has completed when you hit the button?

And it's not that your array is empty, it doesn't exist.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-05-2012, 04:13 PM   #5 (permalink)
iPhone Developer
 
djbrooks111's Avatar
 
Join Date: Sep 2010
Posts: 25
djbrooks111 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Right, you aren't using your properties.

Are you sure the didLoad method has completed when you hit the button?

And it's not that your array is empty, it doesn't exist.
Yes I am positive that the didLoad method has completed. And how am I not using my properties? Also, don't NSMutableArrays autorelease after the method that they are used in? Could that be the issue?
__________________
Check out my current apps!

Drinking Games!
Never Have I Ever
Never Have I Ever HD (iPad)
Love/Hate
djbrooks111 is offline   Reply With Quote
Old 02-05-2012, 04:26 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Oh geez, read the properties link in my signature.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-05-2012, 05:05 PM   #7 (permalink)
iPhone Developer
 
djbrooks111's Avatar
 
Join Date: Sep 2010
Posts: 25
djbrooks111 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Oh geez, read the properties link in my signature.
So I looked at your tutorial and this is what I came up with so far.

Game.h

Code:
@property (nonatomic, retain) NSMutableArray *userInfoArrayProperty;
@property (nonatomic, retain) NSMutableArray *userIdArrayProperty;
@property (nonatomic, retain) NSMutableArray *userNameArrayProperty;
@property (nonatomic, retain) NSMutableArray *userPictureArrayProperty;
@property (nonatomic, retain) NSMutableArray *userGenderArrayProperty;
Game.m
Code:
@synthesize facebook, userGenderArrayProperty = userGenderArray, userPictureArrayProperty = userPictureArray, userNameArrayProperty = userNameArray, userIdArrayProperty = userIdArray, userInfoArrayProperty = userInfoArray;

-(IBAction)nextPerson:(id)sender {
    if (sender == bangButton) {
        //DO STUFF
        UIImage *proPic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.iphonedevsdk.com/forum/sdk/misc/iphonedev.png"]]];
        NSLog(@"Name at first index: %@",[[self userNameArrayProperty] objectAtIndex:indexValue]);
        nameLabel.text = [[self userNameArrayProperty] objectAtIndex:indexValue];
        proPicView.image = proPic;
        indexValue++;
    } else if (sender == marryButton) {
        //DO MORE STUFF
    } else if (sender == killButton) {
        //DO EVEN MORE STUFF
    }
}


-(void)request:(FBRequest *)request didLoad:(id)result {
    NSLog(@"reached request method");
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        userInfoArray = [result objectForKey:@"data"];
        userIdArray = [[NSMutableArray alloc] initWithCapacity:10];
        userNameArray = [[NSMutableArray alloc] initWithCapacity:10];
        userPictureArray = [[NSMutableArray alloc] initWithCapacity:10];
        userGenderArray = [[NSMutableArray alloc] initWithCapacity:10];
        
    
            for (int indexVal = 0; indexVal < 200; indexVal++) {
                individualUserInfo = [userInfoArray objectAtIndex:indexVal];
                
                if ([individualUserInfo objectForKey:@"id"] && [individualUserInfo objectForKey:@"name"] && [individualUserInfo objectForKey:@"picture"] && [individualUserInfo objectForKey:@"gender"]) {
            
                    [userIdArray addObject:[individualUserInfo objectForKey:@"id"]];
                    [userNameArray addObject:[individualUserInfo objectForKey:@"name"]];
                    [userPictureArray addObject:[individualUserInfo objectForKey:@"picture"]];
                    [userGenderArray addObject:[individualUserInfo objectForKey:@"gender"]];
                } else {
                    NSLog(@"Request failed for some reason");
                }
            }
        
        [self setUserIdArrayProperty:userIdArray];
        [self setUserNameArrayProperty:userNameArray];
        [self setUserPictureArrayProperty:userPictureArray];
        [self setUserGenderArrayProperty:userGenderArray];
        
        NSLog(@"Friend Name Array : %@ ",userNameArray);
        NSLog(@"ID Array : %@ ",userIdArray);
        NSLog(@"Gender Array : %@ ",userGenderArray);
        NSLog(@"Picture Array : %@ ",userPictureArray);
        NSLog(@"Success in getting info");
        NSLog(@"Friend name, first index: %@",[userNameArray objectAtIndex:0]);
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [userIdArray release], userIdArray = nil;
    [userNameArray release], userNameArray = nil;
    [userPictureArray release], userPictureArray = nil;
    [userGenderArray release], userGenderArray = nil;
}
What am I doing wrong? I still get (null) when I try to access the NSMutableArray through its property in the nextPerson method.
__________________
Check out my current apps!

Drinking Games!
Never Have I Ever
Never Have I Ever HD (iPad)
Love/Hate
djbrooks111 is offline   Reply With Quote
Old 02-05-2012, 09:44 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Wow, you really aren't getting this at all. Let's single out 2 examples here:

Code:
@synthesize userIdArrayProperty = userIdArray;
@synthesize userInfoArrayProperty = userInfoArray;
That's fine. On the left we have the property name, on the right we have the instance variable name. Putting the word "property" on the property is a tad silly, but if it helps you, go for it. Then you use it:

Code:
userInfoArray = [result objectForKey:@"data"];
userIdArray = [[NSMutableArray alloc] initWithCapacity:10];
Direct assignment to the instance variable in each case. Look at your own naming convention... are you using the properties? No. I don't know how you read my post and walked away thinking that this was still ok. You apparently need to reread it a few times. We continue on:

Code:
[self setUserIdArrayProperty:userIdArray];
You then take the instance variable and pass it into the setter... where it will wind up back at the instance variable. What sense does that make? The only thing saving your bacon here is that you didn't try to do this with userInfoArray, because that would have crashed.


Now, none of this addresses your core problem. It's just that your memory management skills are lacking, and you need to learn this stuff.

The core problem is that the array is not being created, OR it is being removed before you go to use it. There is something here that you haven't shown, or you have not accurately described the situation. IF the didLoad method is indeed going first, and IF you log correct values at the end of that method, and IF the array is then null when you look for it in the IBAction method, then really the only possible answer is that you are doing something somewhere in the middle to clear out that array property. You haven't shown that code, so I can only guess.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-06-2012, 11:00 AM   #9 (permalink)
iPhone Developer
 
djbrooks111's Avatar
 
Join Date: Sep 2010
Posts: 25
djbrooks111 is on a distinguished road
Default

So I played around with a couple of different things, and ultimately just remade the project. Now everything works as planned! Thanks for your help!
__________________
Check out my current apps!

Drinking Games!
Never Have I Ever
Never Have I Ever HD (iPad)
Love/Hate
djbrooks111 is offline   Reply With Quote
Reply

Bookmarks

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: 403
18 members and 385 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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