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 02-25-2011, 03:35 AM   #1 (permalink)
J-E
Registered Member
 
Join Date: Feb 2011
Posts: 6
J-E is on a distinguished road
Unhappy HowTo read random lines from plist ?

Hi ! I'm an absolute beginner in XCode and Objective-C, that allmost sounded like the title of the book I'm reading I'm trying to do a small app that read a random line from a text file, when pressing a button on the screen, and then display the result. After a lot of reading in different forums I did find a thread that explained that the best way to it, was to use "plist" instead of a plain textfile... The "jhaList.plist" file is an array, consisting a few lines (strings). Right now it consists of 5 lines:

Line 1
Line 2
Line 3
Line 4
Line 5

But when I get this to work, I want to have about 500 lines in it !

So this is my code so far:

The .h file:
------------
Code:
#import <UIKit/UIKit.h>

@interface jha_003ViewController : UIViewController {

	IBOutlet UILabel *randLine;	
}
NSString *fileContents;

@property (retain,nonatomic) IBOutlet UILabel *randLine;

-(IBAction)randomNumber:(id)sender;

@end

The .m file:
--------------
Code:
#import "jha_003ViewController.h"

@implementation jha_003ViewController
@synthesize randLine;

- (IBAction)randomNumber:(id)sender {
	
	fileContents = [[NSBundle mainBundle] pathForResource:@"jhaList" ofType:@"plist"];
	
	NSDictionary *jhaList = [[NSDictionary alloc] initWithContentsOfFile:fileContents];
	
	NSMutableArray *jhaArray = [jhaList valueForKey:@"jhaArray"];
	
	int randomNumber = arc4random() % [jhaArray count];
	jhaArray = [jhaArray objectAtIndex:randomNumber];
	for (NSString *randomNumber in jhaArray)
		randLine.text = (@"%@", randomNumber);
}
----------------------

But when running the app, it does display the "generate" button linked to "randomNumber" as expected, when pressing it, the app just quit and just jumps out ! It seems that I don't get any value back to the "randLine" which is connected to an UILabel. Can someone point me in the right direction ? As I said I'm a absolute beginner, so please give basic instructions

//J-E

Last edited by J-E; 02-25-2011 at 07:20 AM. Reason: No code tags
J-E is offline   Reply With Quote
Old 02-25-2011, 05:37 AM   #2 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 489
jNoxx is on a distinguished road
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

Read the sticky about posting new posts.

http://www.iphonedevsdk.com/forum/ip...new-topic.html

With other words, use CODE tags. otherwise no one is interested in helping u, no personal offense.
jNoxx is offline   Reply With Quote
Old 02-25-2011, 06:18 AM   #3 (permalink)
J-E
Registered Member
 
Join Date: Feb 2011
Posts: 6
J-E is on a distinguished road
Default

Thanks jNoxx, for pointing that out ! I'm sorry, but I did not know !
I added [ code] and [ /code] before and after the code. It didn't do any difference, the smileys did show anyway ! So I removed since it didn't work ! Can you give me an example, because it's really anoying to have these smileys in the code ?

Thanks for your patience !
J-E
J-E is offline   Reply With Quote
Old 02-25-2011, 06:23 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

when you post there is a # symbol at the top of the text box

Code:
any text that is within the two tags that appear when you hit the # symbol will be up as code not just text
i cant remember jnoxx's words but i think he got a little muddled when posting that, you need to get rid of the spaces between the [ and c or /. I am not entirely certain about the caps - i always do it manually like this:
[ CODE] [ /CODE] but with NO spaces.
Meredi86 is offline   Reply With Quote
Old 02-25-2011, 06:29 AM   #5 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 489
jNoxx is on a distinguished road
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

Yea, sorry, that's what I said in my post. It says, use the # on top of a post (not the quick post, then press "Go Advanced", and second. I also said, u have to delete the spaces between them, because otherwise i wouldve got me a clean code box, so then u wouldn't understand.
So try fixing it! good luck
jNoxx is offline   Reply With Quote
Old 02-25-2011, 06:34 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

