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 04-26-2009, 12:34 AM   #1 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default random text generator

Hi, I am a real newbie to programming on iphones. What I am trying to do is basically make a joke app for a school project. It starts with a navigation based interface, for categories of jokes. Once you tap a category, it goes to a view with a label and a button. You tap the button to randomly pick and display a joke (from a text file or something like that).
Because i am very new to this, though, I have mostly been making my app out of tutorials. I already have made my navigation based app; with subviews and all. Tapping a subview gets a view with one round button and a label. However, I have not yet set up the whole random part.

Right now, when I tap the button, it says "set by [whichever category it is in]" because the tutorial (noob, I know) said I should put a code to make it access global application data. I'm still not sure if I need this for my app, I have a feeling it would actually mess it up.

My questions:
Where and how do I construct my text file with all the jokes for a certain category?
What is the code to make it randomly pick a joke from the text, and where do I insert this code?
Do I need the global application data accessing code?

Thanks a whole, whole lot.
dab is offline   Reply With Quote
Old 04-26-2009, 12:20 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Posts: 10
Default

Probably one of the easiest ways is to read from a property list file (plist). To do this.

Code:
NSString *fileContents = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];

NSDictionary *jokeDic = [[NSDictionary alloc] initWithContentsOfFile:fileContents];

NSMutableArray *items = [categoryDic valueForKey:@"items"];
After you've read your data from your plist file. You can then select a random joke from the items array.

Code:
int randomNumber = arc4random() % [items count];
NSString *joke = [items objectAtIndex:randomNumber];
[someUILabel setText:joke];

Here is an example PLIST file.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>items</key>
	<array>
		<string>joke 1</string>
		<string>joke 2</string>
		<string>joke 3</string>
	</array>
	
</dict>
</plist>
I think that should work, if it doesn't let me know.
brandon0104 is offline   Reply With Quote
Old 04-26-2009, 03:39 PM   #3 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default

I think I understand the concept of what you're saying. But, how do I save as a .plist? I tried using word, pages, and textedit and none let me.

And also, where do I insert this?
NSString *fileContents = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];

NSDictionary *jokeDic = [[NSDictionary alloc] initWithContentsOfFile:fileContents];

NSMutableArray *items = [categoryDic valueForKey:@"items"];

and do I need to change anything to that ^? ("pathforresource and @'filename'")?

Sorry if these are dumb questions, but im new to this and I don't want to mess anything up.
dab is offline   Reply With Quote
Old 04-26-2009, 04:01 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Posts: 10
Default

Just create a new PLIST in Xcode. File > New File > Other > Property List File. Name it anything you would like, just change :@"filename" to the name of your file, NOT including ".plist".

Once you begin to edit you will see your Root which is your dictionary (<dict>) . Add as many arrays as you would like.

Change [categoryDic valueForKey:@"items"]; to whatever you name your Array. For example

Code:
NSMutableArray *items = [categoryDic valueForKey:@"sillyjokes"];
If the name of your array is "sillyjokes". ' I've uploaded an example PLIST file.

You insert the code I posted before in the action of your button. since the NSString *fileContents is used multiple times its easier to place it in your header file. Same with UILabel *someLabel;

Code:
- (IBAction)randomSillyJoke:(id)sender {
fileContents = [[NSBundle mainBundle] pathForResource:@"sillyjokes" ofType:@"plist"];

NSDictionary *sillyJokes = [[NSDictionary alloc] initWithContentsOfFile:fileContents];

NSMutableArray *sillyjokes = [categoryDic valueForKey:@"jokes"];

int randomNumber = arc4random() % [items count];
joke = [sillyJokes objectAtIndex:randomNumber];
[someUILabel setText:joke];
}
- (IBAction)randomDirtyJoke:(id)sender {
fileContents = [[NSBundle mainBundle] pathForResource:@"dirtyjokes" ofType:@"plist"];

NSDictionary *dirtyJokes = [[NSDictionary alloc] initWithContentsOfFile:fileContents];

NSMutableArray *dirtyjokes = [categoryDic valueForKey:@"jokes"];

int randomNumber = arc4random() % [items count];
joke = [dirtyJokes objectAtIndex:randomNumber];
[someUILabel setText:joke];
}



