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 11-21-2011, 03:51 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 204
marciokoko is on a distinguished road
Question Dictionaries, Arrays & UITableViewCells

In a tableViewController of users I have this inside an NSOperationQueue call which is successfully getting the data and creating the users.Array:

Code:
//GET user/points ARRAY
    for (NSDictionary *usersDataDictionary in self.usersArray) {
        // call the method to get that user's points from a web service call
        [self getMyPoints:[usersDataDictionary objectForKey:@"username"]];
    }
getMyPoints is a void method that uses ASI HTTP to asynchronously make a request to a php web service:

Code:
- (void)getMyPoints:(NSString *)user{
	NSLog(@"got the user as:%@",user);
	NSString *urlString = [NSString stringWithFormat:@"http://www.server.com/app/getpoints.php?username=%@",user];
        NSURL *url = [NSURL URLWithString:urlString];
	NSLog(@"string sent is:%@", urlString);
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	[request addPostValue:user forKey:@"username"];
	[request setDelegate:self];
	[request startAsynchronous];
}
and returns the data in a responseString through a delegate method like this:

Code:
- (void)requestFinished:(ASIHTTPRequest *)request{
	// Receive the request from the server
	NSString *responseString = [request responseString];
	NSLog(@"string received is:%@",responseString);
	NSError *outError = NULL;

        // Place that json String into a mutable array
	[ self.myPoints addObject:[NSDictionary dictionaryWithJSONString:responseString error:&outError ]];
	NSLog(@"the myPointsArray is:%@", myPoints );
	[self.tableView reloadData];
}
I will then use that mutable array to populate the detailText in a tableview cell.

Everything works fine up to where I print out the responseString. But from that point on, the parsed string doesn't get put into the myPoints array because that array returns (NULL) when logged into the console.

Any Ideas?
__________________
Mars
www.santiapps.com
www.gea-hn.com
mba-i4
marciokoko is offline   Reply With Quote
Old 11-21-2011, 04:01 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by marciokoko View Post
In a tableViewController of users I have this inside an NSOperationQueue call which is successfully getting the data and creating the users.Array:

Code:
//GET user/points ARRAY
    for (NSDictionary *usersDataDictionary in self.usersArray) {
        // call the method to get that user's points from a web service call
        [self getMyPoints:[usersDataDictionary objectForKey:@"username"]];
    }
getMyPoints is a void method that uses ASI HTTP to asynchronously make a request to a php web service:

Code:
- (void)getMyPoints:(NSString *)user{
	NSLog(@"got the user as:%@",user);
	NSString *urlString = [NSString stringWithFormat:@"http://www.server.com/app/getpoints.php?username=%@",user];
        NSURL *url = [NSURL URLWithString:urlString];
	NSLog(@"string sent is:%@", urlString);
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	[request addPostValue:user forKey:@"username"];
	[request setDelegate:self];
	[request startAsynchronous];
}
and returns the data in a responseString through a delegate method like this:

Code:
- (void)requestFinished:(ASIHTTPRequest *)request{
	// Receive the request from the server
	NSString *responseString = [request responseString];
	NSLog(@"string received is:%@",responseString);
	NSError *outError = NULL;

        // Place that json String into a mutable array
	[ self.myPoints addObject:[NSDictionary dictionaryWithJSONString:responseString error:&outError ]];
	NSLog(@"the myPointsArray is:%@", myPoints );
	[self.tableView reloadData];
}
I will then use that mutable array to populate the detailText in a tableview cell.

Everything works fine up to where I print out the responseString. But from that point on, the parsed string doesn't get put into the myPoints array because that array returns (NULL) when logged into the console.

Any Ideas?
Are you using ARC, or retain/release?

Where is the array myPoints being allocated? It looks like it's a property of your class. Post the code that creates the array and saves it to the property.

