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 09-06-2010, 11:40 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default How do I get my program to read from a text file ?

I have an IBACTION WITH THE FOLLOWING CODE:


-(IBAction)randomWord {


wordArray = [[NSArray alloc] initWithObjects:
@"ANT", @"BAT", @"CAT", @"DOG", @"ELK", @"FAT", @"GUT", @"HIT", @"INK", @"JET",
@"KNOT", @"LIKE", @"MAN", @"NOT", @"OX", @"PAT", @"MIX", @"RAT", @"SIT", @"TIN",
@"IT", @"VENT", @"WET", @"FIX", @"YAK", @"ZEBRA", @"HAT", @"AX", @"SAT", @"FAX",
@"FIT", @"MILK", @"HAIR", @"LION", nil];


NSInteger index = arc4random() % [wordArray count];
label.text = [wordArray objectAtIndex: index];

// perform selector after delay
[self performSelector:@selector(ClearLabel) withObject:nil afterDelay:2];

}

- (void)ClearLabel

{
label.text = @"";
}

I want to get rid of the Array of words and get my program to pull a Random word from a text file instead. That way when I need to update the App with more vocabulary I can just update a text file instead.
rrichar is offline   Reply With Quote
Old 09-06-2010, 03:58 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by rrichar View Post
I have an IBACTION WITH THE FOLLOWING CODE:


-(IBAction)randomWord {


wordArray = [[NSArray alloc] initWithObjects:
@"ANT", @"BAT", @"CAT", @"DOG", @"ELK", @"FAT", @"GUT", @"HIT", @"INK", @"JET",
@"KNOT", @"LIKE", @"MAN", @"NOT", @"OX", @"PAT", @"MIX", @"RAT", @"SIT", @"TIN",
@"IT", @"VENT", @"WET", @"FIX", @"YAK", @"ZEBRA", @"HAT", @"AX", @"SAT", @"FAX",
@"FIT", @"MILK", @"HAIR", @"LION", nil];


NSInteger index = arc4random() % [wordArray count];
label.text = [wordArray objectAtIndex: index];

// perform selector after delay
[self performSelector:@selector(ClearLabel) withObject:nil afterDelay:2];

}

- (void)ClearLabel

{
label.text = @"";
}

I want to get rid of the Array of words and get my program to pull a Random word from a text file instead. That way when I need to update the App with more vocabulary I can just update a text file instead.
This is the second post today on this subject.

See my post in this old thread:

Random text


The code I posted lets you pull random words/phrases from an array without repeating any.
__________________
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 09-06-2010, 07:14 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

I checked out your code.

I added you code that you referred too. I tried to do the following with your functions and got a bunch of errors.

- (void)viewDidLoad {
[super viewDidLoad];
[self buildRandomWordArray];

}

I put [self buildRandomWordArray]; in view did load because it only needs to be called once.

-(IBAction)randomWord {

[self getRandomWord];
label.text = result;

}

error: 'result' undeclared (first use in this function)

At top level:

warning: property 'words' requires method '-words' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'words' requires the method 'setWords:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires method '-randomArray' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires the method 'setRandomArray:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires method '-array' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires the method 'setArray:' to be defined - use @synthesize, @dynamic or provide a method implementation
rrichar is offline   Reply With Quote
Old 09-06-2010, 07:42 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by rrichar View Post
I checked out your code.

I added you code that you referred too. I tried to do the following with your functions and got a bunch of errors.

- (void)viewDidLoad {
[super viewDidLoad];
[self buildRandomWordArray];

}

I put [self buildRandomWordArray]; in view did load because it only needs to be called once.

-(IBAction)randomWord {

[self getRandomWord];
label.text = result;

}

error: 'result' undeclared (first use in this function)

At top level:

warning: property 'words' requires method '-words' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'words' requires the method 'setWords:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires method '-randomArray' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires the method 'setRandomArray:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires method '-array' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires the method 'setArray:' to be defined - use @synthesize, @dynamic or provide a method implementation
Dude,

I'm not teaching a one-on-one free class in remedial Objective C. I wrote the post I referred to assuming a basic level of knowledge.

You are making tons of mistakes that show me that you have no idea how to program for iOS.

The method getRandomWord returns a string object. You have to do something with that returned value. There is no variable "result" unless you declare one.

Code:
-(IBAction)randomWord 
{
  NSString* result;
  result = [self getRandomWord];
  label.text = result;
}

All the errors about properties are telling you exactly what you need to do. You need to add @synthesize statements for all the properties you declare, or write custom getter/setter methods. In this case, just add @synthesize statements.

Download the Objective C 2.0 reference from Apple site and read it. Do a search on @synthesize and find where it is supposed to go in your code.

You may also want to buy a book on beginning iOS development. There is a sticky thread at the top of this forum on recommended iOS books.

If you can't figure out the documentation, take a look at one of Apple's sample applications that are included with the SDK. Search in the XCode docs for HelloWorld. The app delegate and the view controller in that program both declare a couple of properties and use @synthesize.
__________________
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 09-06-2010, 08:21 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

Quote:
Originally Posted by rrichar View Post
I checked out your code.

I added you code that you referred too. I tried to do the following with your functions and got a bunch of errors.

