02-05-2012, 02:21 PM
#1 (permalink )
iPhone Developer
Join Date: Sep 2010
Posts: 25
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!
02-05-2012, 02:28 PM
#2 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Use properties.
These methods are in the same class?
02-05-2012, 02:42 PM
#3 (permalink )
iPhone Developer
Join Date: Sep 2010
Posts: 25
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
02-05-2012, 02:52 PM
#4 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
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.
02-05-2012, 04:13 PM
#5 (permalink )
iPhone Developer
Join Date: Sep 2010
Posts: 25
Quote:
Originally Posted by
BrianSlick
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?
02-05-2012, 04:26 PM
#6 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Oh geez, read the properties link in my signature.
02-05-2012, 05:05 PM
#7 (permalink )
iPhone Developer
Join Date: Sep 2010
Posts: 25
Quote:
Originally Posted by
BrianSlick
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.
02-05-2012, 09:44 PM
#8 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
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.
02-06-2012, 11:00 AM
#9 (permalink )
iPhone Developer
Join Date: Sep 2010
Posts: 25
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!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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