我正在尝试在以下代码中设置一个属性值,但是当我退出该块时,该属性值为空。
- (PRSapoAuth *)initWithUser:(NSString *)user password:(NSString *)password forToken:{
__block NSString *token;
self = [super init];
if (!self) {
return nil;
}
NSString *urlAuth = [NSString stringWithFormat:@"https://services.sapo.pt/Codebits/gettoken?user=%@&password=%@", user, password];
NSString *modifiedURL = [urlAuth stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:modifiedURL]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
token = JSON[@"token"];
self.token = token;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
token = @"";
self.token = token;
}];
[operation addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionNew context:nil];
[operation start];
while (!operation.isFinished) {
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"%@", self.token);
}
有什么我做错了吗?