如何为具有自己属性的 属性 对象覆盖 setter 方法?
How to override setter method for property object that has own properties within?
我有一个摘要 class AuthResult
有 属性 - Token
模型 class.
#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
模型 class 依次有 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
我的目标是覆盖 AuthResult
class 中的 setter 方法以处理不同的情况。例如。在令牌刷新请求后,它 refreshToken
属性 是空的,所以我不需要擦除它。
我试过这种方法 - setter in AuthResult
class:
- (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
class 中的令牌对象始终为空。
如我所见 - 我无权访问对象属性。我确实有权访问实例变量 - “_token”对象。但是我无权访问他的属性。
请指教。谢谢
您忘记初始化令牌对象。
你可以这样做:
- (void)setToken:(Token *)token {
_token = [Token new]; //initialize Token Object
_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;
}
}
But it doesn't work. It makes token object in AuthResult class always empty.
您的 属性 token
具有 引用类型 并且此类的默认值为 nil
.
在您提供的代码中,您从未将任何值分配给 token
本身 。例如语句:
_token.accessToken = token.accessToken;
是一条指令,将值赋给对象的accessToken
,引用了_token
中的值——但作为[=中的值16=] 永远不会被你从 nil
更改的 属性 中没有引用的对象。由于 Objective-C 的规则,您不会在此处收到某些语言会给出的错误,该操作根本不执行任何操作。
问题是你到底想做什么?
If,我猜你的意图在这里,你的 AuthResult
对象应该有它自己的 Token
对象,它的属性应该被你的 setter 修改,那么你就差不多了,你只需要为你的 AuthResult
分配一个 Token
对象。您可以使用如下代码执行此操作:
- (void)setToken:(Token *)token
{
if(_token == nil)
_token = Token.new; // don't have a token yet, allocate one
// now update the fields of our _token from token
_token.accessToken = token.accessToken;
_token.expirationDate = token.expirationDate;
...
虽然这解决了您眼前的问题,但没有解决大的设计问题,您应该这样做吗? _token
中的值可以被 getter 读取,因为它是一个引用,这意味着它的属性可以被使用 getter 的人更改,然后设置它们。如果 _token
的对象引用属于您的 AuthResult
实例,而这可能不是您想要的,例如您可能希望 getter 到 return a 复制.
所以想想你的模型。
HTH
我有一个摘要 class AuthResult
有 属性 - Token
模型 class.
#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
模型 class 依次有 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
我的目标是覆盖 AuthResult
class 中的 setter 方法以处理不同的情况。例如。在令牌刷新请求后,它 refreshToken
属性 是空的,所以我不需要擦除它。
我试过这种方法 - setter in AuthResult
class:
- (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
class 中的令牌对象始终为空。
如我所见 - 我无权访问对象属性。我确实有权访问实例变量 - “_token”对象。但是我无权访问他的属性。
请指教。谢谢
您忘记初始化令牌对象。 你可以这样做:
- (void)setToken:(Token *)token {
_token = [Token new]; //initialize Token Object
_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;
}
}
But it doesn't work. It makes token object in AuthResult class always empty.
您的 属性 token
具有 引用类型 并且此类的默认值为 nil
.
在您提供的代码中,您从未将任何值分配给 token
本身 。例如语句:
_token.accessToken = token.accessToken;
是一条指令,将值赋给对象的accessToken
,引用了_token
中的值——但作为[=中的值16=] 永远不会被你从 nil
更改的 属性 中没有引用的对象。由于 Objective-C 的规则,您不会在此处收到某些语言会给出的错误,该操作根本不执行任何操作。
问题是你到底想做什么?
If,我猜你的意图在这里,你的 AuthResult
对象应该有它自己的 Token
对象,它的属性应该被你的 setter 修改,那么你就差不多了,你只需要为你的 AuthResult
分配一个 Token
对象。您可以使用如下代码执行此操作:
- (void)setToken:(Token *)token
{
if(_token == nil)
_token = Token.new; // don't have a token yet, allocate one
// now update the fields of our _token from token
_token.accessToken = token.accessToken;
_token.expirationDate = token.expirationDate;
...
虽然这解决了您眼前的问题,但没有解决大的设计问题,您应该这样做吗? _token
中的值可以被 getter 读取,因为它是一个引用,这意味着它的属性可以被使用 getter 的人更改,然后设置它们。如果 _token
的对象引用属于您的 AuthResult
实例,而这可能不是您想要的,例如您可能希望 getter 到 return a 复制.
所以想想你的模型。
HTH