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 01-04-2011, 04:21 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 5
soundoc is on a distinguished road
Smile Random words from plist to 7 labels

I have a plist of 352 words listed as items 0-351 and I want to load them into 7 labels (label1- label7) randomly. I am a newbee and I just need a start.

the plist file is called "Words.plist" and the array in the plist file is "gameWords".

Thanks for the help in advance.
soundoc is offline   Reply With Quote
Old 01-04-2011, 06:51 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 soundoc View Post
I have a plist of 352 words listed as items 0-351 and I want to load them into 7 labels (label1- label7) randomly. I am a newbee and I just need a start.

the plist file is called "Words.plist" and the array in the plist file is "gameWords".

Thanks for the help in advance.
Assuming your plist contains an array and that it's stored in your documents directory, use code like this:

Code:
NSString *documentsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString* pathname = [documentsDirectory @"Words.plist"];
NSArray* gameWords = [NSArray arrayWithContentsOfFile: pathname];

NSInteger count = [gameWords count];
label1.text = [gameWords objectAtIndex: arc4random() % count];
label2.text = [gameWords objectAtIndex: arc4random() % count];
__________________
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 01-05-2011, 09:26 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 5
soundoc is on a distinguished road
Default Thanks so much

Quote:
Originally Posted by Duncan C View Post
Assuming your plist contains an array and that it's stored in your documents directory, use code like this:

Code:
NSString *documentsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString* pathname = [documentsDirectory @"Words.plist"];
NSArray* gameWords = [NSArray arrayWithContentsOfFile: pathname];

NSInteger count = [gameWords count];
label1.text = [gameWords objectAtIndex: arc4random() % count];
label2.text = [gameWords objectAtIndex: arc4random() % count];
Thank you Duncan, I see it now..
soundoc is offline   Reply With Quote
Old 01-06-2011, 11:16 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 5
soundoc is on a distinguished road
Default Tried it and getting 2 errors

Quote:
Originally Posted by Duncan C View Post
Assuming your plist contains an array and that it's stored in your documents directory, use code like this:

Code:
NSString *documentsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString* pathname = [documentsDirectory @"Words.plist"];
NSArray* gameWords = [NSArray arrayWithContentsOfFile: pathname];

NSInteger count = [gameWords count];
label1.text = [gameWords objectAtIndex: arc4random() % count];
label2.text = [gameWords objectAtIndex: arc4random() % count];
The first error is on the [doumentsDirectory @"Words.plist"]; line. the complier error reads: Expected ':' before 'OBJC_STRING' token
The second is at the first label.text statement. The app freezes the simulator.
soundoc is offline   Reply With Quote
Old 01-07-2011, 12:26 PM   #5 (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 soundoc View Post
The first error is on the [doumentsDirectory @"Words.plist"]; line. the complier error reads: Expected ':' before 'OBJC_STRING' token
The second is at the first label.text statement. The app freezes the simulator.

It looks like there was an editing error in the code I posted. It should read like this (change in bold):

Code:
NSString *documentsDirectory = 
  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString* pathname = [documentsDirectory stringByAppendingPathComponent: @"Words.plist"];
NSArray* gameWords = [NSArray arrayWithContentsOfFile: pathname];

NSInteger count = [gameWords count];
label1.text = [gameWords objectAtIndex: arc4random() % count];
label2.text = [gameWords objectAtIndex: arc4random() % count];

As for the hang, that makes no sense. With the editing error in my first post, the gameWords array should fail to read, so the variable gameWords should be nil. Sending a message to nil should do nothing, so the two assignments to label1.text and label2.text should just silently fail.

If you put a breakpoint before and after those statements, or a log statement before and after, does it definitely hang on one of those assignments?

BTW, most of us that post code in response to questions just bang out the code into the forum. We don't run it through the compiler for syntax checking, or test it. Our intention is to give you the general idea, with the expectation that you will clean it up and integrate it with your code.
__________________
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 01-07-2011, 01:25 PM   #6 (permalink)
"Sorry, but I dont know."
 
Join Date: Dec 2009
Location: Switzerland (CH)
Age: 16
Posts: 213
mavrick3 is on a distinguished road
Default

Attention! It's possible that the iPhone takes twice the same object with Duncans solution!
mavrick3 is offline   Reply With Quote
Old 01-07-2011, 01:37 PM   #7 (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 mavrick3 View Post
Attention! It's possible that the iPhone takes twice the same object with Duncans solution!
Huh? "takes twice the same object?" What does that mean?

You mean that more than one label could get the same text? That's true. The OP did not say that he wanted the random text to be random, non-repeating text. That would require a bit more code.

I have a post linked in my signature that shows how to generate random, non-repeating strings. That code could be easily adapted to create random, non-repeating button labels.
__________________
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 01-10-2011, 11:45 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 5
soundoc is on a distinguished road
Wink Thanks for your help

Quote:
Originally Posted by Duncan C View Post
Huh? "takes twice the same object?" What does that mean?

You mean that more than one label could get the same text? That's true. The OP did not say that he wanted the random text to be random, non-repeating text. That would require a bit more code.

I have a post linked in my signature that shows how to generate random, non-repeating strings. That code could be easily adapted to create random, non-repeating button labels.
I got it to work.
I have these in the'.h' file
int count
NSArray *gamewords


//this is what worked in the '.m' file
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Words" ofType:@"plist"];
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dataPath];
gameWords = [plistDict objectForKey:@"gameWords"];

// initial load of words onto labels randomly
count = [gameWords count];
label1.text = [gameWords objectAtIndex:arc4random() % count];
label2.text = [gameWords objectAtIndex:arc4random() % count];
label3.text = [gameWords objectAtIndex:arc4random() % count];
label4.text = [gameWords objectAtIndex:arc4random() % count];
label5.text = [gameWords objectAtIndex:arc4random() % count];
label6.text = [gameWords objectAtIndex:arc4random() % count];
label7.text = [gameWords objectAtIndex:arc4random() % count];

Thanks so much for all your help
Tom
soundoc is offline   Reply With Quote
Reply

Bookmarks

Tags
label, label with text, plist

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: 362
7 members and 355 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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