Hope this helps
Attached Files
File Type: zip sillyjokes.zip (722 Bytes, 199 views)
brandon0104 is offline   Reply With Quote
Old 04-26-2009, 06:21 PM   #5 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default

I set up the plist successfully (thanks!!!) but im still having trouble.
I modified the code in your last post so it should work with the plist and array I made, and then I put it in my subviewonecontroller.m. When I tried in the .h like you suggested, it made even more errors.

Here is a screenshot of what happened:


The last error I think I can solve by deleting that part. But, I really need help with the others..they don't make much sense.
dab is offline   Reply With Quote
Old 04-26-2009, 06:38 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Posts: 10
Default

First, you cant have multiple definitions of the same method. OnButtonClick is defined twice, you need to combine the two methods. I'll get to that later.
fileContents is an NSString and needs to be declared in your Header file.


Code:
NSString *fileContents;
or if you dont plan on using it in more than one method you dont have to, just add NSString *fileContents = blah blah blah...


You didn't quite finish. You renamed categoryDic, "elevatorJokes" but didn't change it in [categoryDic valueForKey...

Same with your NSMutableArray eleJokes. [items count]; should be [eleJokes count];

[sillyJokes objectAtIndex:randomNumber]; needs to be [eleJokes objectAtIndex:randomNumber];

Now, copy every inside your second OnButtonClick. Paste it inside the first one.

Delete the displayString line.

And edit [appDelegate setModelData:displayString]; to

Code:
[appDelegate setModelData:joke];

I think that about takes care of it...

Also, you will want to give your variables more meaningful names. Such as elevatorJokesDict or eleJokesMutArray. That way in the future you actually know what they are.

Last edited by brandon0104; 04-26-2009 at 07:56 PM.
brandon0104 is offline   Reply With Quote
Old 04-26-2009, 09:32 PM   #7 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default

4 Errors to go, I think 2 can be taken down at once

As you can see in the pic below, once I merged the second "onbuttonclick" function, I got problems with the code below it. It seems having the onbuttonclick code there made it work;now that its merged the stuff below doesnt work. I tried a few things like deleting that piece of code (ondatachangeevent), and that in turn made the stuff below it have errors.
Also, how exactly do I declare joke and elejokes? I do want to use filecontents for more than one thing. I have "NSString *fileContents;" so far, do I do something like NSString *fileContents = etc on the line below it?

Thanks again, you are being very helpful.


Last edited by dab; 04-26-2009 at 09:38 PM.
dab is offline   Reply With Quote
Old 04-26-2009, 09:39 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Posts: 10
Default

I think you're biting off a bit more than you can chew. You need to work through some more tutorials and work on little projects first. This helps a lot, trust me I've been where you are.

1. [appDelegate setModalData: joke]; is placed before joke is even declared. Thats why you're getting that error.

2. joke = [eleJokes object.... is spelled differently than your original elejokes mutable array.

3. UILabel is class, not a variable. As I said in the last post you should delete that line anyway.

4. You didn't even close your brackets. "}"
brandon0104 is offline   Reply With Quote
Old 04-26-2009, 10:14 PM   #9 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default

Oh, I completely agree. Quite a bit of this is over my head (even though I understand a little :P). Thing is, I don't have enough time to do all those tutorials and such, I just have to jump right in.

I finally figured out how to make this work--I had to rename all the "joke" references to "elejokes". Now, it works.

I really can't say how thankful I am of you You have been very helpful. When I finish the app, im going to make sure to recognize you in the credits. Anyway, thanks again.
dab is offline   Reply With Quote
Old 04-30-2009, 11:18 PM   #10 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default

Quote:
Originally Posted by dab View Post
4 Errors to go, I think 2 can be taken down at once

As you can see in the pic below, once I merged the second "onbuttonclick" function, I got problems with the code below it. It seems having the onbuttonclick code there made it work;now that its merged the stuff below doesnt work. I tried a few things like deleting that piece of code (ondatachangeevent), and that in turn made the stuff below it have errors.
Also, how exactly do I declare joke and elejokes? I do want to use filecontents for more than one thing. I have "NSString *fileContents;" so far, do I do something like NSString *fileContents = etc on the line below it?

Thanks again, you are being very helpful.


Hey I am new to developing now and was reading this post and was wondering where did you define -elejokes-
appleman is offline   Reply With Quote
Old 04-30-2009, 11:28 PM   #11 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Post

Quote:
Originally Posted by brandon0104 View Post
I think you're biting off a bit more than you can chew. You need to work through some more tutorials and work on little projects first. This helps a lot, trust me I've been where you are.

1. [appDelegate setModalData: joke]; is placed before joke is even declared. Thats why you're getting that error.

2. joke = [eleJokes object.... is spelled differently than your original elejokes mutable array.

3. UILabel is class, not a variable. As I said in the last post you should delete that line anyway.

4. You didn't even close your brackets. "}"
I am trying to create an app that displays random text and when I use

Code:
[appDelegate setModelData:jokes];
A warning comes up that says my delegate might not respond to "setModelData" what could be the reason for this?
appleman is offline   Reply With Quote
Old 05-01-2009, 12:31 AM   #12 (permalink)
dab
New Member
 
Join Date: Apr 2009
Posts: 7
Default

Quote:
Hey I am new to developing now and was reading this post and was wondering where did you define -elejokes-
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.

Quote:
A warning comes up that says my delegate might not respond to "setModelData" what could be the reason for this?
Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.

EDIT read this-
actually i think you might have something different with the warning, make sure that its like this

fileContents = [[NSBundle mainBundle] pathForResource:@"y" ofType:@"plist"];
NSDictionary *y = [[NSDictionary alloc] initWithContentsOfFile:fileContents];
NSMutableArray *x = [y valueForKey:@"x"];

int randomNumber = arc4random() % [x count];
x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}

with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.

Last edited by dab; 05-01-2009 at 12:38 AM.
dab is offline   Reply With Quote
Old 05-02-2009, 09:06 PM   #13 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default

Quote:
Originally Posted by dab View Post
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.



Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.

EDIT read this-
actually i think you might have something different with the warning, make sure that its like this

fileContents = [[NSBundle mainBundle] pathForResource:@"y" ofType:@"plist"];
NSDictionary *y = [[NSDictionary alloc] initWithContentsOfFile:fileContents];
NSMutableArray *x = [y valueForKey:@"x"];

int randomNumber = arc4random() % [x count];
x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}

with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.
Ok maybe you can tell me what is going on all I did was add everything hear to my application. Whenever I run my application in 2.2.1 and I click on a table row that when touched goes to the second view the app crashes. But when I run the app in 3.0 the app does not crash when I perform the action described above. I think there might be an error because my second view is populated from the app delegate. But if this is the problem then why would it work in 3.0 and not 2.2.1 any ideas?
appleman is offline   Reply With Quote
Old 05-02-2009, 09:40 PM   #14 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default

Quote:
Originally Posted by dab View Post
NSMutableArray *elejokes = [jokes valueForKey:@"elejokes"];
defined elejokes. i was having a problem with it because I capitalized the J there, it should have been lower case like the others.



Its kind of weird, I get that same thing in the same place sometimes (i have no idea why) but it has never prevented the app from running or crashed it or anything like that. so I just ignore it, if you have everything else fixed you should be fine.

EDIT read this-
actually i think you might have something different with the warning, make sure that its like this

fileContents = [[NSBundle mainBundle] pathForResource:@"y" ofType:@"plist"];
NSDictionary *y = [[NSDictionary alloc] initWithContentsOfFile:fileContents];
NSMutableArray *x = [y valueForKey:@"x"];

int randomNumber = arc4random() % [x count];
x = [x objectAtIndex:randomNumber];
[appDelegate setModelData:x];
}

