属性必须是可读写的,而其主要属性必须是只读的 [英] Attribute must be readwrite while its primary must be read only
问题描述
我在情节提要中添加了一个 UITextView,在这种情况下,我为它创建了一个属性并将其连接到 UIView 的子视图(称为 FieldView)中.楼盘是这样的
I added a UITextView to storyboard that I created a property for and connected in a subview of UIView (called FieldView) in this case. The property was like this
@property (strong, nonatomic) UITextView * instructions;
那个 FieldView 是 viewController 的一个属性
That FieldView is a property of the viewController
@property (strong, nonatomic) IBOutlet FieldView *fieldView;
当我想在 viewController 中使用代码隐藏 UITextView *指令时,我在 .h 文件中声明了该属性,以便我最终可以在按下按钮时执行此操作
When I wanted to hide the UITextView *instructions with code in the viewController, I declared the property in the .h file so that I could eventually do this when a button was pressed
self.fieldView.instructions.hidden = YES;
但是,xCode 给了我一个错误
However, xCode's giving me an error
illegal redeclaration of property in class extension FieldView, attribute must be readwrite while its primary must be readonly
当我在 .h 和 .m 文件中添加 readwrite 时
When I added readwrite in both the .h and .m files
@property (weak, nonatomic, readwrite) IBOutlet UITextView *instructions;
it said `perhaps you intended this to be a readwrite redeclaration of a readonly public property
做我想做的事情的正确方法是什么?
What is the correct way to do what I am trying to do?
推荐答案
要解决您的问题,您需要在 .h
文件和 readwrite<中声明
readonly
属性.m
文件中的/code> 属性:
To resolve your issue you need declare readonly
property in .h
file and readwrite
property in .m
file:
//FieldView.h
@interface FieldView
@property (nonatomic, readonly, strong) UITextView *instructions;
@end
// FieldView.m
@interface FieldView()
@property (nonatomic, readwrite, strong) IBOutlet UITextView *instructions;
@end
这篇关于属性必须是可读写的,而其主要属性必须是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!