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...
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...
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
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...
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
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
)
...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.
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?