PDA

View Full Version : Cocoa Help for a n00b




antipex
Aug 31, 2006, 09:26 PM
I'm getting started with Cocoa programming, and writing a quick little program. When I try to compile this code I get "error: parse error before '@' token". What's wrong???

#import "MakePassword.h"

@implementation MakePassword

- (IBAction)MakePassword:(id)sender
{
char Randoms;
//[NSString TheResult];

srand([[NSDate date] timeIntervalSince1970]);

Randoms = (char)rand();


[ResultPasswordText setStringValue:@Randoms];
}

@end


And also, right now I just want it to display one character to see if it works. But how would I get it to generate, say, an 8 character string of random letters and numbers, then put it in ResultPasswordText?

Thanks!



kpua
Sep 1, 2006, 12:33 AM
The problem is that you didn't make write your string as @"Randoms", but just wrote @Randoms.

HiRez
Sep 1, 2006, 02:55 AM
I think actually you want to leave the @ out of there. @"Randoms" creates an Objective-C string constant, you don't want that. You don't need the @ symbol to access variables, such as chars.

To generate some pseudo-random alphanumeric codes, I might do something like this:#define PASSWORD_LENGTH 8

- (IBAction)doMakePassword:(id)sender {
srandom([[NSDate date] timeIntervalSince1970]);

char randoms[PASSWORD_LENGTH];
int i = 0;
char aRandom = 0;
for (i = 0; i < PASSWORD_LENGTH; i++) {
while (YES) {
aRandom = (char)random() + 128;
if (((aRandom >= '0') && (aRandom <= '9')) || ((aRandom >= 'a') && (aRandom <= 'z'))) {
randoms[i] = aRandom;
break; // we found an alphanumeric character, move on
}
}
}

[resultPasswordText setStringValue:[NSString stringWithCString:(const char*)randoms length:PASSWORD_LENGTH]];
}

There are probably better ways to do that, but I think that works.

Also, in Objective-C, object instance names, variable names, and method names are always intercapped and uncapitalized ("myObject", "myMethod", "randoms", "makePassword"), while class names are capitalized and usually preceded with a two- or three-character code that is personal to you such as your initials ("NSString", "CSTBinaryCounter"). Constants are found in some different forms, but all caps with underscores is common ("PASSWORD_LENGTH"). On the Mac, prefixing a "k" to a name is another popular way to mark a constant ("kPasswordLength"). You also have a method named exactly the same as the class, which may not be a problem but it is usually not done.

antipex
Sep 1, 2006, 09:11 AM
Wow thanks for the help! I plugged that code in though and got "error: parse error before ';' token" on this line:

[ResultPasswordText setStringValue:[NSString stringWithCString:(const char*)randoms length:PASSWORD_LENGTH];

DavidLeblond
Sep 1, 2006, 09:26 AM
Wow thanks for the help! I plugged that code in though and got "error: parse error before ';' token" on this line:

[ResultPasswordText setStringValue:[NSString stringWithCString:(const char*)randoms length:PASSWORD_LENGTH];

Need another ]?

HiRez
Sep 1, 2006, 09:38 AM
Need another ]?
Yeah, sorry about that, I did some cutting and pasting and guess I dropped that somewhere...fixed it in my post.

antipex
Sep 1, 2006, 10:20 AM
Awesome - works wondefully! Thanks so much for your time and help :D