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

Garrett

macrumors regular
Original poster
Apr 4, 2007
173
0
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!
 

newb16

macrumors regular
Feb 27, 2008
100
0
I just got back into Obj-C, I guess I can never say 3. usernameString == "Garrett", that doesn't work, regardless if I ever put Garrett in the username field.

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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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);
}
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.

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?
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.

3. usernameString == "Garrett", that doesn't work, regardless if I ever put Garrett in the username field.

Thanks for all the help in advance!
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).
 

luckylefty01

macrumors member
Apr 8, 2008
32
0
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.