我有一個抽象類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