Currently, I understand that I cannot set Property Variables within a class method.
For Example:
#ISUser.h
@interface ISUser : NSObject
@property (nonatomic, retain) NSString *username;
@property (nonatomic, retain) NSString *password;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *firstname;
@property (nonatomic, retain) NSString *lastname;
+ (void)logInWithUsernameInBackground:(NSString *)username
password:(NSString *)password
block:(ISUserResultBlock)block;
@end
I am looking through Parse's framework and am trying to get a better understanding of how to implement a login like they have done. The class method (void)logInWithUsernameInBackground:password:block
is where I attempt to assign the property variables username and password but it's a no go.
Here is the implementation of the current method:
+ (void)logInWithUsernameInBackground:(NSString *)username password:(NSString *)password block:(ISUserResultBlock)block
{
//self.username = username // Of course, I cannot do this
NSString *preferredLanguageCodes = [[NSLocale preferredLanguages] componentsJoinedByString:@", "];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kAPIHost, kAPIPath]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes] forHTTPHeaderField:@"Accept-Language"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSData * data = [[NSString stringWithFormat: @"command=login&username=%@&password=%@", username, password] dataUsingEncoding: NSUTF8StringEncoding];
[request setHTTPBody:data];
ConnectionBlock *connection = [[ConnectionBlock alloc] initWithRequest:request];
[connection executeRequestOnSuccess: ^(NSHTTPURLResponse *response, NSString *bodyString, NSError *error) {
block([self user], error);
} failure:^(NSHTTPURLResponse *response, NSString *bodyString, NSError *error) {
block([self user], error);
}];
}
Within the parse PFUser.h file, this is a class method... But how do they assign the property variables?
I know that static variables can be assigned/set within a class method but I would like to access such variables from another class.
EDIT: After viewing the first comment, The ISUser class has a singleton already implemented.
+ (instancetype)currentUser
{
static ISUser *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
But now what? I need to override the init method and set the variables? But how does the init method know what to set the variables to? Will I have to add parameters to the + (instancetype)currentUser
like so + (instancetype)currentUser:username password:(NSString *)password
then override the init method as well? + (instancetype)currentUser
is another class method that I pulled from the PFUser framework.