iOS Need help using NSString

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
My friends and I are doing a project for one of our classes, it didn't seem to difficult but none of us know how to code effectively.

Basically our project will get the file path from three text files within the bundle. From the files we will randomly pull one line from each file. From these we will make random sentences that will generally be crazy.

We've researched a but we're still a bit confused. This is what we've got so far:

Code:
- (IBAction)displayRandom
{
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];  
	if (filePath) {  
		NSString *myText = [NSString stringWithContentsOfFile:filePath];  
		if (myText) {  
			ftext.text= myText;  
			
		}  
	}
from here we aren't too sure about to go from here. Any tips or help would be greatly appreciated.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
5
From the files we will randomly pull one line from each file.
Look at the NSString method componentsSeparatedByString: for creating an array out of all the lines of text in the file.

From these we will make random sentences that will generally be crazy.
You will need srandom() and random(). From there it's just generating the right range of random numbers as the array index and putting them back together with a mutable string.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
Could you possibly give us a code snippet? Like I said earlier, none of us know the language and most of the class is just hands down confused.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
No, the the class is a project class. Basically, our instructor gives us something to do and we do it. It just so happened to be an iPhone app and we just can't seem to learn anything about it.
 
Comment

newb16

macrumors regular
Feb 27, 2008
100
0
No, the the class is a project class. Basically, our instructor gives us something to do and we do it. It just so happened to be an iPhone app and we just can't seem to learn anything about it.
If you are not supposed to learn anything, just complete the 'project', then tell the instructor to give you exact code snippet you need to insert in the code to 'complete' the 'project'. Or read appropriate documentation.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
So after a bit of research, we've come up with a decent bit of code, but we're having some errors we can't find a solution to.

Code:
char *type[] = {"Warning, ","Alert, ","Caution, ","Danger "};
	int *part1picked = arc4random() % 4;
	NSString *part1word;
	
	char *dynamic[] = { "Conan O'Brien is ","Zombies are, Raptors are ", "Mutants are ", "Escaped Convicts are ", "Chef Boyardee is ", "Gordon Ramsey is ", "Rick Astley is ", "Vampires are ", "Frankenstein is ", "Werewolves are "};
	int *part2picked = arc4random() % 10;
	NSString *part2word;
		
	char *statik[] = { "attacking cars!", "eating people!", "starting a pyramid scheme!", "selling cookies!", "toilet papering houses!", "abusing cooking spray!", "rampaging across the highway!", "kidnapping your mom!", "cussing at old people!", "sniffing glue!", "tipping over port-o-potties!", "deep frying butter!", "selling fake jewelry!", "not honoring coupons!"};
	int *part3picked = arc4random() % 14;
	NSString *part3word;
	
	NSString *sentence;
		
	part1word = type[part1picked];
	part2word = dynamic[part2picked];
	part3word = statik[part3picked];
		
		sentence = [part1word stringByAppendingString:part2word stringByAppendingstring:part3word];
The errors that we're are getting are where we choose the words from strings.
Code:
part1word = type[part1picked];
part2word = dynamic[part2picked];
part3word = statik[part3picked];
Here is the error: Array subscript is not an integer

Any insight on this would be very helpful.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
Just wondering but, why shouldn't you have a pointer to an int?

We didn't get any warnings about that though.
 
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
Just wondering but, why shouldn't you have a pointer to an int?
Because that's probably not how you want to be referencing it. int is a primitive and thus is normally handled as such. So, either lose the pointer or you need to adjust how you reference it, such as when using it as an index in an array, like in this line:
Code:
part1word = type[part1picked];
You're not using a primitive value here, you're using a pointer to one.

We didn't get any warnings about that though.
Interesting. When I compiled the code you supplied, I got 4 warnings and 3 errors. The first three warnings were: "Initialization makes pointer from integer without a cast" on each of the "int *part..." lines.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
So which would be the better route to take? Using the index of an array or use a primitive value?

And from this, could you point me in what I should be looking at to accomplish this?
 
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
So which would be the better route to take? Using the index of an array or use a primitive value?
I don't think you properly comprehend the options, which are: 1) use a primitive value, and 2) use a pointer to a primitive value. Using an index of an array is inevitable, if you hope to access an element of the array.

And from this, could you point me in what I should be looking at to accomplish this?
Based on the code you supplied and how I believe you are using it, using a primitive value should be sufficient. So, start with:
Code:
int part1picked = arc4random() % 4;
Next, you should to decide if you want to be dealing with a C-type strings (char *) or Objective-C-type strings (NSString). You're mixing them needlessly.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
I figured most of it out in the last 5 minutes or so some how. The only thing I'm having trouble with now is getting the strings into the label variables for the interface. I went with the C route, so I'm using char for my strings.
 
Comment

Beadon99

macrumors newbie
Original poster
Nov 30, 2009
8
0
Well, I've modified the existing code, so basically the variable type will be in a separate label, which is one problem I'm having. Another is trying to join string dynamic and statik into one so I can store it in a UILabel as a whole.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.