我有一个抽象类AuthResult
有财产 - Token
模型类。如何覆盖拥有自己属性的属性对象的setter方法?
#import <Foundation/Foundation.h>
@class Token;
@interface AuthResult : NSObject
+ (instancetype)sharedInstance; // designated initializer
@property (readwrite, strong, nonatomic) Token *token;
@property (readwrite, nonatomic) BOOL isAuthorized;
@end
Token
模型类,在轮到他,有在5个属性:
#import <Foundation/Foundation.h>
@interface Token : NSObject
@property (readwrite, strong, nonatomic) NSString *accessToken;
@property (readwrite, strong, nonatomic) NSDate *expirationDate;
@property (readwrite, strong, nonatomic) NSString *tokenType;
@property (readwrite, strong, nonatomic) NSString *scope;
@property (readwrite, strong, nonatomic) NSString *refreshToken;
@end
我的目标是覆盖setter方法在AuthResult
类,以处理不同的情况。例如。在令牌刷新请求后,它具有refreshToken
属性,因此我不需要清除它。
我试过这个方法 - 二传手在AuthResult
类:
- (void)setToken:(Token *)token {
_token.accessToken = token.accessToken;
_token.expirationDate = token.expirationDate;
_token.tokenType = token.tokenType;
_token.scope = token.scope;
if (token.refreshToken != nil) {
// DO NOT OVERRIDE REFRESH_TOKEN HERE (after refresh token request it comes as null)
_token.refreshToken = token.refreshToken;
}
}
但它不工作。它使AuthResult
类中的令牌对象始终为空。
正如我所见 - 我没有访问对象属性。我可以访问实例变量 - “_token”对象。但我无法访问HIS属性。
请指教。谢谢
我想你应该在分配值之前检查'_token == nil'并在需要时创建它? – Lucho