Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacBook MH

macrumors member
Original poster
Mar 7, 2009
76
0
Hi guys,
I'm trying to make an IPHONE app that would load two text files and always give you a random word (set it as the UILabel) from one of them. You will have to guess if it is from text file A or text file B. I want there to be only two buttons A and B. If your guess is right, it adds a point to integer "right" and if you are wrong it adds a point to int "wrong". I'm pretty new to iOS programming, and I have no clue to do this, mostly the part where the app takes a random word from the text a or b, and I'm also not sure how to distinguish if the users guess was right or not. I am able to create a simple interface and write some code towards it.
Any advice how to do that kind of apps?
 
Basically you want your app to read the text files and then parse the data into two arrays. Each element in each array would have one word. Then you can use a random number generator to select an index for the array (between 0 and the length of the array) and display that word. This should be fairly straightforward. I don't know the methods off the top of my head, but the documentation provided by apple should make it pretty easy to find them.

Displaying the words and the counter is pretty easy too. You can create the interface in Interface Builder with the UILabels and UIButtons, and then in your header file declare them as such:

IBOutlet UILabel *word;
IBOutlet UILabel *rightCounter;

Then in Interface Builder you can connect (hold down control and drag with the mouse pointer between the labels/buttons and "File's Owner" ) the elements.

Then you can programatically set the labels (word.text, rightCounter.text) as strings values.
 
Thanks,
Its definitely a good idea with the arrays. What about the random number generator? And I also would like some help with the methods (or at least keywords to look up in the manual or google)...
 
https://forums.macrumors.com/threads/291684/

That ought to help. That code segment is reading a file line by line and storing each line as an array element. If your text files are storing each word on a separate line, then I believe that's exactly what you want. If you're storing them in another manner then you'd need to be more creative with your delimiters (the part that the componentsSeperatedByString method takes as a parameter).

As for random number generators, looks like the standard is basically the same as it is for c... You seed a random number generator with a time, then call random() % highestValue (your array size) and that returns a random number between 0 and highestValue. There's some examples here:

http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c

Otherwise just google "Objective C Random Number"
 
ok thanks, but how do I do the correct/wrong part? and also, wouldn't I need to randomize the .txt files first?
 
Why? If you're randomly pulling a word from an array, what does it matter?
Yes, but you have to .txt files, how does the app randomly choose from one of them?
 
Yes, but you have to .txt files, how does the app randomly choose from one of them?

You didn't specify that that part needed to be random too :p

You can just do the exact same thing as for the array index, except use 2 as the highest value that you % (modulus) the random() value with. That will return either a 0 or a 1. If it's 0 you use one array, if it's 1 you use the other.

You also save that value of course so that you can compare it to the user's guess to see if they guessed correctly.
 
ok now how do I know from which txt file the given word is? Because I need that for the A and B button.
 
Im sorry guys, but im 14 years old... I do everything I know myself, but there are some things that are not completely clear to me.
 
It's ok. I think this project should be manageable for you, my advice is to break it down into parts. Pick a small part, like reading from the text files, and get that working. Make sure you understand how it's working, and then move on to another part.

That's actually good advice for working with software in general. A great approach in Software Engineering is to get something working, and then iterate often to improve it. Right now you're trying to do everything at once. Just take a step back and start at the beginning and take it one step at a time.
 
Yes, I'll do that, but first I needed to see if it was even possible for me to do this with my low coding skills. Now I created the interface and linked it to the IBOutlets. Now I'm trying to configure it so it loads the word from the txt file.
 
As cnstoll is alluding to: Decomposition

If that seems overwhelming, it's time to step back from the real coding and go learn the fundamentals of programming (in any language) and build up your comfort level from there.
 
As cnstoll is alluding to: Decomposition

If that seems overwhelming, it's time to step back from the real coding and go learn the fundamentals of programming (in any language) and build up your comfort level from there.

This is why it's best to practice by writing programs that one does understand how to write. Try new things in it? Sure. But taking simpler concepts and transforming them into software is how one learns how to represent concepts of all kinds in software. And breaking things down into simpler concepts that can be represented in software is exactly how complex things are done in software.
 
That would be what the random (0 or 1) would get you. If the generator gives you a 0, then you get a second random number (between 0 and length of array 0) and select a word. You save that 0 so that you know the word was from Array 0. Array 0 corresponds to one text file, and Array 1 corresponds to the second text file. Keeping track of the names of the files, or what they represent, is up to you.

Some of the questions you're asking are very basic design related decisions that you should be making yourself. More and more it feels like we're designing your app for you. Part of the fun of making these apps is thinking about these things and how the pieces should all work together. I don't mind answering questions at all, just try to keep it more programming and iOS specific.
 
Last edited by a moderator:
Break it down. It's like you're not even bothering to think any of this through.

If you have two separate arrays, which were loaded from two separate files, how do you think you might tell them apart? Maybe an array containing the two separate arrays, or maybe two separate named variables, possibly called wordsFromA and wordsFromB.

If you need every single part of the design handed to you or spelled out in complete detail, then you should probably pick a simpler program to write first. Seriously, if you're having this much difficulty translating a concept into an actual software design, then you need to work on those skills by translating concepts into designs. You won't learn much by having someone do all the design work for you.

It's ok. I think this project should be manageable for you, my advice is to break it down into parts. Pick a small part, like reading from the text files, and get that working. Make sure you understand how it's working, and then move on to another part.

That's actually good advice for working with software in general. A great approach in Software Engineering is to get something working, and then iterate often to improve it. Right now you're trying to do everything at once. Just take a step back and start at the beginning and take it one step at a time.
 
Last edited by a moderator:
Ok thanks,
you all are probably right, I should start up with something easier.
Most likely I will make a Navigation based application with the slovak grammar (im from Slovakia)... That is a lot easier i think.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.