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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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-22-2010, 05:38 AM   #1 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question Simple count of NSArray's items & it's dictionarys?

Just wanna count the number of items in the array and it's dictionarys to set an max-value for another random-method.
Tried this, but don't get any counts from it. Any tips or pointers? Thanks.;
Code:
- (void)viewDidLoad {
	// Read in cardarray from file
	NSString *path = [[NSBundle mainBundle] pathForResource:@"cardArray" ofType:@"plist"];
	
	// Put dictionarys in array
	NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	self.cards = dict;
	[dict release];
	
	NSArray *array = [[cards allKeys] sortedArrayUsingSelector:@selector(compare)];
	self.keys = array;

	// Check all went well & how many cards loaded (items in Array)
	// int cardsCount = [self.cards count]; 
	NSLog(@"Total of cards in array loaded: %i", [self.keys count]);
	NSLog(@"Fields in Dictionary loaded: %i", [self.cards count]);
		  
	// NSLog(@"Cards in array loaded: %i", cardsCount);	// don't work
	
	// Defines which image (put splash/startup image here)
	self.vcimage = [UIImage imageNamed:@"splashscreen.png"];
	// Set image to screen
	imageView.image = vcimage;
	[self.view becomeFirstResponder];
    [super viewDidLoad];
}
The cardArray.plist file looks like this;
Code:
KEY              TYPE         VALUE
Root             Array        (7 items)   // Sample testitems for now
- Item 0        Dictionary  (4 items)
-- cardUrl      String       splashscreen.png
-- cardTxt      String       Startupscreen - hit me...
-- cardCat      String       Fun
-- cardFav     Boolean     [√]  // Checked

- Item 1, Item 2, Item 3, Item 4   etc...
The h-file;
Code:
#import <UIKit/UIKit.h>

@interface ShakeViewController : UIViewController {
	UIImageView		*imageView;
	UIImage			*vcimage;
	NSDictionary	        *cards;
	NSArray			*keys;	
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) UIImage *vcimage;
@property (nonatomic, retain) NSDictionary *cards;
@property (nonatomic, retain) NSArray *keys;

@end
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 05:49 AM   #2 (permalink)
Registered Member
 
sundarrif's Avatar
 
Join Date: May 2009
Location: India
Age: 25
Posts: 30
Send a message via Skype™ to sundarrif
Default

Quote:
Originally Posted by _Mac View Post
Just wanna count the number of items in the array and it's dictionarys to set an max-value for another random-method.
Tried this, but don't get any counts from it. Any tips or pointers? Thanks.;
Code:
- (void)viewDidLoad {
	// Read in cardarray from file
	NSString *path = [[NSBundle mainBundle] pathForResource:@"cardArray" ofType:@"plist"];
	
	// Put dictionarys in array
	NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	self.cards = dict;
	[dict release];
	
	NSArray *array = [[cards allKeys] sortedArrayUsingSelector:@selector(compare)];
	self.keys = array;

	// Check all went well & how many cards loaded (items in Array)
	// int cardsCount = [self.cards count]; 
	NSLog(@"Total of cards in array loaded: %i", [self.keys count]);
	NSLog(@"Fields in Dictionary loaded: %i", [self.cards count]);
		  
	// NSLog(@"Cards in array loaded: %i", cardsCount);	// don't work
	
	// Defines which image (put splash/startup image here)
	self.vcimage = [UIImage imageNamed:@"splashscreen.png"];
	// Set image to screen
	imageView.image = vcimage;
	[self.view becomeFirstResponder];
    [super viewDidLoad];
}
The cardArray.plist file looks like this;
Code:
KEY              TYPE         VALUE
Root             Array        (7 items)   // Sample testitems for now
- Item 0        Dictionary  (4 items)
-- cardUrl      String       splashscreen.png
-- cardTxt      String       Startupscreen - hit me...
-- cardCat      String       Fun
-- cardFav     Boolean     [√]  // Checked

- Item 1, Item 2, Item 3, Item 4   etc...
The h-file;
Code:
#import <UIKit/UIKit.h>

@interface ShakeViewController : UIViewController {
	UIImageView		*imageView;
	UIImage			*vcimage;
	NSDictionary	        *cards;
	NSArray			*keys;	
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) UIImage *vcimage;
@property (nonatomic, retain) NSDictionary *cards;
@property (nonatomic, retain) NSArray *keys;

@end
Code for array count

Code:
int count=[dict count];
int countarray=[array count];
sundarrif is offline   Reply With Quote
Old 02-22-2010, 06:07 AM   #3 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question

Thank's sundarrif. But I most be missing something? Still don't work. Tried to implement your tip below;
Code:
	// Put dictionarys in array
	NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
		int count = [dict count];
		NSLog(@"Total of cards in array loaded: %i", count);
	self.cards = dict;
	[dict release];
	
	NSArray *array = [[cards allKeys] sortedArrayUsingSelector:@selector(compare)];
		int countarray = [array count];
		NSLog(@"Fields in Dictionary loaded: %i", countarray);
	self.keys = array;
