random string - how to - needed

- I got a single string to print to a text field but random strings still dont.


DO LOOP:


a. generate a random number between 1-20

b. apply the random number to a string variable (ie, create string 1, string 2, string x)

c. display the string to a text field



my code started with the example in the documentation, eg:


NSString *str = [NSStringstringWithFormat:@"THIS TEXT IS FINE"];




This works fine but I want the text field to randomly display any of 20 different strings.


So - include a random number generator ala:

int randomInt = (arc4random()%20) +1;


But how to insert or bind or somehow associate the random number to the string variable, string1, string15, string20 ?

this is the hard part, at least for me....


Finally display the string to the text field in my GUI:


textview2.text = str;



Ive been reading up on NSLog but its just not clear how to use it, or if I really need it.

Posted on Apr 22, 2011 9:34 AM

Reply
7 replies

Apr 22, 2011 9:43 AM in response to msuper69

I tried that - NSMuteableArray - NSArray - geez what a mess. I couldnt make it work. Where you point stars, commas, brackets, the order of the variables, makes no sense to me. Its too foreign.


By contrast, I hear tourists and other foreigners speaking variations on English here in the US all the time, I can still make out what they are saying, mostly.


It must be the relative unsophistication of Xcode - it only undestands things a few ways, good luck with endless error messages if you dont happen to guess correctly. Oh joy.


After trying the puzzle pieces for a few hours, you realize, you either lack endless patience to try every possible permutation, or you dont connect the dots sufficiently, or the topic is too abstract to embrace adequately with the knowledge at hand.

Apr 22, 2011 9:50 AM in response to Chris Chamberlain1

What are you talking about? Arrays in Objective-C are not difficult at all. If you are going to be programming in Objective-C you need to know how to use these classes or you will be making a lot of work for yourself.


"Relative unsophistication of Xcode"? What?????


"It must be the relative unsophistication of Xcode - it only undestands things a few ways, good luck with endless error messages if you dont happen to guess correctly. Oh joy."


It's called syntax. Every language has it and Objective-C is no more difficult than any other language.

Apr 22, 2011 9:55 AM in response to Chris Chamberlain1

The fact that you're comparing a derived, spoken language to a programming language indicates your lack of basic skills. It's a poor man that blames his tools. Suggest you either buckle down and learn Obj-C or abandon your involvement.


Either way, please don't assume others will share your approach. If you're here asking for help, we're glad to try - if you're only here to vent, please stop, thanks.

Apr 22, 2011 3:20 PM in response to Chris Chamberlain1

If using arrays is too hard (it's the most basic C, actually), you can always opt for the low-tech solution most newbee programmers stumble upon themselves -- including me, I must admit:


int randomInt = (arc4random()%20) +1;

if (randomInt == 1)

str = "My first string";

if (randomInt == 2)

str = "My second string";

etc.


Perfectly understandable, even for non-native speakers of C.

Apr 26, 2011 10:21 AM in response to msuper69

This code works, although admittedly, it may not be the best possible solution:


NSError* result;

int randomInt = (arc4random()%33) +1;

NSString* quote;

NSArray* lines;

NSString* path;

NSString* fileContent;

path = @"/Users/chrisc/Desktop/RANDOM10/words.txt";



//Read the file into a string object

fileContent = [NSStringstringWithContentsOfFile: path encoding: NSUTF8StringEncodingerror: &result];



//Break up the string into pieces divided by the quote symbol.

lines = [fileContent componentsSeparatedByString: @","];



if ([lines count] > 0)

quote = [lines objectAtIndex: randomInt];


//We broke the string into at least 2 pieces, so return the bit after the first quote.



else


//No quotes found, so set the quote string to the empty string

quote = @"";



NSLog(@"quoted string = '%@'", quote);


textview2.text = quote;


}


so my question is -


how can I shorten the path so that XCode reads the file that Ive copied to my resources folder (ie, it wont rely on the external path which wont be available once I compile this app) ?


I really dont understand the NSLog thing, the documentation on this term makes little sense.

Apr 28, 2011 12:19 AM in response to Chris Chamberlain1

NSBundle is probably what you want. You could do something like:

NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt"];

If you use a "copy files" build phase for your project in Xcode, you can make it copy words.txt into the Resources folder when the project is being built. This is pretty nice really because if you need to localise the words for a foreign language, the support's already there for that.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

random string - how to - needed

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.