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 12-14-2010, 06:14 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default Counting the size of an Array that contains NSdictionaries

Counting the size of an array of NSDictionary

I have a NSArray with several NSDictionary objects inside it.

I want to count the size of the array, but regardless of what
I do the program ends, complaining of a bad access error.

I copied the structure from UICatalog.

In UICatalog, there is this:

Code:
// dataSourceArray is an NSArray
//NSArray					*dataSourceArray;
self.dataSourceArray = [NSArray arrayWithObjects:
						[NSDictionary dictionaryWithObjectsAndKeys:
							 @"UISwitch", kSectionTitleKey,
							 @"Standard Switch", kLabelKey,
							 @"ControlsViewController.m:\r-(UISwitch *)switchCtl", kSourceKey,
							 self.switchCtl, kViewKey,
						 nil],

						[NSDictionary dictionaryWithObjectsAndKeys:
							 @"UISlider", kSectionTitleKey,
							 @"Standard Slider", kLabelKey,
							 @"ControlsViewController.m:\r-(UISlider *)sliderCtl", kSourceKey,
							 self.sliderCtl, kViewKey,
						 nil],
						
						
						nil];

// Later on, the program is able to count the size of dataSourceArray

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [self.dataSourceArray count];
}


However, whenever I try my own version it keeps falling over.

Code:
NSMutableArray *listOfPlayers = [[[NSMutableArray alloc] init] autorelease];
NSDictionary *dict;

// Loop through SQL results
while ([rs next]) 
{

	
	NSNumber *plyID		= [NSNumber numberWithInt:[rs intForColumn:@"playerID"]];
	NSString *plyFName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerFName"]];
	NSString *plySName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerSName"]];
	NSNumber *plyAvatar	= [NSNumber numberWithInt:[rs intForColumn:@"playerAvatar"]];
	NSString *avName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"avatarName"]];
	
	
	// Put the above into a dictionary
	dict = [NSDictionary dictionaryWithObjectsAndKeys:
						  @"plyID", plyID,
						  @"plyFName", plyFName,
						  @"plySName", plySName,
						  @"plyAvatar", plyAvatar,
						  @"avName",avName,
						  nil];
						
	// Add the above dictionary to the NSArray
	[listOfPlayers addObject:dict];
} // wend

// Close the result set.
[rs close];

// The program will crash here. It won't display the size of the array
NSLog(@"listOfPlayers = %@",[listOfPlayers count]);

Even if I do a single dict and add it to an array, I can never get the
program to display the size of the array.

I've done a log of the contents and it's all okay.

So I must be doing something wrong.

All I want to do is:

a. Add a dictionary to a NSMutableArray
b. Count the contents/objects in the array.

If you can help, that'd be great!

Thanks
zardon is offline   Reply With Quote
Old 12-14-2010, 08:18 AM   #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 zardon View Post
Counting the size of an array of NSDictionary

I have a NSArray with several NSDictionary objects inside it.

I want to count the size of the array, but regardless of what
I do the program ends, complaining of a bad access error.

I copied the structure from UICatalog.

In UICatalog, there is this:



Even if I do a single dict and add it to an array, I can never get the
program to display the size of the array.

I've done a log of the contents and it's all okay.

So I must be doing something wrong.

All I want to do is:

a. Add a dictionary to a NSMutableArray
b. Count the contents/objects in the array.

If you can help, that'd be great!
You're autorleasing the array, and not retaining it anywhere. It's probably getting released, so you're crashing.

Create a retained property, and save the array in the property using property notation.
__________________
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 offline   Reply With Quote
Old 12-14-2010, 09:51 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You're autorleasing the array, and not retaining it anywhere. It's probably getting released, so you're crashing.

Create a retained property, and save the array in the property using property notation.
Hi there.

The reason why its autoreleased is because its in a function where I need to return the array, and I believe that I need to autorelease it otherwise it would add a memory footprint for it.

Here is the full code to the method/funciton

Code:
// Inside a view controller

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	self.listOfPlayers = [[NSMutableArray alloc] init];
	
	Player *p = [[Player alloc] init];
	
	self.listOfPlayers = [p getAllPlayers];
	
	NSLog(@"Results -- %@", self.listOfPlayers);

        // This is where it will crash
	NSLog(@"Count -- %@", [self.listOfPlayers count]);
	
	
	[p release];
}


// Inside player.m
-(NSMutableArray*)getAllPlayers
{
	
	bootleggers3AppDelegate *appDelegate = (bootleggers3AppDelegate *)[[UIApplication sharedApplication] delegate];	
	
	NSMutableArray *listOfPlayers = [[[NSMutableArray alloc] init] autorelease];
	NSDictionary *dict;
	
	[appDelegate.db beginTransaction];
	
	FMResultSet *rs = [appDelegate.db executeQuery:@"SELECT p.id AS playerID, p.fname AS playerFName, p.sname AS playerSName, p.playerAvatar AS playerAvatar, a.name AS avatarName FROM players AS p INNER JOIN avatars AS a ON p.playerAvatar = a.id ORDER BY p.id ASC LIMIT 4"];
	
	
	// Loop through results
	while ([rs next]) 
	{

		
		NSNumber *plyID		= [NSNumber numberWithInt:[rs intForColumn:@"playerID"]];
		NSString *plyFName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerFName"]];
		NSString *plySName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerSName"]];
		NSNumber *plyAvatar	= [NSNumber numberWithInt:[rs intForColumn:@"playerAvatar"]];
		NSString *avName	= [NSString stringWithFormat:@"%@", [rs stringForColumn:@"avatarName"]];
		
		
		// Put the above into a dictionary
		dict = [NSDictionary dictionaryWithObjectsAndKeys:
							  @"plyID", plyID,
							  @"plyFName", plyFName,
							  @"plySName", plySName,
							  @"plyAvatar", plyAvatar,
							  @"avName",avName,
							  nil];
		
		 
		[listOfPlayers addObject:dict];
	} // wend
	
	// Close the result set.
	[rs close];
	
	[appDelegate.db commit];
	
	return listOfPlayers;
}
I will try your idea of a property notation and retaining on a cut down version of the above in a new file and seeing if that works instead.
zardon is offline   Reply With Quote
Old 12-14-2010, 10:56 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default

I think I know what the reason is.

I put the keys and the objects in the wrong order.

This code seems to work with just 1 dictionary in an array.

Code:
	NSMutableArray *items = [[NSMutableArray alloc] init];
	
	// --
		
	NSNumber *plyID		= [NSNumber numberWithInt:1];
	NSString *plyFName	= [NSString stringWithFormat:@"fname"];
	NSString *plySName	= [NSString stringWithFormat:@"sname"];
	NSNumber *plyAvatar	= [NSNumber numberWithInt:1];
	NSString *avName	= [NSString stringWithFormat:@"avname"];
	
// I originally put the keys first and then the object, this might be the
// reason why the app died the first time round.
	NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:plyID, @"plyID", 
						  plyFName, @"plyFName", 
						  plySName, @"plySName",
						  plyAvatar, @"plyAvatar",
						  avName, @"avName",
						  nil];
	
	[items addObject:dict];
	
	NSLog(@"Items = %@", items);
	NSLog(@"Items Size = %d", [items count]);
I'm going to see if this resolves the problem

Edit:

Yep, it was a case of me putting the objects and the keys in the wrong order. UGH!

Thanks all to who helped.

Last edited by zardon; 12-14-2010 at 11:09 AM.
zardon is offline   Reply With Quote
Reply

Bookmarks

Tags
count, nsarray, nsdictionary

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
9 members and 387 guests
chemistry, daudrizek, HemiMG, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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