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

View Single Post
Old 04-26-2009, 12:20 PM   #2 (permalink)
brandon0104
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
 

» Advertisements
» Stats
Members: 158,812
Threads: 89,206
Posts: 380,629
Top Poster: BrianSlick (7,129)
Welcome to our newest member, shoppingcenter
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:41 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.