0

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.

jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • 1
    You can use a class method with a singleton pattern - the first call to `logInWithUsernameInBackground` would allocate an object and perform the login. Subsequent calls would use the existing object and simply call the completion block immediately - http://www.galloway.me.uk/tutorials/singleton-classes/ – Paulw11 May 22 '14 at 05:01

1 Answers1

1

You can not set properties in class method. You can do it on instance methods. Thats because properties are for instances of the class.

In the parse login method they use some properties as method parameters and use them for the login process, but not manipulate them.

Hope it helps

Sergio B.
  • 81
  • 3