A few problems:
- Loor smells.
- You try to use a variable declaration like any expression. Don't insert = NSString* finalString there. This would be better, if you really need finalString at all:
Code: Select all
NSString *finalString = insults[0] = ...
How/whether you put spaces around * doesn't really matter, FYI, but I prefer to put the * closer to where it takes effect (int *a, *b; int* c, d; is equivalent to int *a; int *b; int *c; int d;).
- You used ; instead of : before @" smells."
- "getName" displays an insult in a text field?
- This would be cleaner:
Code: Select all
NSString *insults[11] = {
@" smells.",
etc
};
- (IBAction) getName: sender // sender is usually prepended by some type but I'm no Cocoa/Obj-C expert.
{
[textView setString:[name stringByAppendingString:insults[rand()%11]]];
}
Some thoughts:
Code: Select all
// static keeps insults from affecting other source files, where it isn't used. It might cause trouble otherwise if another file also defines insults.
static NSString *insults[] = {
@"%@ smells.",
@"This person is too stubborn to move to the left side: %@",
@"%@ was 100%% lucky.",
etc
};
#define ARRAY_ELEMENTS(array) (sizeof (array)/sizeof *(array))
- (IBAction) getName: sender
{
// This sizeof trick gives the # of elements in an array. Now you can easily add/remove insults without worrying about outdated constants throughout your code.
[textView setString:[NSString stringWithFormat:insults[rand()%(sizeof insults/sizeof *insults)]]];
// Can also #define it.
[textView setString:[NSString stringWithFormat:insults[rand()%ARRAY_ELEMENTS(insults)], name]];
// Note that I used stringWithFormat and added ", name". It allows you to move the name around in the message with %@. Use %% if you want an actual %.
}