But result in terminal looks like this;
Code:
[Session started at 2010-02-22 12:04:46 +0100.]
2010-02-22 12:04:48.277 cards[5915:207] Total of cards in array loaded: 0
2010-02-22 12:04:48.278 cards[5915:207] Fields in Dictionary loaded: 0
OR maybe I didn't load the plist correctly?
__________________
-- Happy Coding

Last edited by _Mac; 02-22-2010 at 06:14 AM.
_Mac is offline   Reply With Quote
Old 02-22-2010, 06:19 AM   #4 (permalink)
Registered Member
 
sundarrif's Avatar
 
Join Date: May 2009
Location: India
Age: 25
Posts: 30
Send a message via Skype™ to sundarrif
Default

Are you sure the value is allocated in Dictionary.
sundarrif is offline   Reply With Quote
Old 02-22-2010, 06:23 AM   #5 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question

Quote:
Originally Posted by sundarrif View Post
Are you sure the value is allocated in Dictionary.
Think so, please have a look at the code at top of post. But I'm just a newbie and could have done it wrong. Thank's for helping out.

Maybe there's an easier / better way to check it?
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 06:28 AM   #6 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question Still unsolved...

This might help you to help me. What I really want to do is;
1) Check the .plist for how many items I've got (setting a maxcount).
2) Select a random item within that array and within the above maxcount.
3) Read the fields (pictureurl + text) & present on screen.

Pretty simple, but I've just got stuck on this for hours...
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 06:53 AM   #7 (permalink)
Registered Member
 
sundarrif's Avatar
 
Join Date: May 2009
Location: India
Age: 25
Posts: 30
Send a message via Skype™ to sundarrif
Default

Quote:
Originally Posted by _Mac View Post
This might help you to help me. What I really want to do is;
1) Check the .plist for how many items I've got (setting a maxcount).
2) Select a random item within that array and within the above maxcount.
3) Read the fields (pictureurl + text) & present on screen.

Pretty simple, but I've just got stuck on this for hours...
In the .plist add Dictionary in the root like below

KEY TYPE VALUE
Root Dictionary (7 items) // Sample testitems for now
- Item 0 Array (4 items)
-- cardUrl String splashscreen.png
-- cardTxt String Startupscreen - hit me...
-- cardCat String Fun
-- cardFav Boolean [√] // Checked
sundarrif is offline   Reply With Quote
Old 02-22-2010, 07:03 AM   #8 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question

Thanks, that partly solved it. Got a crash but I think it's due to that I've got to change Dictionary -> Array within each item. Testing it now...
__________________
-- Happy Coding

Last edited by _Mac; 02-22-2010 at 07:06 AM.
_Mac is offline   Reply With Quote
Old 02-22-2010, 07:07 AM   #9 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

Code:
NSString *path = [[NSBundle mainBundle pathForResource:@"cardArray" ofType:@"plist"];
	
	// Put dictionarys in array
	NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
	NSInteger cou=[dict count];
this will bring you the count. you must create a array not dictionary since your Root is of type Array
sumanth is offline   Reply With Quote
Old 02-22-2010, 07:14 AM   #10 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

Code:
NSString *path = [[NSBundle mainBundle pathForResource:@"cardArray" ofType:@"plist"];
	
	// Put dictionarys in array
	NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
	NSInteger cou=[dict count];//array count
	NSInteger dictionarycou=[[dict objectAtIndex:0]count]//dictionary count
sumanth is offline   Reply With Quote
Old 02-22-2010, 07:21 AM   #11 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question

hmm confused...
Which is it? Does Arrays go into Dictionarys or is it the other way around?

With sundarrif's suggestion (changing root from Array to Dictionary) I got the item-count (of directory) but an error on the arrays (selector) below - but you (sumanth) suggest I do it in the opposite way?

Code:
The Debugger has exited with status 0.
[Session started at 2010-02-22 13:23:04 +0100.]
2010-02-22 13:23:05.970 cards[6608:207] Path = 59909216
2010-02-22 13:23:05.971 cards[6608:207] Total of cards in array loaded: 7
2010-02-22 13:23:05.973 cards[6608:207] *** -[NSCFString compare]: unrecognized selector sent to instance 0x3916d70
2010-02-22 13:23:05.973 cards[6608:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString compare]: unrecognized selector sent to instance 0x3916d70'
2010-02-22 13:23:05.974 cards[6608:207] Stack: (
    29291611,
    2444371209,
    29673531,
    29242998,
    29095618,
    29079175,
    345343,
    345039,
    8494,
    3241794,
    3292062,
    3287177,
    3294702,
    3290326,
    4432528,
    55808688,
    55808111,
    55806150,
    55805242,
    2727589,
    2751284,
    2733695,
    2760801,
    37387609,
    29076352,
    29072456,
    2727445,
    2764719
)
__________________
-- Happy Coding

Last edited by _Mac; 02-22-2010 at 07:27 AM.
_Mac is offline   Reply With Quote
Old 02-22-2010, 07:22 AM   #12 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

just keep your original plist as it is ..dont change that and try the code.

for the plist
Code:
KEY              TYPE         VALUE
Root             Array        (7 items)   // Sample testitems for now
- Item 0        Dictionary  (4 items)
-- cardUrl      String       splashscreen.png
-- cardTxt      String       Startupscreen - hit me...
-- cardCat      String       Fun
-- cardFav     Boolean     [√]  // Checked

- Item 1, Item 2, Item 3, Item 4   etc...
try the below code to get the array count and dictionary count
Code:
NSString *path = [[NSBundle mainBundle pathForResource:@"cardArray" ofType:@"plist"];
	
	// Put dictionarys in array
	NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
	NSInteger cou=[dict count];//array count
	NSInteger dictionarycou=[[dict objectAtIndex:0]count]//dictionary count
even sundarrif solution is correct ,you can follow him if you can fix the error.

Last edited by sumanth; 02-22-2010 at 07:30 AM.
sumanth is offline   Reply With Quote
Old 02-22-2010, 07:43 AM   #13 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Thumbs up SOLVED!

Thanks U2! Finally got it to work :-)
Converted back to the original plist and used the code below from sumanth;
Code:
	// Read in cardarray from file
	NSString *path = [[NSBundle mainBundle] pathForResource:@"cardsArray" ofType:@"plist"];
	
	// Put dictionarys in array (second version)
	NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
	NSInteger dictionarycou=[[dict objectAtIndex:0]count];  //dictionary count
	NSInteger cou=[dict count]; //array count
	NSLog(@"Arrayfields count: %i", dictionarycou);
	NSLog(@"Cards in Dictionary: %i", cou);
Got the result;
Code:
[Session started at 2010-02-22 13:34:53 +0100.]
2010-02-22 13:34:55.686 cards[6673:207] Cards in Dictionary: 4
2010-02-22 13:34:55.687 cards[6673:207] Array fields count: 7
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 07:45 AM   #14 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question May I also ask for...

...how to best actually read the items into the app from the array?
Suggestions? Thank's.

Here's quick recap of what I want to do again in this ViewController;
Quote:
This might help you to help me. What I really want to do is;
√ 1) Check the .plist for how many items I've got (setting a maxcount).
√ 2) Select a random item within that array and within the above maxcount.
3) Read the fields (pictureurl + text) & present on screen.
__________________
-- Happy Coding

