ios - 在类方法中设置属性变量

标签 ios objective-c properties parse-platform class-method

目前,我知道我无法在类方法中设置属性变量。

例如:

#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

我正在研究 Parse 的框架,并试图更好地理解如何像他们一样实现登录。类方法 (void)logInWithUsernameInBackground:password:block 是我尝试分配属性变量用户名和密码的地方,但这是不行的。

这是当前方法的实现:

+ (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);
    }];
}

在解析 PFUser.h 文件中,这是一个类方法...但是它们如何分配属性变量?

我知道可以在类方法中分配/设置静态变量,但我想从另一个类访问此类变量。

编辑:查看第一条评论后,ISUser 类已经实现了一个单例。

+ (instancetype)currentUser
{
    static ISUser *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

但是现在呢?我需要重写 init 方法并设置变量吗?但是 init 方法如何知道将变量设置为什么?我是否必须向 + (instancetype)currentUser 添加参数,就像这样 + (instancetype)currentUser:username password:(NSString *)password 然后重写 init 方法? + (instancetype)currentUser 是我从 PFUser 框架中提取的另一个类方法。

最佳答案

您不能在类方法中设置属性。您可以在实例方法上执行此操作。那是因为属性是针对类的实例的。

在解析登录方法中,他们使用一些属性作为方法参数并将它们用于登录过程,但不对其进行操作。

希望对你有帮助

关于ios - 在类方法中设置属性变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23797892/

相关文章:

objective-c - NSStream 处理事件给出状态 4

c# - 监视给定类的属性值变化的快速方法?

java - log4j 喜欢 jar 文件中的属性吗?

ios - 如何与我的应用程序共享选定的文本?

objective-c - Canon EDSDK 在 Mac OS 10.7.5 上的一个非常基本的程序上因 EXC_BAD_ACCESS 而崩溃

objective-c - Xcode 4 和分支

variables - Gradle 任务中额外属性和变量之间的区别?

iOS PresentModalViewController 不工作

ios - 如何访问委托(delegate)函数中的变量?

iphone - IOS 为什么使用 _variable 初始化失败而不是使用 NSNumber 的 self.variable?