with y as the name of the plist
and x as the name of the array.
if you make sure it is just like this with x and y replaced it SHOULD NOT have a warning.
Never mind last post for some reason Interface Builder threw in a UiSearchDisplay controller.
appleman is offline   Reply With Quote
Old 05-06-2009, 06:25 PM   #15 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default

I am a noobie and trying to understand the code posted...I think I have a good handle on your code. the thing I don't understand is:

[appDelegate setModelData:x]

Why did it replace [someUILabel setText:joke]?

Is it necessary to use Delegate to work? I am building my own exercise with a label and a button..so that when a button is pushed..a random line will be displayed in the UIlabel.
Shogun308 is offline   Reply With Quote
Old 05-07-2009, 06:09 PM   #16 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default Can't figure out..

I followed the posts above to try to try it myself..but I can't get rid of the warning message..

Shogun308 is offline   Reply With Quote
Old 05-31-2009, 10:00 PM   #17 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Smile I got it working!!!

please pm me to find out how
maclife30 is offline   Reply With Quote
Old 06-01-2009, 02:43 AM   #18 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 679
Default

perhaps you could just manual enter all of the jokes through code... it would appear to be easier in my opinion
Code:
NSArray *myranjoke = [NSArray arrayWithObjects: 
				@"Joke #1 goes here",
				@"Joke #2 goes here",
				@"Joke #3 goes here,
				@"Joke #4 goes here", nil]; // you can add as many as you want.. not just 4
	int chosen = arc4random() % [myranjoke count];
	nameofUiLabel.text = [myranjoke objectAtIndex: chosen];
