Cocoa Help for a n00b

Discussion in 'Mac Programming' started by antipex, Aug 31, 2006.

  1. macrumors member

    antipex

    Joined:
    Aug 31, 2006
    Location:
    Portland, OR
    #1
    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!
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    The problem is that you didn't make write your string as @"Randoms", but just wrote @Randoms.
     
  3. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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.
     
  4. thread starter macrumors member

    antipex

    Joined:
    Aug 31, 2006
    Location:
    Portland, OR
    #4
    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];
     
  5. macrumors 68020

    DavidLeblond

    Joined:
    Jan 6, 2004
    Location:
    Raleigh, NC
    #5
    Need another ]?
     
  6. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    Yeah, sorry about that, I did some cutting and pasting and guess I dropped that somewhere...fixed it in my post.
     
  7. thread starter macrumors member

    antipex

    Joined:
    Aug 31, 2006
    Location:
    Portland, OR
    #7
    Awesome - works wondefully! Thanks so much for your time and help :D
     

Share This Page