Last edited by _Mac; 02-22-2010 at 07:54 AM.
_Mac is offline   Reply With Quote
Old 02-22-2010, 08:17 AM   #15 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

you will get the image name in strimage and text in strtext
Code:
NSString *strimage=[[dict objectAtIndex:0] objectForKey:@"cardUrl"];
NSString *strtext=[[dict objectAtIndex:0] objectForKey:@"cardTxt"];
sumanth is offline   Reply With Quote
Old 02-22-2010, 08:50 AM   #16 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Thumbs up SOLVED! (Again)

Great thx - will try that soon.
And for writing to the .plist? (after changing it to NSMutableArray of course) I suppose there are similar simple code?
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 09:24 AM   #17 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Question

Tested the above but I just get strimage = "61975024" I suppose that I get the imageUrl in a wrong format?
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 09:40 AM   #18 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

you will get splashscreen.png in strimage. Because in the plist the value for the key cardUrl is splashscreen.png.


Code:
UIImageView *imageview=[[UIImageView alloc]init];
	imageview.image=[UIImage imageNamed:strimage];
sumanth is offline   Reply With Quote
Old 02-22-2010, 09:46 AM   #19 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Default

Tried to do this;
Code:
NSString *strimage=[[dict objectAtIndex:0] objectForKey:@"cardUrl"];
	NSLog(@"ImageUrl = %i", strimage);
and got this;
Code:
[Session started at 2010-02-22 15:44:27 +0100.]
2010-02-22 15:44:29.255 cards[7192:207] Cards in Dictionary: 7
2010-02-22 15:44:29.256 cards[7192:207] Array fields count: 4
2010-02-22 15:44:29.257 cards[7192:207] ImageUrl = 63992704
__________________
-- Happy Coding
_Mac is offline   Reply With Quote
Old 02-22-2010, 09:50 AM   #20 (permalink)
Registered Member
 
sumanth's Avatar
 
Join Date: Jul 2009
Location: India
Posts: 78
Default

give this,

NSLog(strimage);

or

NSLog(@"ImageUrl = %@", strimage);
sumanth is offline   Reply With Quote
Old 02-22-2010, 10:10 AM   #21 (permalink)
User Interface expert
 
Join Date: May 2009
Location: Sweden
Posts: 105
Thumbs up SOLVED! (Again again ;-)

Works like a charm :-) THX!
Code:
[Session started at 2010-02-22 16:08:41 +0100.]
2010-02-22 16:08:46.132 cards[7279:207] Cards in Dictionary: 7
2010-02-22 16:08:46.133 cards[7279:207] Array fields count: 4
2010-02-22 16:08:46.134 cards[7279:207] ImageUrl = splashscreen.png
__________________
-- Happy Coding
_Mac 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: 274
21 members and 253 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, HemiMG, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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