A Little Objective-C Help

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

  1. Garrett macrumors regular

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

    Anyhow, here is my .m file:
    Code:
    #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);
    	
    }
    
    
    @end
    
    And my .h file:
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface GBBkwld : NSObject {
        IBOutlet id spinner;
        IBOutlet NSSecureTextField *passwordValue;
        IBOutlet NSTextField *usernameValue;
        IBOutlet NSTextField *status;
    }
    - (IBAction)login:(id)sender;
    
    @end
    
    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?
    Code:
    - (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

    Joined:
    Feb 27, 2008
    #2
    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

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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

    Joined:
    Apr 8, 2008
    #4
    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