0

I've found a few similar questions to this but not relating to XCode.

I am creating an app where clicking a button generates a randomised quote and displays it in a label.

What I'd like to do is have a text file with all of the quotes, then have my app randomly select a quote from that file.

The problem is that my code at present reads the entire contents of the file.

    textHIYP = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
textHIYP.editable = NO;
[self.view addSubview:textHIYP];

textHIYP.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Quotes" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];

The only other way I can think of doing it is to have an arc4random() code that randomises which file it locates, with each file containing a single quote...but as I want to have over a hundred quotes, this seems less than ideal.

Any suggestions as to how to read a particular line of a text file?

Thanks heaps in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    This has nothing to do with Xcode. This is a Cocoa-touch / Objective-C question. – rmaddy Nov 16 '13 at 16:33
  • What is Cocoa-touch? The program I'm using is Xcode. Sorry if I've misunderstood! – David Taylor Nov 27 '13 at 01:32
  • Xcode is an IDE. Your question has nothing to do with the editor you are using. Objective-C is a programming language. Cocoa-touch is the iOS framework you are using. Your question is about how to use the framework, written in Objective-C. – rmaddy Nov 27 '13 at 02:09
  • Ugh...I couldn't look more clueless if I wore a hat that read "N00b"! Thanks! I'll have a hunt for Objective-C tutorials. :) – David Taylor Nov 27 '13 at 03:06

2 Answers2

1

You could read the whole file into an NSString, then split it into an array with componentsSeparatedByString:@"\n", keep that it memory, and return a random element of the array when needed. If there are just hundreds (and not, say, millions) of lines, the additional memory used should not be an issue.

Arkku
  • 41,011
  • 10
  • 62
  • 84
0

You're imitating the behavior of the old fortune program, so adopt its tactic, too. The database of quotes is a set of files with quotes separated by lines containing only a special character:

Cabbage crates coming over the briny!
%
Top hole, squiffy!
%
Next we have number four, ”crunchy frog“.
%
Lemon curry?
%

For each of those quote files, a companion file is generated (using the strfile utility) that stores the offset and length of each quote. To pick a quote, you load just the (relatively small) index file into memory, choose an element at random, then use that offset and length to seek and read a single quote from the appropriate quote file.

Here's a Python implementation of fortune on GitHub. The original C code should be floating around somewhere too.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Haha, great sense of humour! A fan of Monty Python, I see! Okay, as I'm completely new to Xcode and the language, I'm not really sure where to find code that I might be able to pull together to do this. Would the original alternative of having heaps of different files be a sloppy use of memory? After accessing a file and reading it, would I need to somehow close that file to retrieve the memory? The files in question would be very small - each one would have a sentence about the length of the ones quoted above - but it would be an ongoing act of loading these files. – David Taylor Nov 27 '13 at 01:40
  • ^ (One blurb daily for an indefinite length of time.) I just don't want the app to crash because I was supposed to do some kind of memory dump... :/ – David Taylor Nov 27 '13 at 01:42
  • Yes, you would open the file, read the line you wanted, and then close it. You can't avoid using the memory for the data you need, but the file's not using working memory just because it's open -- only when you actually get the contents. – jscs Nov 27 '13 at 08:26
  • Awesome - thanks. It may not be the most elegant solution, but given my lack of knowledge and the time pressure involved (I'm hoping to give this app as a gift for Christmas if possible) I'll go with that basic option. Thanks heaps for your help! – David Taylor Nov 27 '13 at 20:56