Attribute must be readwrite while its primary must

2019-09-03 15:45发布

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;

That FieldView is a property of the viewController

@property (strong, nonatomic) IBOutlet FieldView *fieldView;

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;

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

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?

2条回答
孤傲高冷的网名
2楼-- · 2019-09-03 16:01

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
查看更多
家丑人穷心不美
3楼-- · 2019-09-03 16:16

I Also got same issue,

If same name property is declared in .h file and you are again declaring it in extension then you will get this error.

So renaming property name will resolve the issue.

查看更多
登录 后发表回答