Nooby cocoa programmer asking for help.

Everything todo with programming goes HERE.
Post Reply
User avatar
Loor
Average Program
Posts: 95
Joined: Sun May 06, 2007 11:54 pm
Location: ∞

Nooby cocoa programmer asking for help.

Post by Loor »

Hi all programmers.

I have recently gotten into to Mac programming, and i have made a little app that insults my friend by selecting a random insult out of however many i want whenever you press a button.
Now i've decided to take it a bit further, and make it insult whoever you type in the text field :D

I've got the text field, connected it through Interface builder, but whenever i try

Code: Select all

- (IBAction) getName: sender
	{
	int rand =  random()%11;
	NSString* insults[11];
	insults[0]=NSString* finalString = [name stringByAppendingString;@" smells."];

....

[textView setString: insults[rand]];
I get a "Parse error before NSString".
I know i need to do something with finalString, but i can't figure out what.
Any help would be much appreciated.

-Loor
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Post by Jonathan »

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 %.
}
User avatar
Loor
Average Program
Posts: 95
Joined: Sun May 06, 2007 11:54 pm
Location: ∞

Post by Loor »

Great! Thanks for your help!

Except, now,

Image

It gets the name of the button XD
User avatar
DDMJ
Reverse Outside Corner Grinder
Posts: 1882
Joined: Thu Jun 08, 2006 12:15 am
Location: LA, CA, USA, NA
Contact:

Post by DDMJ »

Jonathan wrote:A few problems:
- Loor smells.
It's not nice to say a girl smells :lol:.
User avatar
dlh
Formerly That OS X Guy
Posts: 2035
Joined: Fri Jan 02, 2004 12:05 am
Contact:

Post by dlh »

Loor wrote:Great! Thanks for your help!

Except, now,

[...]

It gets the name of the button XD

Code: Select all

[nameTextField stringValue];
Post Reply