A Little Objective-C Help

Discussion in 'Mac Programming' started by Garrett, Jul 8, 2008.

  1. Garrett macrumors regular

    Apr 4, 2007
    I just got back into Obj-C, I guess I can never say I was ever in. :p

    Anyhow, here is my .m file:
    #import "GBBkwld.h"
    @implementation GBBkwld
    - (IBAction)login:(id)sender {
    	NSString *usernameString = [usernameValue stringValue];
    	NSString *passwordString = [passwordValue stringValue];
    	if(usernameString == "Garrett" && passwordString == "password") {
    		//[spinner startAnimation:self];
    		[status setStringValue:@"Logged In!"];
    	} else {
    		//[spinner stopAnimation:self];
    		//[usernameValue setEnabled:YES];
    		//[passwordValue setEnabled:YES];
    		[status setStringValue:@"Wrong Info."];
    	NSLog(@"Username: %@ and Password: %@", usernameString, passwordString);
    And my .h file:
    #import <Cocoa/Cocoa.h>
    @interface GBBkwld : NSObject {
        IBOutlet id spinner;
        IBOutlet NSSecureTextField *passwordValue;
        IBOutlet NSTextField *usernameValue;
        IBOutlet NSTextField *status;
    - (IBAction)login:(id)sender;
    I was hoping somebody could look over it for any tidying up that you can see. I think it's good so far.

    My 3 problems though:
    1. Understanding Methods, I don't know how to call them. I try: [test url:mad:"~/test.xml"], but it says Test isn't defined, how would I define it for something like this?
    - (void) loginTest:(NSString*)url {
     NSLog(@"%@", url);
    2. How would it be best for me to authenticate some users? I have a database set up (on Apache/PHP/MySQL), I was assuming I would have to make a mini-api to call back and forth and read the XML. Is that the only way, or what?
    3. usernameString == "Garrett", that doesn't work, regardless if I ever put Garrett in the username field.

    Thanks for all the help in advance!
  2. newb16 macrumors regular

    Feb 27, 2008
    Because NSString* is not like c++ std::string and there is no overloaded '==' operator. Use [username compare: @"foo"] or something like doc reads. Comparing them as two pointers you always get false.
  3. HiRez macrumors 603


    Jan 6, 2004
    Western US
    On the left is the object you are calling, on the right goes the message you want to send along with parameters. In your case, if you are calling that method from the same object, you'd write [self loginTest:mad:"~/test.xml]; or from another object you change self to the name of the object instance that your loginTest method is contained in. For example, if you have it in a class called LoginController, and you've created an instance of that called myLoginController, you'd write [myLoginController loginTest:mad:"~/test.xml]; Again, this is only if you call it from a different object. When calling into the same object, you use self.

    I don't know much about that but I'd think you could just use the MySQL C API to query the DB directly. Someone may have also written an objective-C wrapper for it. If your DB doesn't really need to be MySQL, you might consider making a CoreData application and use XML or SQLite for the store, it would probably be less painful in the long run.

    As newb16 says, that won't work, use if ([someString isEqualToString:anotherString]) ... to compare two NSStrings for content instead of the pointer value (memory location).
  4. luckylefty01 macrumors member

    Apr 8, 2008
    I would also note that since you're dealing with NSStrings, "Garrett" needs to be @"Garrett", with an at symbol in front.

    I would also use [usernameString isEqualToString:mad:"Garrett"]; instead of compare:

Share This Page