You should have the property set up as retain (or strong, if you're using ARC) and make sure you assign the array to the property using the setter. Something like this:

Code:
self.myPoints = [NSMutableArray arrayWithCapacity: 10];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 04:16 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 204
marciokoko is on a distinguished road
Question Null Array

I have

@property (nonatomic,retain) NSMutableArray *myPoints;

and

@synthesize myPoints;

Then in viewDidLoad I do:

Code:
self.myPoints = [NSMutableArray arrayWithCapacity:30];
self.myPoints = nil;
Then just now I did this, to make sure what was being put into the array:

Code:
// I COMMENTED THIS PART OUT
//[self.myPoints addObject:
// IN ORDER TO LOG WHAT WILL BE PUT INSIDE...
NSLog(@"parsed json.........%@",[NSDictionary dictionaryWithJSONString:responseString error:&outError]);
and i correctly get this:

2011-11-21 16:07:35.487 [925:15b03] string received is:[{"username":"IrishSparky","PUNTOS":"2"}]
2011-11-21 16:07:35.488 [925:15b03] parsed json.........(
{
PUNTOS = 2;
username = IrishSparky;
}
)
__________________
Mars
www.santiapps.com
www.gea-hn.com
mba-i4
marciokoko is offline   Reply With Quote
Old 11-21-2011, 05:50 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by marciokoko View Post
I have

@property (nonatomic,retain) NSMutableArray *myPoints;

and

@synthesize myPoints;

Then in viewDidLoad I do:

Code:
self.myPoints = [NSMutableArray arrayWithCapacity:30];
self.myPoints = nil;
Think about what that code does! It creates a new mutable array and assigns it to your myPoints property.
Then, on the very next line, you set self.myPoints back to nil, so of course you can't save values to the array. The array is gone, almost as soon as its created!

Get rid of that line:

Code:
self.myPoints = nil;
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 06:23 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

try adding this line in viewDidLoad, instead of the lines you currently have:

self.myPoints = [[NSMutableArray alloc] init];
RickSDK is offline   Reply With Quote
Old 11-21-2011, 07:51 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 204
marciokoko is on a distinguished road
Question empty array

Ok i tried both things. Now it crashes a bit further down because the self.myPoints array is empty when CFRAIP is called because here is the complete code:

Code:
if(indexPath.section == 0){
	// Configure the cell with players in game.
	NSDictionary *userDataDict = [usersArray objectAtIndex:indexPath.row];
	cell.textLabel.text = [userDataDict objectForKey:@"username"];
        
	// NOW DISPLAY POINTS
        //NSArray *userPointsArray = [self.myPoints objectAtIndex:indexPath.row]; 
        //NSDictionary *userPointsDict = [userPointsArray objectAtIndex:0];
        //NSString *pts = [userPointsDict objectForKey:@"POINTS"];
	//cell.detailTextLabel.text = pts;
	return cell;
}
else {
	//IF NOT FIRST FEW CELLS...
	// Configure cell with your stats
	NSLog(@"1st object:%@",[self.myPoints objectAtIndex:0]);
	NSDictionary *dict = [self.myPoints objectAtIndex:0];
	NSLog(@"1st key:%@",[dict objectForKey:@"PUNTOS"]);
	cell.textLabel.text = [dict objectForKey:@"PUNTOS"];
	cell.detailTextLabel.text = @"Bumped tokens are worth more!";
        cell.textLabel.backgroundColor=[UIColor clearColor];
	return cell;
}
I guess its reaching the CFRAIP method before the NSOperationQueue is done...because this is the error I get on crash:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

----------EDIT----------
So I added a @"hi" string to the array and it worked but it crashed later on because of course, its not a dict I'm passing it, it was just a string. Anywho, so what's happening is that since Im separating the getting of that data into a separate NSOperation, it keeps running the CFRAIP method and of course reaches an empty self.myPoints and crashes...

This means I have to rethink my logic here...Im thinking of adding an if/else such that if the self.myPoints array is still NULL then just add placeholder text...until the array has been populated...like:

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // change background of cell
    cell.backgroundColor = [UIColor clearColor];
    //[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"greenwood.png"]];
    
	if(indexPath.section == 0){
		// Configure the cell with players in game.
		NSDictionary *userDataDict = [usersArray objectAtIndex:indexPath.row];
		cell.textLabel.text = [userDataDict objectForKey:@"username"];
        
	// NOW DISPLAY POINTS
        //NSArray *userPointsArray = [self.myPoints objectAtIndex:indexPath.row]; 
        //NSDictionary *userPointsDict = [userPointsArray objectAtIndex:0];
        //NSString *pts = [userPointsDict objectForKey:@"PUNTOS"];
	//cell.detailTextLabel.text = pts;
        //cell.textLabel.backgroundColor=[UIColor clearColor];
		return cell;
	}
	else {
		//IF NOT FIRST FEW CELLS...
		// Configure cell with your stats
        if (self.myPoints == NULL) {
            cell.textLabel.text = @"POINTS";
            cell.detailTextLabel.text = @"Loading!";
            cell.textLabel.backgroundColor=[UIColor clearColor];
            return cell;
        } else if (self.myPoints != NULL) {
            NSLog(@"1st object:%@",[self.myPoints objectAtIndex:0]);
            NSDictionary *dict = [self.myPoints objectAtIndex:0];
            NSLog(@"1st key:%@",[dict objectForKey:@"PUNTOS"]);
            cell.textLabel.text = [dict objectForKey:@"PUNTOS"];
            cell.detailTextLabel.text = @"Bumped tokens are worth more!";
            cell.textLabel.backgroundColor=[UIColor clearColor];
            return cell;
        }
	}
}
But it didn't work. I still get the same error when it reaches the empty self.myPoints array...

Quote:
Originally Posted by RickSDK View Post
try adding this line in viewDidLoad, instead of the lines you currently have:

self.myPoints = [[NSMutableArray alloc] init];
__________________
Mars
www.santiapps.com
www.gea-hn.com
mba-i4

Last edited by marciokoko; 11-21-2011 at 09:18 PM.
marciokoko 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: 396
16 members and 380 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja, sacha1996, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
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:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0