Really new with objective c (building iphone app) and I'm trying to figure out how to properly understand how calling methods work (compared to c#, the most recent language I've been working with)
I have this implementation
@interface User : NSObject{
}
@property NSInteger Id;
@property NSString *email, *password;
-(BOOL)isValid;
@end
@implementation User
-(BOOL)isValid{
NSString *password = self.validateString:self.password;
NSString *email = self.email;
if(validUser){
return YES;
}else{
return NO;
}
}
EDIT: SOrry if it wasn't clear but this is the method I'm trying to call.
-(NSString *)validateString:(NSString *)string{
// process the string
return @"";
}
Basically I'm trying to create an instance in my view onclick of a button like so:
- (IBAction)btnSubmit:(id)sender {
// get values of email and password
// do an isvalid to check with web service.
User *user = [[User alloc] init];
user.email = @"email@email.com";
user.rawPassword = @"pass";
if(user.isValid){
// go to next page
}
else{
// else refresh current page
}
}
Is creating the instance on click and then passwing the values to process inside the instance a good practice?
Thanks!
No need of creating it's own object there. You can use the current object for doing this:
- (IBAction)btnSubmit:(id)sender
{
self.email = @"email@email.com";
self.rawPassword = @"pass";
if(self.isValid)
{
// go to next page
}
else
{
// else refresh current page
}
}
You can refer to same object within its scope using self
keyword. It's similar to this
keyword used in C++
I'm not sure which methods you are trying to call within the object but to call a method in the current object you use self
.
ie
[self runSomeFunction];
Use
Note:- If you are creating a button actiion in same class then there is no need to create an instance of same class , you can use self
- (IBAction)btnSubmit:(id)sender {
// get values of email and password
// do an isvalid to check with web service.
User *user = [[User alloc] init];//not need if its same user class
user.email = @"email@email.com";//self.email=@"email@email.com"; if same user class
user.rawPassword = @"pass";//self.rawPassword=@"pass"; if same user class
if([self isValid]){//made a change here as in objective c its a syntax to call a method not with "."
// go to next page
}
else{
// else refresh current page
}
}
That depens on whether you really need to alloc a new instance which we cannt say because that depends on your business logic.
If you would crate a new instance in c++ here with new, then yes, alloc/init a new instance and use it.
If you would refer to this
in c++, as your questiont title suggests, then you can use self
quite corresponding to this
. self.isValid
and [self isValid]
are equivalents.
This is for instance methods. Within class methods self
would refer to the class, not the instance
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.