仅允许从一个特定的 class 更改 属性
Allow change in property only from one specific class
假设我有一个 class Participant
看起来像这样
Participant.h
@interface Participant : NSObject
@property(nonatomic, readonly) NSString *name;
@property(nonatomic, readonly) NSString *id;
@end
这里的属性是readonly
因为我不希望任何人使用这个界面来改变它
除了ParticipantManager.h
我应该在 Participant
class 中做哪些更改以及如何创建 ParticipantManager
以便只有 ParticipantManager
可以更改 Participant
[= 的属性38=]
更多上下文
当值发生变化时,我从 react-native
收到一个事件。为了保持同步,我希望我的界面 ParticipantManager
只更改值。
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface ParticipantManager : RCTEventEmitter <RCTBridgeModule>
@end
^^ 以上class应该只能改变Participant
的属性 class
用户要更改值,他会调用 changeName
方法,该方法会将事件发送回 react-native
,其中 react-native
会更改值并发送回本机代码
我试过的。
所以,我考虑过使用 class 扩展概念,但我遇到了错误。
这是我做的
我创建 Participant+Private.h
实现 setName 方法
#import " Participant.h"
interface Participant()
- (void)setName:(NSString)name
- (void)setId:(NSString)name
@end
PS: 我在Participant.h
中实现了setName和setId方法
- (void)setName:(NSString)name {
_name = name;
}
但是当我在 ParticipantManager.h
中使用它时,它抛出错误
No visible @interface for Participant
declares the selector
setName
我是这样用的
#import "Participant+Private.h"
NSString* value = @"varun";
[[Participant sharedInstance] setName:value];
谁能帮我解决错误?
稍微详细的问题在这里:
No visible @interface for Participant declares the selector setName
Objective-C 不提供那种合理的隐私,例如“除了我在此命名的其他特定 class 之外,我对所有人都是私人的”。 (实际上,我不知道有哪一种语言有这种行为,当然这并不是说没有这样的语言。)
如果这是一个框架,您可以使用 @package
隐私将某些内容的隐私限制在同一框架中的其他 class 中。
否则,Objective-C 通常通过导入 header 来解决可见性问题,因此如果您将 Participant 的 public 访问器放入 header 并且唯一的 class 导入 header 是 ParticipantManager,那么只有 ParticipantManager 能看到它们。
假设我有一个 class Participant
看起来像这样
Participant.h
@interface Participant : NSObject
@property(nonatomic, readonly) NSString *name;
@property(nonatomic, readonly) NSString *id;
@end
这里的属性是readonly
因为我不希望任何人使用这个界面来改变它
除了ParticipantManager.h
我应该在 Participant
class 中做哪些更改以及如何创建 ParticipantManager
以便只有 ParticipantManager
可以更改 Participant
[= 的属性38=]
更多上下文
当值发生变化时,我从 react-native
收到一个事件。为了保持同步,我希望我的界面 ParticipantManager
只更改值。
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface ParticipantManager : RCTEventEmitter <RCTBridgeModule>
@end
^^ 以上class应该只能改变Participant
的属性 class
用户要更改值,他会调用 changeName
方法,该方法会将事件发送回 react-native
,其中 react-native
会更改值并发送回本机代码
我试过的。
所以,我考虑过使用 class 扩展概念,但我遇到了错误。
这是我做的
我创建 Participant+Private.h
实现 setName 方法
#import " Participant.h"
interface Participant()
- (void)setName:(NSString)name
- (void)setId:(NSString)name
@end
PS: 我在Participant.h
- (void)setName:(NSString)name {
_name = name;
}
但是当我在 ParticipantManager.h
中使用它时,它抛出错误
No visible @interface for
Participant
declares the selectorsetName
我是这样用的
#import "Participant+Private.h"
NSString* value = @"varun";
[[Participant sharedInstance] setName:value];
谁能帮我解决错误?
稍微详细的问题在这里: No visible @interface for Participant declares the selector setName
Objective-C 不提供那种合理的隐私,例如“除了我在此命名的其他特定 class 之外,我对所有人都是私人的”。 (实际上,我不知道有哪一种语言有这种行为,当然这并不是说没有这样的语言。)
如果这是一个框架,您可以使用 @package
隐私将某些内容的隐私限制在同一框架中的其他 class 中。
否则,Objective-C 通常通过导入 header 来解决可见性问题,因此如果您将 Participant 的 public 访问器放入 header 并且唯一的 class 导入 header 是 ParticipantManager,那么只有 ParticipantManager 能看到它们。