PDA

View Full Version : Need help using NSString




Beadon99
Nov 30, 2009, 09:33 AM
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:

- (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
Nov 30, 2009, 10:34 AM
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.

Beadon99
Dec 2, 2009, 08:44 AM
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.

dejo
Dec 2, 2009, 10:36 AM
Like I said earlier, none of us know the language and most of the class is just hands down confused.
Isn't learning the language one of the tasks of this class?

Beadon99
Dec 2, 2009, 04:56 PM
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.

newb16
Dec 3, 2009, 02:30 AM
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.

Beadon99
Dec 7, 2009, 09:28 AM
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.

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.

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.

dejo
Dec 7, 2009, 09:32 AM
int *part1picked = arc4random() % 4;
...
You sure you want a pointer to an int?

P.S. Pay attention to the warnings as well as the errors.

Beadon99
Dec 8, 2009, 07:47 AM
Just wondering but, why shouldn't you have a pointer to an int?

We didn't get any warnings about that though.

dejo
Dec 8, 2009, 08:58 AM
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:
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.

Beadon99
Dec 8, 2009, 03:25 PM
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?

dejo
Dec 8, 2009, 03:36 PM
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:
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.

Beadon99
Dec 8, 2009, 03:47 PM
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.

dejo
Dec 8, 2009, 03:54 PM
The only thing I'm having trouble with now is getting the strings into the label variables for the interface.
Care to elaborate? Are you trying to store sentence into a UILabel? What trouble are you having?

Beadon99
Dec 8, 2009, 03:59 PM
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.