- (void)viewDidLoad {
[super viewDidLoad];
[self buildRandomWordArray];

}

I put [self buildRandomWordArray]; in view did load because it only needs to be called once.

-(IBAction)randomWord {

[self getRandomWord];
label.text = result;

}

error: 'result' undeclared (first use in this function)

At top level:

warning: property 'words' requires method '-words' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'words' requires the method 'setWords:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires method '-randomArray' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'randomArray' requires the method 'setRandomArray:' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires method '-array' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'array' requires the method 'setArray:' to be defined - use @synthesize, @dynamic or provide a method implementation
I @synthesize all the variables in the warnings above.

With the following IBACTION

-(IBAction)randomWord {

NSString* result;
result = [self getRandomWord];
label.text = result;

}

When the user clicks the button once every word in my Words.txt file appears in the label. When the user clicks the button again nothing happens. So I modified it to the following:

-(IBAction)randomWord {

[self buildRandomWordArray];
NSString* result;
result = [self getRandomWord];
label.text = result;

[self resetRandomArray];
}

Now every time the user clicks the button the whole contents of my words.txt file is display in my label without in formating. Any suggestions.
rrichar is offline   Reply With Quote
Old 09-06-2010, 08:32 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by rrichar View Post
I @synthesize all the variables in the warnings above.
I don't understand that sentence. Post the code that includes the @synthesize statements. If you have them in the right place, you won't get the warnings you posted.


Quote:

With the following IBACTION

-(IBAction)randomWord {

NSString* result;
result = [self getRandomWord];
label.text = result;

}

When the user clicks the button once every word in my Words.txt file appears in the label. When the user clicks the button again nothing happens. So I modified it to the following:

-(IBAction)randomWord {

[self buildRandomWordArray];
NSString* result;
result = [self getRandomWord];
label.text = result;

[self resetRandomArray];
}

Now every time the user clicks the button the whole contents of my words.txt file is display in my label without in formating. Any suggestions.
The code I posted is written assuming each word is on a separate line in your text file. If you just have a text file
"word word word word word" then you will get everything as one "word"
__________________
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 09-06-2010, 08:34 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

This is the contents of my Words.txt file. There is a line after each word.

ANT
BAT
CAT
DOG
FAT
GUT
HIT
INK
JET
KNOT
LIKE
MAN
NOT
OX
PAT
MIX
RAT
SIT
TIN
IT
VENT
WET
FIX
YAK
ZEBRA
HAT
AX
SAT
FAX
FIT
MILK
HAIR
LION

Copied straight from XCODE.
rrichar is offline   Reply With Quote
Old 09-06-2010, 08:50 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by rrichar View Post


When the user clicks the button once every word in my Words.txt file appears in the label. When the user clicks the button again nothing happens.
What do you mean every word appears? Do they appear one at a time on top of each other in rapid succession? Do you have a really long label and you see all the words one right after another?

[QUOTE
Now every time the user clicks the button the whole contents of my words.txt file is display in my label without in formating. Any suggestions.[/quote]


Again, what do you mean the whole contents appears? Are they smooshed together with no spaces so you can see them all?

What event do you have connected your action method? You want to use touch up inside. That way the action doesn't get triggered until you release the button. Some of the other events might trigger the action to be triggered repeatedly. I'd have to look through the list of events to see which ones might fire repeatedly for a single press.
__________________
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 09-06-2010, 09:01 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

I have a long *** label. At the the current font size of the label every word of the Array appears in the label. One ling, every word one after another. So this is my txt file

ANT
BAT
CAT
DOG
FAT
GUT
HIT
INK
JET
KNOT
LIKE
MAN
NOT
OX
PAT
MIX
RAT
SIT
TIN
IT
VENT
WET
FIX
YAK
ZEBRA
HAT
AX
SAT
FAX
FIT
MILK
HAIR
LION

and here is the result when the user clicks the button.

ANTBATCATDOGFATGUTHITINKJETETC. If I increase the font size of the label some words get cut off. Only one word is suppose to be displayed.
rrichar is offline   Reply With Quote
Old 09-06-2010, 09:15 PM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by rrichar View Post
I have a long *** label. At the the current font size of the label every word of the Array appears in the label. One ling, every word one after another. So this is my txt file

ANT
BAT
CAT
DOG
FAT
GUT
HIT
INK
JET
KNOT
LIKE
MAN
NOT
OX
PAT
MIX
RAT
SIT
TIN
IT
VENT
WET
FIX
YAK
ZEBRA
HAT
AX
SAT
FAX
FIT
MILK
HAIR
LION

and here is the result when the user clicks the button.

ANTBATCATDOGFATGUTHITINKJETETC. If I increase the font size of the label some words get cut off. Only one word is suppose to be displayed.
In the code I posted, after the line


Code:
self.words = [text componentsSeparatedByString: @"\n"];
Add a debug statement:

NSLog(@"After componentsSeparatedByString, self.words = %@. array count = %d", self.words, [self.words count]);

What is the array count displayed to the console when you run it?
__________________
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 09-06-2010, 09:23 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 138
Default

I added the code. Now where do I go to give you the number I need ?
rrichar is offline   Reply With Quote
Reply

Bookmarks

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: 259
23 members and 236 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Desert Diva, Dominus, glenn_sayers, HemiMG, JasonR, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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