ah sorry jnoxx, though you said (leave spaces) i could see how some people around here may find that confusing and not be able to debug that message.
Meredi86 is offline   Reply With Quote
Old 02-25-2011, 06:41 AM   #7 (permalink)
J-E
Registered Member
 
Join Date: Feb 2011
Posts: 6
J-E is on a distinguished road
Default

Thanks Meredi86 ! Now it looks a lot better !
J-E is offline   Reply With Quote
Old 02-25-2011, 08:35 AM   #8 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 489
jNoxx is on a distinguished road
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

Quote:
Originally Posted by Meredi86 View Post
ah sorry jnoxx, though you said (leave spaces) i could see how some people around here may find that confusing and not be able to debug that message.
Changing it, thanks for the pointer
jNoxx is offline   Reply With Quote
Old 03-06-2011, 02:13 PM   #9 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

is there a solution for this?
cityofangels is offline   Reply With Quote
Old 03-06-2011, 02:44 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

plists can be read as arrays, and then its a very simple matter of picking a random object from that array
kapps11 is offline   Reply With Quote
Old 03-06-2011, 03:34 PM   #11 (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 J-E View Post
Hi ! I'm an absolute beginner in XCode and Objective-C, that allmost sounded like the title of the book I'm reading I'm trying to do a small app that read a random line from a text file, when pressing a button on the screen, and then display the result. After a lot of reading in different forums I did find a thread that explained that the best way to it, was to use "plist" instead of a plain textfile... The "jhaList.plist" file is an array, consisting a few lines (strings). Right now it consists of 5 lines:

Line 1
Line 2
Line 3
Line 4
Line 5

But when I get this to work, I want to have about 500 lines in it !

So this is my code so far:

The .h file:
------------
Code:
#import <UIKit/UIKit.h>

@interface jha_003ViewController : UIViewController {

	IBOutlet UILabel *randLine;	
}
NSString *fileContents;

@property (retain,nonatomic) IBOutlet UILabel *randLine;

-(IBAction)randomNumber:(id)sender;

@end

The .m file:
--------------
Code:
#import "jha_003ViewController.h"

@implementation jha_003ViewController
@synthesize randLine;

- (IBAction)randomNumber:(id)sender {
	
	fileContents = [[NSBundle mainBundle] pathForResource:@"jhaList" ofType:@"plist"];
	
	NSDictionary *jhaList = [[NSDictionary alloc] initWithContentsOfFile:fileContents];
	
	NSMutableArray *jhaArray = [jhaList valueForKey:@"jhaArray"];
	
	int randomNumber = arc4random() % [jhaArray count];
	jhaArray = [jhaArray objectAtIndex:randomNumber];
	for (NSString *randomNumber in jhaArray)
		randLine.text = (@"%@", randomNumber);
}
----------------------

But when running the app, it does display the "generate" button linked to "randomNumber" as expected, when pressing it, the app just quit and just jumps out ! It seems that I don't get any value back to the "randLine" which is connected to an UILabel. Can someone point me in the right direction ? As I said I'm a absolute beginner, so please give basic instructions

//J-E
You say your plist contains an array, but the code you posted reads the file as a dictionary, then reads an array from that dictionary. How are you creating this plist file?

And you say it just quits and jumps out. You need to run the program in the debugger and step through it line by line, checking the values of all your variables until you find the line that is crashing.

If I had to guess, I would guess that you are not reading your array properly, so it has zero elements. Then when you try to do the "arc4random() % [jhaArray count]" statement, you're crashing on a divide-by-zero error (the modulo operator is a remainder operator, and does a divide to find the answer.)
__________________
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
Reply

Bookmarks

Tags
array, plist, random

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: 372
19 members and 353 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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