orange gold is offline   Reply With Quote
Old 06-13-2009, 06:10 PM   #19 (permalink)
iPhone App Developer
 
Join Date: Jun 2009
Posts: 25
Unhappy Still having trouble!!!

I have the same Idea as you and am trying to make a joke app! Can you post the final source code of the app so I can download it to see what I did wrong? I keep getting so many errors!
ccctennis is offline   Reply With Quote
Old 06-13-2009, 08:46 PM   #20 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

Quote:
Originally Posted by ccctennis View Post
I have the same Idea as you and am trying to make a joke app! Can you post the final source code of the app so I can download it to see what I did wrong? I keep getting so many errors!
I would like to see the source code too.

Quote:
Originally Posted by orange gold View Post
perhaps you could just manual enter all of the jokes through code... it would appear to be easier in my opinion
Code:
NSArray *myranjoke = [NSArray arrayWithObjects: 
				@"Joke #1 goes here",
				@"Joke #2 goes here",
				@"Joke #3 goes here,
				@"Joke #4 goes here", nil]; // you can add as many as you want.. not just 4
	int chosen = arc4random() % [myranjoke count];
	nameofUiLabel.text = [myranjoke objectAtIndex: chosen];
It might be easier, but it wouldn't be as neat. It would be easier to edit if you created them from a plist then edited it.
kohjingyu is offline   Reply With Quote
Old 06-22-2009, 12:11 AM   #21 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Post

Quote:
Originally Posted by brandon0104 View Post
Probably one of the easiest ways is to read from a property list file (plist). To do this.

Code:
NSString *fileContents = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];

NSDictionary *jokeDic = [[NSDictionary alloc] initWithContentsOfFile:fileContents];

NSMutableArray *items = [categoryDic valueForKey:@"items"];
After you've read your data from your plist file. You can then select a random joke from the items array.

Code:
int randomNumber = arc4random() % [items count];
NSString *joke = [items objectAtIndex:randomNumber];
[someUILabel setText:joke];

Here is an example PLIST file.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>items</key>
	<array>
		<string>joke 1</string>
		<string>joke 2</string>
		<string>joke 3</string>
	</array>
	
</dict>
</plist>
I think that should work, if it doesn't let me know.
Thanks for all the help that you gave out I have a question how can you set

Code:
int randomNumber = arc4random() % [items count];
NSString *joke = [items objectAtIndex:randomNumber];
[someUILabel setText:joke];
to change in sequence on the plist positive or set it to change negative from where is already is?
appleman is offline   Reply With Quote
Old 06-22-2009, 07:59 AM   #22 (permalink)
German iPhone dev
 
Join Date: Jun 2009
Location: Düsseldorf
Age: 19
Posts: 28
Send a message via AIM to Gi-lo
Default

For me it works
Gi-lo is offline   Reply With Quote
Old 12-22-2009, 12:55 PM   #23 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 131
Default

i have done something similar to this, but what i need to know is, is there any way of making it customizable so the user can add/delete there own strings??
eski is offline   Reply With Quote
Reply

Bookmarks

Tags
generator, random, text

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: 896
21 members and 875 guests
2e1fmo, at0m87, aziz, Azuresilver, coding, Desert Diva, el.severo, fiftysixty, imran_ime4u, iVenh, jjaaxx44, nicko, pratikchandak, Promo Dispenser, ramonpadillas, RoryHarvey, Thomas, ziocleto, Zuningo
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,312
Threads: 89,035
Posts: 379,822
Top Poster: BrianSlick (7,086)
Welcome to our newest member, 2e1fmo
Powered by vBadvanced CMPS v3.1.0

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