Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

antipex

macrumors member
Original poster
Aug 31, 2006
90
36
Portland, Oregon
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???

Code:
#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

macrumors 6502
Jul 25, 2006
294
0
The problem is that you didn't make write your string as @"Randoms", but just wrote @Randoms.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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:
Code:
#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

macrumors member
Original poster
Aug 31, 2006
90
36
Portland, Oregon
Wow thanks for the help! I plugged that code in though and got "error: parse error before ';' token" on this line:

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

DavidLeblond

macrumors 68020
Jan 6, 2004
2,351
694
Raleigh, NC
antipex said:
Wow thanks for the help! I plugged that code in though and got "error: parse error before ';' token" on this line:

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

Need another ]?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.